Add extra config options
[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
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
69     return (total_blocks - ((block_group_count - 1) * blocks_per_group));
70 }
71
72 uint32_t ext4_inodes_in_group_cnt(struct ext4_sblock *s, uint32_t bgid)
73 {
74     uint32_t block_group_count = ext4_block_group_cnt(s);
75     uint32_t inodes_per_group  = ext4_get32(s, inodes_per_group);
76     uint32_t total_inodes = ext4_get32(s, inodes_count);
77
78
79     if (bgid < block_group_count - 1)
80         return inodes_per_group;
81
82     return (total_inodes - ((block_group_count - 1) * inodes_per_group));
83 }
84
85 int ext4_sb_write(struct ext4_blockdev *bdev, struct ext4_sblock *s)
86 {
87     return ext4_block_writebytes(bdev, EXT4_SUPERBLOCK_OFFSET,
88             s, EXT4_SUPERBLOCK_SIZE);
89 }
90
91 int ext4_sb_read(struct ext4_blockdev *bdev, struct ext4_sblock *s)
92 {
93     return ext4_block_readbytes(bdev, EXT4_SUPERBLOCK_OFFSET,
94             s, EXT4_SUPERBLOCK_SIZE);
95 }
96
97 bool ext4_sb_check(struct ext4_sblock *s)
98 {
99     if (ext4_get16(s, magic) != EXT4_SUPERBLOCK_MAGIC)
100         return false;
101
102     if (ext4_get32(s, inodes_count) == 0)
103         return false;
104
105     if (ext4_sb_get_blocks_cnt(s) == 0)
106         return false;
107
108     if (ext4_get32(s, blocks_per_group) == 0)
109         return false;
110
111     if (ext4_get32(s, inodes_per_group) == 0)
112         return false;
113
114     if (ext4_get16(s, inode_size) < 128)
115         return false;
116
117     if (ext4_get32(s, first_inode) < 11)
118         return false;
119
120     if (ext4_sb_get_desc_size(s) < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
121         return false;
122
123     if (ext4_sb_get_desc_size(s) > EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
124         return false;
125
126     return true;
127 }
128
129 static inline int is_multiple(uint32_t a, uint32_t b)
130 {
131     while (1) {
132         if (a < b)
133             return 0;
134         if (a == b)
135             return 1;
136         if ((a % b) != 0)
137             return 0;
138         a = a / b;
139     }
140 }
141
142 static int ext4_sb_sparse(uint32_t group)
143 {
144     if (group <= 1)
145         return 1;
146
147     if (!(group & 1))
148         return 0;
149
150     return (is_multiple(group, 7) || is_multiple(group, 5) ||
151             is_multiple(group, 3));
152 }
153
154
155 bool ext4_sb_is_super_in_bg(struct ext4_sblock *s, uint32_t group)
156 {
157     if (ext4_sb_has_feature_read_only(s,
158             EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
159             !ext4_sb_sparse(group))
160         return false;
161     return true;
162 }
163
164
165 /**
166  * @}
167  */