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