b7a4625a906d567a24d46b94772c83e551977a4b
[lwext4.git] / lwext4 / ext4_block_group.h
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_block_group.h
39  * @brief Block group function set.
40  */
41
42 #ifndef EXT4_BLOCK_GROUP_H_
43 #define EXT4_BLOCK_GROUP_H_
44
45
46 #include <ext4_config.h>
47 #include <ext4_types.h>
48 #include <ext4_super.h>
49
50 #include <stdint.h>
51 #include <stdbool.h>
52
53 static inline uint64_t ext4_bg_get_block_bitmap(struct ext4_bgroup *bg,
54     struct ext4_sblock *s)
55 {
56     uint64_t v = to_le32(bg->block_bitmap_lo);
57
58     if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
59         v |= (uint64_t) to_le32(bg->block_bitmap_hi) << 32;
60
61     return v;
62 }
63
64 static inline uint64_t ext4_bg_get_inode_bitmap(struct ext4_bgroup *bg,
65     struct ext4_sblock *s)
66 {
67
68     uint64_t v = to_le32(bg->inode_bitmap_lo);
69
70     if (ext4_sb_get_desc_size(s)> EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
71         v |= (uint64_t) to_le32(bg->inode_bitmap_hi) << 32;
72
73     return v;
74 }
75
76 static inline uint64_t ext4_bg_get_inode_table_first_block(
77     struct ext4_bgroup *bg, struct ext4_sblock *s)
78 {
79     uint64_t v = to_le32(bg->inode_table_first_block_lo);
80
81     if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
82         v |= (uint64_t) to_le32(bg->inode_table_first_block_hi) << 32;
83
84     return v;
85 }
86
87 static inline uint32_t ext4_bg_get_free_blocks_count(struct ext4_bgroup *bg,
88     struct ext4_sblock *s)
89 {
90     uint32_t v = to_le16(bg->free_blocks_count_lo);
91
92     if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
93         v |= (uint32_t) to_le16(bg->free_blocks_count_hi) << 16;
94
95     return v;
96 }
97
98 static inline void ext4_bg_set_free_blocks_count(struct ext4_bgroup *bg,
99     struct ext4_sblock *s, uint32_t cnt)
100 {
101     bg->free_blocks_count_lo = to_le16((cnt << 16) >> 16);
102     if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
103         bg->free_blocks_count_hi = to_le16(cnt >> 16);
104 }
105
106 static inline uint32_t ext4_bg_get_free_inodes_count(struct ext4_bgroup *bg,
107     struct ext4_sblock *s)
108 {
109     uint32_t v = to_le16(bg->free_inodes_count_lo);
110
111     if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
112         v |= (uint32_t) to_le16(bg->free_inodes_count_hi) << 16;
113
114     return v;
115 }
116
117 static inline void ext4_bg_set_free_inodes_count(struct ext4_bgroup *bg,
118     struct ext4_sblock *s, uint32_t cnt)
119 {
120     bg->free_inodes_count_lo = to_le16((cnt << 16) >> 16);
121     if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
122         bg->free_inodes_count_hi = to_le16(cnt >> 16);
123 }
124
125
126 static inline uint32_t ext4_bg_get_used_dirs_count(struct ext4_bgroup *bg,
127     struct ext4_sblock *s)
128 {
129     uint32_t v = to_le16(bg->used_dirs_count_lo);
130
131     if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
132         v |= (uint32_t) to_le16(bg->used_dirs_count_hi) << 16;
133
134     return v;
135 }
136
137 static inline void ext4_bg_set_used_dirs_count(struct ext4_bgroup *bg,
138     struct ext4_sblock *s, uint32_t cnt)
139 {
140     bg->used_dirs_count_lo = to_le16((cnt << 16) >> 16);
141     if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
142         bg->used_dirs_count_hi = to_le16(cnt >> 16);
143 }
144
145
146 static inline uint32_t ext4_bg_get_itable_unused(struct ext4_bgroup *bg,
147     struct ext4_sblock *s)
148 {
149
150     uint32_t v = to_le16(bg->itable_unused_lo);
151
152     if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
153         v |= (uint32_t) to_le16(bg->itable_unused_hi) << 16;
154
155     return v;
156 }
157
158 static inline void ext4_bg_set_itable_unused(struct ext4_bgroup *bg,
159     struct ext4_sblock *s, uint32_t cnt)
160 {
161     bg->itable_unused_lo = to_le16((cnt << 16) >> 16);
162     if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
163         bg->itable_unused_hi = to_le16(cnt >> 16);
164 }
165
166
167 static inline void ext4_bg_set_checksum(struct ext4_bgroup *bg,
168     uint16_t crc)
169 {
170     bg->checksum = to_le16(crc);
171 }
172
173 static inline bool ext4_bg_has_flag(struct ext4_bgroup *bg, uint32_t f)
174 {
175     return to_le16(bg->flags) & f;
176 }
177
178 static inline void ext4_bg_set_flag(struct ext4_bgroup *bg, uint32_t f)
179 {
180     uint16_t flags = to_le16(bg->flags);
181     flags |= f;
182     bg->flags = to_le16(flags);
183 }
184
185 static inline void ext4_bg_clear_flag(struct ext4_bgroup *bg, uint32_t f)
186 {
187     uint16_t flags = to_le16(bg->flags);
188     flags &= ~f;
189     bg->flags = to_le16(flags);
190 }
191
192
193 uint16_t ext4_bg_crc16(uint16_t crc, const uint8_t *buffer, size_t len);
194
195 #endif /* EXT4_BLOCK_GROUP_H_ */
196
197 /**
198  * @}
199  */