Improve include policy
[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
45 uint32_t ext4_block_group_cnt(struct ext4_sblock *s)
46 {
47     uint64_t blocks_count = ext4_sb_get_blocks_cnt(s);
48     uint32_t blocks_per_group = ext4_get32(s, blocks_per_group);
49
50     uint32_t block_groups_count = blocks_count / blocks_per_group;
51
52     if (blocks_count % blocks_per_group)
53         block_groups_count++;
54
55     return block_groups_count;
56 }
57
58 uint32_t ext4_blocks_in_group_cnt(struct ext4_sblock *s, uint32_t bgid)
59 {
60     uint32_t block_group_count = ext4_block_group_cnt(s);
61     uint32_t blocks_per_group = ext4_get32(s, blocks_per_group);
62     uint64_t total_blocks = ext4_sb_get_blocks_cnt(s);
63
64     if (bgid < block_group_count - 1)
65         return blocks_per_group;
66
67     return (total_blocks - ((block_group_count - 1) * blocks_per_group));
68 }
69
70 uint32_t ext4_inodes_in_group_cnt(struct ext4_sblock *s, uint32_t bgid)
71 {
72     uint32_t block_group_count = ext4_block_group_cnt(s);
73     uint32_t inodes_per_group = ext4_get32(s, inodes_per_group);
74     uint32_t total_inodes = ext4_get32(s, inodes_count);
75
76     if (bgid < block_group_count - 1)
77         return inodes_per_group;
78
79     return (total_inodes - ((block_group_count - 1) * inodes_per_group));
80 }
81
82 int ext4_sb_write(struct ext4_blockdev *bdev, struct ext4_sblock *s)
83 {
84     return ext4_block_writebytes(bdev, EXT4_SUPERBLOCK_OFFSET, s,
85                                  EXT4_SUPERBLOCK_SIZE);
86 }
87
88 int ext4_sb_read(struct ext4_blockdev *bdev, struct ext4_sblock *s)
89 {
90     return ext4_block_readbytes(bdev, EXT4_SUPERBLOCK_OFFSET, s,
91                                 EXT4_SUPERBLOCK_SIZE);
92 }
93
94 bool ext4_sb_check(struct ext4_sblock *s)
95 {
96     if (ext4_get16(s, magic) != EXT4_SUPERBLOCK_MAGIC)
97         return false;
98
99     if (ext4_get32(s, inodes_count) == 0)
100         return false;
101
102     if (ext4_sb_get_blocks_cnt(s) == 0)
103         return false;
104
105     if (ext4_get32(s, blocks_per_group) == 0)
106         return false;
107
108     if (ext4_get32(s, inodes_per_group) == 0)
109         return false;
110
111     if (ext4_get16(s, inode_size) < 128)
112         return false;
113
114     if (ext4_get32(s, first_inode) < 11)
115         return false;
116
117     if (ext4_sb_get_desc_size(s) < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
118         return false;
119
120     if (ext4_sb_get_desc_size(s) > EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
121         return false;
122
123     return true;
124 }
125
126 static inline int is_multiple(uint32_t a, uint32_t b)
127 {
128     while (1) {
129         if (a < b)
130             return 0;
131         if (a == b)
132             return 1;
133         if ((a % b) != 0)
134             return 0;
135         a = a / b;
136     }
137 }
138
139 static int ext4_sb_sparse(uint32_t group)
140 {
141     if (group <= 1)
142         return 1;
143
144     if (!(group & 1))
145         return 0;
146
147     return (is_multiple(group, 7) || is_multiple(group, 5) ||
148             is_multiple(group, 3));
149 }
150
151 bool ext4_sb_is_super_in_bg(struct ext4_sblock *s, uint32_t group)
152 {
153     if (ext4_sb_has_feature_read_only(s, EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
154         !ext4_sb_sparse(group))
155         return false;
156     return true;
157 }
158
159 static uint32_t ext4_bg_num_gdb_meta(struct ext4_sblock *s, uint32_t group)
160 {
161     uint32_t dsc_per_block =
162         ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s);
163
164     uint32_t metagroup = group / dsc_per_block;
165     uint32_t first = metagroup * dsc_per_block;
166     uint32_t last = first + dsc_per_block - 1;
167
168     if (group == first || group == first + 1 || group == last)
169         return 1;
170     return 0;
171 }
172
173 static uint32_t ext4_bg_num_gdb_nometa(struct ext4_sblock *s, uint32_t group)
174 {
175     if (!ext4_sb_is_super_in_bg(s, group))
176         return 0;
177     uint32_t dsc_per_block =
178         ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s);
179
180     uint32_t db_count =
181         (ext4_block_group_cnt(s) + dsc_per_block - 1) / dsc_per_block;
182
183     if (ext4_sb_has_feature_incompatible(s, EXT4_FEATURE_INCOMPAT_META_BG))
184         return ext4_sb_first_meta_bg(s);
185
186     return db_count;
187 }
188
189 uint32_t ext4_bg_num_gdb(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     uint32_t first_meta_bg = ext4_sb_first_meta_bg(s);
194     uint32_t metagroup = group / dsc_per_block;
195
196     if (!ext4_sb_has_feature_incompatible(s, EXT4_FEATURE_INCOMPAT_META_BG) ||
197         metagroup < first_meta_bg)
198         return ext4_bg_num_gdb_nometa(s, group);
199
200     return ext4_bg_num_gdb_meta(s, group);
201 }
202
203 uint32_t ext4_num_base_meta_clusters(struct ext4_sblock *s,
204                                      uint32_t block_group)
205 {
206     uint32_t num;
207     uint32_t dsc_per_block =
208         ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s);
209
210     num = ext4_sb_is_super_in_bg(s, block_group);
211
212     if (!ext4_sb_has_feature_incompatible(s, EXT4_FEATURE_INCOMPAT_META_BG) ||
213         block_group < ext4_sb_first_meta_bg(s) * dsc_per_block) {
214         if (num) {
215             num += ext4_bg_num_gdb(s, block_group);
216             num += ext4_get16(s, s_reserved_gdt_blocks);
217         }
218     } else {
219         num += ext4_bg_num_gdb(s, block_group);
220     }
221
222     uint32_t clustersize = 1024 << ext4_get32(s, log_cluster_size);
223     uint32_t cluster_ratio = clustersize / ext4_sb_get_block_size(s);
224     uint32_t v = (num + cluster_ratio - 1) >> ext4_get32(s, log_cluster_size);
225
226     return v;
227 }
228
229 /**
230  * @}
231  */