2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
6 * Copyright (c) 2012 Martin Sucha
7 * Copyright (c) 2012 Frantisek Princ
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
34 /** @addtogroup lwext4
38 * @file ext4_block_group.h
39 * @brief Block group function set.
42 #ifndef EXT4_BLOCK_GROUP_H_
43 #define EXT4_BLOCK_GROUP_H_
46 #include <ext4_config.h>
47 #include <ext4_types.h>
48 #include <ext4_super.h>
53 static inline uint64_t ext4_bg_get_block_bitmap(struct ext4_bgroup *bg,
54 struct ext4_sblock *s)
56 uint64_t v = to_le32(bg->block_bitmap_lo);
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;
64 static inline uint64_t ext4_bg_get_inode_bitmap(struct ext4_bgroup *bg,
65 struct ext4_sblock *s)
68 uint64_t v = to_le32(bg->inode_bitmap_lo);
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;
76 static inline uint64_t ext4_bg_get_inode_table_first_block(
77 struct ext4_bgroup *bg, struct ext4_sblock *s)
79 uint64_t v = to_le32(bg->inode_table_first_block_lo);
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;
87 static inline uint32_t ext4_bg_get_free_blocks_count(struct ext4_bgroup *bg,
88 struct ext4_sblock *s)
90 uint32_t v = to_le16(bg->free_blocks_count_lo);
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;
98 static inline void ext4_bg_set_free_blocks_count(struct ext4_bgroup *bg,
99 struct ext4_sblock *s, uint32_t cnt)
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);
106 static inline uint32_t ext4_bg_get_free_inodes_count(struct ext4_bgroup *bg,
107 struct ext4_sblock *s)
109 uint32_t v = to_le16(bg->free_inodes_count_lo);
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;
117 static inline void ext4_bg_set_free_inodes_count(struct ext4_bgroup *bg,
118 struct ext4_sblock *s, uint32_t cnt)
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);
126 static inline uint32_t ext4_bg_get_used_dirs_count(struct ext4_bgroup *bg,
127 struct ext4_sblock *s)
129 uint32_t v = to_le16(bg->used_dirs_count_lo);
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;
137 static inline void ext4_bg_set_used_dirs_count(struct ext4_bgroup *bg,
138 struct ext4_sblock *s, uint32_t cnt)
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);
146 static inline uint32_t ext4_bg_get_itable_unused(struct ext4_bgroup *bg,
147 struct ext4_sblock *s)
150 uint32_t v = to_le16(bg->itable_unused_lo);
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;
158 static inline void ext4_bg_set_itable_unused(struct ext4_bgroup *bg,
159 struct ext4_sblock *s, uint32_t cnt)
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);
167 static inline void ext4_bg_set_checksum(struct ext4_bgroup *bg,
170 bg->checksum = to_le16(crc);
173 static inline bool ext4_bg_has_flag(struct ext4_bgroup *bg, uint32_t f)
175 return to_le16(bg->flags) & f;
178 static inline void ext4_bg_set_flag(struct ext4_bgroup *bg, uint32_t f)
180 uint16_t flags = to_le16(bg->flags);
182 bg->flags = to_le16(flags);
185 static inline void ext4_bg_clear_flag(struct ext4_bgroup *bg, uint32_t f)
187 uint16_t flags = to_le16(bg->flags);
189 bg->flags = to_le16(flags);
193 uint16_t ext4_bg_crc16(uint16_t crc, const uint8_t *buffer, size_t len);
195 #endif /* EXT4_BLOCK_GROUP_H_ */