Refactoring superblock features getters
[lwext4.git] / lwext4 / 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_crc32c.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 = 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 (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 static uint32_t ext4_sb_csum(struct ext4_sblock *s)
84 {
85         return ext4_crc32c(~0, s,
86                         ext4_offsetof(struct ext4_sblock, checksum));
87 }
88
89 static bool ext4_sb_verify_csum(struct ext4_sblock *s)
90 {
91         if (!ext4_sb_feature_ro_com(s, EXT4_FRO_COM_METADATA_CSUM))
92                 return true;
93
94         if (s->checksum_type != to_le32(EXT4_CHECKSUM_CRC32C))
95                 return false;
96
97         return s->checksum == to_le32(ext4_sb_csum(s));
98 }
99
100 static void ext4_sb_set_csum(struct ext4_sblock *s)
101 {
102         if (!ext4_sb_feature_ro_com(s, EXT4_FRO_COM_METADATA_CSUM))
103                 return;
104
105         s->checksum = to_le32(ext4_sb_csum(s));
106 }
107
108 int ext4_sb_write(struct ext4_blockdev *bdev, struct ext4_sblock *s)
109 {
110         ext4_sb_set_csum(s);
111         return ext4_block_writebytes(bdev, EXT4_SUPERBLOCK_OFFSET, s,
112                                      EXT4_SUPERBLOCK_SIZE);
113 }
114
115 int ext4_sb_read(struct ext4_blockdev *bdev, struct ext4_sblock *s)
116 {
117         return ext4_block_readbytes(bdev, EXT4_SUPERBLOCK_OFFSET, s,
118                                     EXT4_SUPERBLOCK_SIZE);
119 }
120
121 bool ext4_sb_check(struct ext4_sblock *s)
122 {
123         if (ext4_get16(s, magic) != EXT4_SUPERBLOCK_MAGIC)
124                 return false;
125
126         if (ext4_get32(s, inodes_count) == 0)
127                 return false;
128
129         if (ext4_sb_get_blocks_cnt(s) == 0)
130                 return false;
131
132         if (ext4_get32(s, blocks_per_group) == 0)
133                 return false;
134
135         if (ext4_get32(s, inodes_per_group) == 0)
136                 return false;
137
138         if (ext4_get16(s, inode_size) < 128)
139                 return false;
140
141         if (ext4_get32(s, first_inode) < 11)
142                 return false;
143
144         if (ext4_sb_get_desc_size(s) < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
145                 return false;
146
147         if (ext4_sb_get_desc_size(s) > EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
148                 return false;
149
150         if (!ext4_sb_verify_csum(s))
151                 return false;
152
153         return true;
154 }
155
156 static inline int is_power_of(uint32_t a, uint32_t b)
157 {
158         while (1) {
159                 if (a < b)
160                         return 0;
161                 if (a == b)
162                         return 1;
163                 if ((a % b) != 0)
164                         return 0;
165                 a = a / b;
166         }
167 }
168
169 bool ext4_sb_sparse(uint32_t group)
170 {
171         if (group <= 1)
172                 return 1;
173
174         if (!(group & 1))
175                 return 0;
176
177         return (is_power_of(group, 7) || is_power_of(group, 5) ||
178                 is_power_of(group, 3));
179 }
180
181 bool ext4_sb_is_super_in_bg(struct ext4_sblock *s, uint32_t group)
182 {
183         if (ext4_sb_feature_ro_com(s, EXT4_FRO_COM_SPARSE_SUPER) &&
184             !ext4_sb_sparse(group))
185                 return false;
186         return true;
187 }
188
189 static uint32_t ext4_bg_num_gdb_meta(struct ext4_sblock *s, uint32_t group)
190 {
191         uint32_t dsc_per_block =
192             ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s);
193
194         uint32_t metagroup = group / dsc_per_block;
195         uint32_t first = metagroup * dsc_per_block;
196         uint32_t last = first + dsc_per_block - 1;
197
198         if (group == first || group == first + 1 || group == last)
199                 return 1;
200         return 0;
201 }
202
203 static uint32_t ext4_bg_num_gdb_nometa(struct ext4_sblock *s, uint32_t group)
204 {
205         if (!ext4_sb_is_super_in_bg(s, group))
206                 return 0;
207         uint32_t dsc_per_block =
208             ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s);
209
210         uint32_t db_count =
211             (ext4_block_group_cnt(s) + dsc_per_block - 1) / dsc_per_block;
212
213         if (ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG))
214                 return ext4_sb_first_meta_bg(s);
215
216         return db_count;
217 }
218
219 uint32_t ext4_bg_num_gdb(struct ext4_sblock *s, uint32_t group)
220 {
221         uint32_t dsc_per_block =
222             ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s);
223         uint32_t first_meta_bg = ext4_sb_first_meta_bg(s);
224         uint32_t metagroup = group / dsc_per_block;
225
226         if (!ext4_sb_feature_incom(s,EXT4_FINCOM_META_BG) ||
227             metagroup < first_meta_bg)
228                 return ext4_bg_num_gdb_nometa(s, group);
229
230         return ext4_bg_num_gdb_meta(s, group);
231 }
232
233 uint32_t ext4_num_base_meta_clusters(struct ext4_sblock *s,
234                                      uint32_t block_group)
235 {
236         uint32_t num;
237         uint32_t dsc_per_block =
238             ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s);
239
240         num = ext4_sb_is_super_in_bg(s, block_group);
241
242         if (!ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG) ||
243             block_group < ext4_sb_first_meta_bg(s) * dsc_per_block) {
244                 if (num) {
245                         num += ext4_bg_num_gdb(s, block_group);
246                         num += ext4_get16(s, s_reserved_gdt_blocks);
247                 }
248         } else {
249                 num += ext4_bg_num_gdb(s, block_group);
250         }
251
252         uint32_t clustersize = 1024 << ext4_get32(s, log_cluster_size);
253         uint32_t cluster_ratio = clustersize / ext4_sb_get_block_size(s);
254         uint32_t v =
255             (num + cluster_ratio - 1) >> ext4_get32(s, log_cluster_size);
256
257         return v;
258 }
259
260 /**
261  * @}
262  */