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, struct ext4_sblock *s)
55 uint64_t v = to_le32(bg->block_bitmap_lo);
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;
63 static inline uint64_t ext4_bg_get_inode_bitmap(struct ext4_bgroup *bg, struct ext4_sblock *s)
66 uint64_t v = to_le32(bg->inode_bitmap_lo);
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;
74 static inline uint64_t ext4_bg_get_inode_table_first_block(struct ext4_bgroup *bg, struct ext4_sblock *s)
76 uint64_t v = to_le32(bg->inode_table_first_block_lo);
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;
84 static inline uint32_t ext4_bg_get_free_blocks_count(struct ext4_bgroup *bg, struct ext4_sblock *s)
86 uint32_t v = to_le16(bg->free_blocks_count_lo);
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;
94 static inline void ext4_bg_set_free_blocks_count(struct ext4_bgroup *bg, struct ext4_sblock *s, uint32_t cnt)
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);
101 static inline uint32_t ext4_bg_get_free_inodes_count(struct ext4_bgroup *bg, struct ext4_sblock *s)
103 uint32_t v = to_le16(bg->free_inodes_count_lo);
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;
111 static inline void ext4_bg_set_free_inodes_count(struct ext4_bgroup *bg, struct ext4_sblock *s, uint32_t cnt)
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);
119 static inline uint32_t ext4_bg_get_used_dirs_count(struct ext4_bgroup *bg, struct ext4_sblock *s)
121 uint32_t v = to_le16(bg->used_dirs_count_lo);
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;
129 static inline void ext4_bg_set_used_dirs_count(struct ext4_bgroup *bg, struct ext4_sblock *s, uint32_t cnt)
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);
137 static inline uint32_t ext4_bg_get_itable_unused(struct ext4_bgroup *bg, struct ext4_sblock *s)
140 uint32_t v = to_le16(bg->itable_unused_lo);
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;
148 static inline void ext4_bg_set_itable_unused(struct ext4_bgroup *bg, struct ext4_sblock *s, uint32_t cnt)
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);
156 static inline void ext4_bg_set_checksum(struct ext4_bgroup *bg, uint16_t crc)
158 bg->checksum = to_le16(crc);
161 static inline bool ext4_bg_has_flag(struct ext4_bgroup *bg, uint32_t f)
163 return to_le16(bg->flags) & f;
166 static inline void ext4_bg_set_flag(struct ext4_bgroup *bg, uint32_t f)
168 uint16_t flags = to_le16(bg->flags);
170 bg->flags = to_le16(flags);
173 static inline void ext4_bg_clear_flag(struct ext4_bgroup *bg, uint32_t f)
175 uint16_t flags = to_le16(bg->flags);
177 bg->flags = to_le16(flags);
181 uint16_t ext4_bg_crc16(uint16_t crc, const uint8_t *buffer, size_t len);
183 #endif /* EXT4_BLOCK_GROUP_H_ */