1 | /*
|
---|
2 | * Copyright (c) 2011 Frantisek Princ
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libext4
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file libext4_block_group.c
|
---|
35 | * @brief Ext4 block group structure operations
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <byteorder.h>
|
---|
39 | #include "libext4.h"
|
---|
40 |
|
---|
41 | uint64_t ext4_block_group_get_block_bitmap(ext4_block_group_t *bg,
|
---|
42 | ext4_superblock_t *sb)
|
---|
43 | {
|
---|
44 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
45 | return ((uint64_t)uint32_t_le2host(bg->block_bitmap_hi) << 32) |
|
---|
46 | uint32_t_le2host(bg->block_bitmap_lo);
|
---|
47 | } else {
|
---|
48 | return uint32_t_le2host(bg->block_bitmap_lo);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | void ext4_block_group_set_block_bitmap(ext4_block_group_t *bg,
|
---|
53 | ext4_superblock_t *sb, uint64_t block_bitmap)
|
---|
54 | {
|
---|
55 | bg->block_bitmap_lo = host2uint32_t_le((block_bitmap << 32) >> 32);
|
---|
56 |
|
---|
57 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
58 | bg->block_bitmap_hi = host2uint32_t_le(block_bitmap >> 32);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | uint64_t ext4_block_group_get_inode_bitmap(ext4_block_group_t *bg,
|
---|
63 | ext4_superblock_t *sb)
|
---|
64 | {
|
---|
65 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
66 | return ((uint64_t)uint32_t_le2host(bg->inode_bitmap_hi) << 32) |
|
---|
67 | uint32_t_le2host(bg->inode_bitmap_lo);
|
---|
68 | } else {
|
---|
69 | return uint32_t_le2host(bg->inode_bitmap_lo);
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | void ext4_block_group_set_inode_bitmap(ext4_block_group_t *bg,
|
---|
75 | ext4_superblock_t *sb, uint64_t inode_bitmap)
|
---|
76 | {
|
---|
77 | bg->inode_bitmap_lo = host2uint32_t_le((inode_bitmap << 32) >> 32);
|
---|
78 |
|
---|
79 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
80 | bg->inode_bitmap_hi = host2uint32_t_le(inode_bitmap >> 32);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | uint64_t ext4_block_group_get_inode_table_first_block(ext4_block_group_t *bg,
|
---|
85 | ext4_superblock_t *sb)
|
---|
86 | {
|
---|
87 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
88 | return ((uint64_t)uint32_t_le2host(bg->inode_table_first_block_hi) << 32) |
|
---|
89 | uint32_t_le2host(bg->inode_table_first_block_lo);
|
---|
90 | } else {
|
---|
91 | return uint32_t_le2host(bg->inode_table_first_block_lo);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | void ext4_block_group_set_inode_table_first_block(ext4_block_group_t *bg,
|
---|
96 | ext4_superblock_t *sb, uint64_t ino_tbl_first)
|
---|
97 | {
|
---|
98 | bg->inode_table_first_block_lo = host2uint32_t_le((ino_tbl_first << 32) >> 32);
|
---|
99 |
|
---|
100 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
101 | bg->inode_table_first_block_hi = host2uint32_t_le(ino_tbl_first >> 32);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | uint32_t ext4_block_group_get_free_blocks_count(ext4_block_group_t *bg,
|
---|
106 | ext4_superblock_t *sb)
|
---|
107 | {
|
---|
108 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
109 | return ((uint32_t)uint16_t_le2host(bg->free_blocks_count_hi) << 16) |
|
---|
110 | uint16_t_le2host(bg->free_blocks_count_lo);
|
---|
111 | } else {
|
---|
112 | return uint16_t_le2host(bg->free_blocks_count_lo);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | void ext4_block_group_set_free_blocks_count(ext4_block_group_t *bg,
|
---|
117 | ext4_superblock_t *sb, uint32_t value)
|
---|
118 | {
|
---|
119 | bg->free_blocks_count_lo = host2uint16_t_le((value << 16) >> 16);
|
---|
120 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
121 | bg->free_blocks_count_hi = host2uint16_t_le(value >> 16);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | uint32_t ext4_block_group_get_free_inodes_count(ext4_block_group_t *bg,
|
---|
126 | ext4_superblock_t *sb)
|
---|
127 | {
|
---|
128 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
129 | return ((uint32_t)uint16_t_le2host(bg->free_inodes_count_hi) << 16) |
|
---|
130 | uint16_t_le2host(bg->free_inodes_count_lo);
|
---|
131 | } else {
|
---|
132 | return uint16_t_le2host(bg->free_inodes_count_lo);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | void ext4_block_group_set_free_inodes_count(ext4_block_group_t *bg,
|
---|
137 | ext4_superblock_t *sb, uint32_t value)
|
---|
138 | {
|
---|
139 | bg->free_inodes_count_lo = host2uint16_t_le((value << 16) >> 16);
|
---|
140 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
141 | bg->free_inodes_count_hi = host2uint16_t_le(value >> 16);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | uint32_t ext4_block_group_get_used_dirs_count(ext4_block_group_t *bg,
|
---|
147 | ext4_superblock_t *sb)
|
---|
148 | {
|
---|
149 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
150 | return ((uint32_t)uint16_t_le2host(bg->used_dirs_count_hi) << 16) |
|
---|
151 | uint16_t_le2host(bg->used_dirs_count_lo);
|
---|
152 | } else {
|
---|
153 | return uint16_t_le2host(bg->used_dirs_count_lo);
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | void ext4_block_group_set_used_dirs_count(ext4_block_group_t *bg,
|
---|
158 | ext4_superblock_t *sb, uint32_t count)
|
---|
159 | {
|
---|
160 | bg->used_dirs_count_lo = host2uint16_t_le((count << 16) >> 16);
|
---|
161 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
162 | bg->used_dirs_count_hi = host2uint16_t_le(count >> 16);
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | uint16_t ext4_block_group_get_flags(ext4_block_group_t *bg)
|
---|
167 | {
|
---|
168 | return uint16_t_le2host(bg->flags);
|
---|
169 | }
|
---|
170 |
|
---|
171 | void ext4_block_group_set_flags(ext4_block_group_t *bg, uint16_t flags)
|
---|
172 | {
|
---|
173 | bg->flags = host2uint16_t_le(flags);
|
---|
174 | }
|
---|
175 |
|
---|
176 | uint32_t ext4_block_group_get_itable_unused(ext4_block_group_t *bg,
|
---|
177 | ext4_superblock_t *sb)
|
---|
178 | {
|
---|
179 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
180 | return ((uint32_t)uint16_t_le2host(bg->itable_unused_hi) << 16) |
|
---|
181 | uint16_t_le2host(bg->itable_unused_lo);
|
---|
182 | } else {
|
---|
183 | return uint16_t_le2host(bg->itable_unused_lo);
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | void ext4_block_group_set_itable_unused(ext4_block_group_t *bg,
|
---|
188 | ext4_superblock_t *sb, uint32_t value)
|
---|
189 | {
|
---|
190 | bg->itable_unused_lo = host2uint16_t_le((value << 16) >> 16);
|
---|
191 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
192 | bg->itable_unused_hi = host2uint16_t_le(value >> 16);
|
---|
193 | }
|
---|
194 |
|
---|
195 | }
|
---|
196 |
|
---|
197 | uint16_t ext4_block_group_get_checksum(ext4_block_group_t *bg)
|
---|
198 | {
|
---|
199 | return uint16_t_le2host(bg->checksum);
|
---|
200 | }
|
---|
201 |
|
---|
202 | void ext4_block_group_set_checksum(ext4_block_group_t *bg, uint16_t checksum)
|
---|
203 | {
|
---|
204 | bg->checksum = host2uint16_t_le(checksum);
|
---|
205 | }
|
---|
206 |
|
---|
207 | // Flags operations
|
---|
208 | bool ext4_block_group_has_flag(ext4_block_group_t *bg, uint32_t flag)
|
---|
209 | {
|
---|
210 | if (ext4_block_group_get_flags(bg) & flag) {
|
---|
211 | return true;
|
---|
212 | }
|
---|
213 | return false;
|
---|
214 | }
|
---|
215 |
|
---|
216 | void ext4_block_group_clear_flag(ext4_block_group_t *bg, uint32_t clear_flag)
|
---|
217 | {
|
---|
218 | uint32_t flags = ext4_block_group_get_flags(bg);
|
---|
219 | flags = flags & (~clear_flag);
|
---|
220 | ext4_block_group_set_flags(bg, flags);
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * @}
|
---|
226 | */
|
---|