source: mainline/uspace/lib/ext4/src/block_group.c@ 3061bc1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3061bc1 was 4bfad34, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Move parts of ext4 filesystem initialization.

  • Property mode set to 100644
File size: 10.9 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Sucha
3 * Copyright (c) 2012 Frantisek Princ
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libext4
31 * @{
32 */
33/**
34 * @file block_group.c
35 * @brief Ext4 block group structure operations.
36 */
37
38#include <byteorder.h>
39#include "ext4/block_group.h"
40#include "ext4/superblock.h"
41
42/** Get address of block with data block bitmap.
43 *
44 * @param bg Pointer to block group
45 * @param sb Pointer to superblock
46 *
47 * @return Address of block with block bitmap
48 *
49 */
50uint64_t ext4_block_group_get_block_bitmap(ext4_block_group_t *bg,
51 ext4_superblock_t *sb)
52{
53 if (ext4_superblock_get_desc_size(sb) >
54 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
55 return ((uint64_t) uint32_t_le2host(bg->block_bitmap_hi) << 32) |
56 uint32_t_le2host(bg->block_bitmap_lo);
57 else
58 return uint32_t_le2host(bg->block_bitmap_lo);
59}
60
61/** Set address of block with data block bitmap.
62 *
63 * @param bg Pointer to block group
64 * @param sb Pointer to superblock
65 * @param block_bitmap Address of block with block bitmap
66 *
67 */
68void ext4_block_group_set_block_bitmap(ext4_block_group_t *bg,
69 ext4_superblock_t *sb, uint64_t block_bitmap)
70{
71 bg->block_bitmap_lo = host2uint32_t_le((block_bitmap << 32) >> 32);
72
73 if (ext4_superblock_get_desc_size(sb) >
74 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
75 bg->block_bitmap_hi = host2uint32_t_le(block_bitmap >> 32);
76}
77
78/** Get address of block with i-node bitmap.
79 *
80 * @param bg Pointer to block group
81 * @param sb Pointer to superblock
82 *
83 * @return Address of block with i-node bitmap
84 *
85 */
86uint64_t ext4_block_group_get_inode_bitmap(ext4_block_group_t *bg,
87 ext4_superblock_t *sb)
88{
89 if (ext4_superblock_get_desc_size(sb) >
90 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
91 return ((uint64_t) uint32_t_le2host(bg->inode_bitmap_hi) << 32) |
92 uint32_t_le2host(bg->inode_bitmap_lo);
93 else
94 return uint32_t_le2host(bg->inode_bitmap_lo);
95}
96
97/** Set address of block with i-node bitmap.
98 *
99 * @param bg Pointer to block group
100 * @param sb Pointer to superblock
101 * @param inode_bitmap Address of block with i-node bitmap
102 *
103 */
104void ext4_block_group_set_inode_bitmap(ext4_block_group_t *bg,
105 ext4_superblock_t *sb, uint64_t inode_bitmap)
106{
107 bg->inode_bitmap_lo = host2uint32_t_le((inode_bitmap << 32) >> 32);
108
109 if (ext4_superblock_get_desc_size(sb) >
110 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
111 bg->inode_bitmap_hi = host2uint32_t_le(inode_bitmap >> 32);
112}
113
114/** Get address of the first block of the i-node table.
115 *
116 * @param bg Pointer to block group
117 * @param sb Pointer to superblock
118 *
119 * @return Address of first block of i-node table
120 *
121 */
122uint64_t ext4_block_group_get_inode_table_first_block(ext4_block_group_t *bg,
123 ext4_superblock_t *sb)
124{
125 if (ext4_superblock_get_desc_size(sb) >
126 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
127 return ((uint64_t)
128 uint32_t_le2host(bg->inode_table_first_block_hi) << 32) |
129 uint32_t_le2host(bg->inode_table_first_block_lo);
130 else
131 return uint32_t_le2host(bg->inode_table_first_block_lo);
132}
133
134/** Set address of the first block of the i-node table.
135 *
136 * @param bg Pointer to block group
137 * @param sb Pointer to superblock
138 * @param inode_table_first Address of first block of i-node table
139 *
140 */
141void ext4_block_group_set_inode_table_first_block(ext4_block_group_t *bg,
142 ext4_superblock_t *sb, uint64_t inode_table_first)
143{
144 bg->inode_table_first_block_lo =
145 host2uint32_t_le((inode_table_first << 32) >> 32);
146
147 if (ext4_superblock_get_desc_size(sb) >
148 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
149 bg->inode_table_first_block_hi =
150 host2uint32_t_le(inode_table_first >> 32);
151}
152
153/** Get number of free blocks in block group.
154 *
155 * @param bg Pointer to block group
156 * @param sb Pointer to superblock
157 *
158 * @return Number of free blocks in block group
159 *
160 */
161uint32_t ext4_block_group_get_free_blocks_count(ext4_block_group_t *bg,
162 ext4_superblock_t *sb)
163{
164 if (ext4_superblock_get_desc_size(sb) >
165 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
166 return ((uint32_t)
167 uint16_t_le2host(bg->free_blocks_count_hi) << 16) |
168 uint16_t_le2host(bg->free_blocks_count_lo);
169 else
170 return uint16_t_le2host(bg->free_blocks_count_lo);
171}
172
173/** Set number of free blocks in block group.
174 *
175 * @param bg Pointer to block group
176 * @param sb Pointer to superblock
177 * @param value Number of free blocks in block group
178 *
179 */
180void ext4_block_group_set_free_blocks_count(ext4_block_group_t *bg,
181 ext4_superblock_t *sb, uint32_t value)
182{
183 bg->free_blocks_count_lo = host2uint16_t_le((value << 16) >> 16);
184 if (ext4_superblock_get_desc_size(sb) >
185 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
186 bg->free_blocks_count_hi = host2uint16_t_le(value >> 16);
187}
188
189/** Get number of free i-nodes in block group.
190 *
191 * @param bg Pointer to block group
192 * @param sb Pointer to superblock
193 *
194 * @return Number of free i-nodes in block group
195 *
196 */
197uint32_t ext4_block_group_get_free_inodes_count(ext4_block_group_t *bg,
198 ext4_superblock_t *sb)
199{
200 if (ext4_superblock_get_desc_size(sb) >
201 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
202 return ((uint32_t)
203 uint16_t_le2host(bg->free_inodes_count_hi) << 16) |
204 uint16_t_le2host(bg->free_inodes_count_lo);
205 else
206 return uint16_t_le2host(bg->free_inodes_count_lo);
207}
208
209/** Set number of free i-nodes in block group.
210 *
211 * @param bg Pointer to block group
212 * @param sb Pointer to superblock
213 * @param value Number of free i-nodes in block group
214 *
215 */
216void ext4_block_group_set_free_inodes_count(ext4_block_group_t *bg,
217 ext4_superblock_t *sb, uint32_t value)
218{
219 bg->free_inodes_count_lo = host2uint16_t_le((value << 16) >> 16);
220 if (ext4_superblock_get_desc_size(sb) >
221 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
222 bg->free_inodes_count_hi = host2uint16_t_le(value >> 16);
223}
224
225/** Get number of used directories in block group.
226 *
227 * @param bg Pointer to block group
228 * @param sb Pointer to superblock
229 *
230 * @return Number of used directories in block group
231 *
232 */
233uint32_t ext4_block_group_get_used_dirs_count(ext4_block_group_t *bg,
234 ext4_superblock_t *sb)
235{
236 if (ext4_superblock_get_desc_size(sb) >
237 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
238 return ((uint32_t)
239 uint16_t_le2host(bg->used_dirs_count_hi) << 16) |
240 uint16_t_le2host(bg->used_dirs_count_lo);
241 else
242 return uint16_t_le2host(bg->used_dirs_count_lo);
243}
244
245/** Set number of used directories in block group.
246 *
247 * @param bg Pointer to block group
248 * @param sb Pointer to superblock
249 * @param value Number of used directories in block group
250 *
251 */
252void ext4_block_group_set_used_dirs_count(ext4_block_group_t *bg,
253 ext4_superblock_t *sb, uint32_t count)
254{
255 bg->used_dirs_count_lo = host2uint16_t_le((count << 16) >> 16);
256 if (ext4_superblock_get_desc_size(sb) >
257 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
258 bg->used_dirs_count_hi = host2uint16_t_le(count >> 16);
259}
260
261/** Get flags of block group.
262 *
263 * @param bg Pointer to block group
264 *
265 * @return Flags of block group
266 *
267 */
268uint16_t ext4_block_group_get_flags(ext4_block_group_t *bg)
269{
270 return uint16_t_le2host(bg->flags);
271}
272
273/** Set flags for block group.
274 *
275 * @param bg Pointer to block group
276 * @param flags Flags for block group
277 *
278 */
279void ext4_block_group_set_flags(ext4_block_group_t *bg, uint16_t flags)
280{
281 bg->flags = host2uint16_t_le(flags);
282}
283
284/** Get number of unused i-nodes.
285 *
286 * @param bg Pointer to block group
287 * @param sb Pointer to superblock
288 *
289 * @return Number of unused i-nodes
290 *
291 */
292uint32_t ext4_block_group_get_itable_unused(ext4_block_group_t *bg,
293 ext4_superblock_t *sb)
294{
295 if (ext4_superblock_get_desc_size(sb) >
296 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
297 return ((uint32_t)
298 uint16_t_le2host(bg->itable_unused_hi) << 16) |
299 uint16_t_le2host(bg->itable_unused_lo);
300 else
301 return uint16_t_le2host(bg->itable_unused_lo);
302}
303
304/** Set number of unused i-nodes.
305 *
306 * @param bg Pointer to block group
307 * @param sb Pointer to superblock
308 * @param value Number of unused i-nodes
309 *
310 */
311void ext4_block_group_set_itable_unused(ext4_block_group_t *bg,
312 ext4_superblock_t *sb, uint32_t value)
313{
314 bg->itable_unused_lo = host2uint16_t_le((value << 16) >> 16);
315 if (ext4_superblock_get_desc_size(sb) >
316 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
317 bg->itable_unused_hi = host2uint16_t_le(value >> 16);
318}
319
320/** Get checksum of block group.
321 *
322 * @param bg Pointer to block group
323 *
324 * @return checksum of block group
325 *
326 */
327uint16_t ext4_block_group_get_checksum(ext4_block_group_t *bg)
328{
329 return uint16_t_le2host(bg->checksum);
330}
331
332/** Set checksum of block group.
333 *
334 * @param bg Pointer to block group
335 * @param checksum Cheksum of block group
336 *
337 */
338void ext4_block_group_set_checksum(ext4_block_group_t *bg, uint16_t checksum)
339{
340 bg->checksum = host2uint16_t_le(checksum);
341}
342
343/** Check if block group has a flag.
344 *
345 * @param bg Pointer to block group
346 * @param flag Flag to be checked
347 *
348 * @return True if flag is set to 1
349 *
350 */
351bool ext4_block_group_has_flag(ext4_block_group_t *bg, uint32_t flag)
352{
353 if (ext4_block_group_get_flags(bg) & flag)
354 return true;
355
356 return false;
357}
358
359/** Set (add) flag of block group.
360 *
361 * @param bg Pointer to block group
362 * @param flag Flag to be set
363 *
364 */
365void ext4_block_group_set_flag(ext4_block_group_t *bg, uint32_t set_flag)
366{
367 uint32_t flags = ext4_block_group_get_flags(bg);
368 flags = flags | set_flag;
369 ext4_block_group_set_flags(bg, flags);
370}
371
372/** Clear (remove) flag of block group.
373 *
374 * @param bg Pointer to block group
375 * @param flag Flag to be cleared
376 *
377 */
378void ext4_block_group_clear_flag(ext4_block_group_t *bg, uint32_t clear_flag)
379{
380 uint32_t flags = ext4_block_group_get_flags(bg);
381 flags = flags & (~clear_flag);
382 ext4_block_group_set_flags(bg, flags);
383}
384
385/**
386 * @}
387 */
Note: See TracBrowser for help on using the repository browser.