Update build system.
[lwext4.git] / src / lwext4 / ext4_super.c
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_super.h
39  * @brief Superblock operations.
40  */
41
42 #include <ext4_config.h>
43 #include <ext4_super.h>
44
45
46 uint32_t ext4_block_group_cnt(struct ext4_sblock *s)
47 {
48     uint64_t blocks_count = ext4_sb_get_blocks_cnt(s);
49     uint32_t blocks_per_group = ext4_get32(s, blocks_per_group);
50
51     uint32_t block_groups_count = blocks_count / blocks_per_group;
52
53     if (blocks_count % blocks_per_group)
54         block_groups_count++;
55
56     return block_groups_count;
57 }
58
59 uint32_t ext4_blocks_in_group_cnt(struct ext4_sblock *s, uint32_t bgid)
60 {
61     uint32_t block_group_count = ext4_block_group_cnt(s);
62     uint32_t blocks_per_group  = ext4_get32(s, blocks_per_group);
63     uint64_t total_blocks          = ext4_sb_get_blocks_cnt(s);
64
65     if (bgid < block_group_count - 1)
66         return blocks_per_group;
67
68
69     return (total_blocks - ((block_group_count - 1) * blocks_per_group));
70 }
71
72 uint32_t ext4_inodes_in_group_cnt(struct ext4_sblock *s, uint32_t bgid)
73 {
74     uint32_t block_group_count = ext4_block_group_cnt(s);
75     uint32_t inodes_per_group  = ext4_get32(s, inodes_per_group);
76     uint32_t total_inodes =              ext4_get32(s, inodes_count);
77
78
79     if (bgid < block_group_count - 1)
80         return inodes_per_group;
81
82     return (total_inodes - ((block_group_count - 1) * inodes_per_group));
83 }
84
85 int     ext4_sb_write(struct ext4_blockdev *bdev, struct ext4_sblock *s)
86 {
87     return ext4_block_writebytes(bdev, EXT4_SUPERBLOCK_OFFSET,
88             s, EXT4_SUPERBLOCK_SIZE);
89 }
90
91 int     ext4_sb_read(struct ext4_blockdev *bdev, struct ext4_sblock *s)
92 {
93     return ext4_block_readbytes(bdev, EXT4_SUPERBLOCK_OFFSET,
94             s, EXT4_SUPERBLOCK_SIZE);
95 }
96
97 bool ext4_sb_check(struct ext4_sblock *s)
98 {
99     if (ext4_get16(s, magic) != EXT4_SUPERBLOCK_MAGIC)
100         return false;
101
102     if (ext4_get32(s, inodes_count) == 0)
103         return false;
104
105     if (ext4_sb_get_blocks_cnt(s) == 0)
106         return false;
107
108     if (ext4_get32(s, blocks_per_group) == 0)
109         return false;
110
111     if (ext4_get32(s, inodes_per_group) == 0)
112         return false;
113
114     if (ext4_get16(s, inode_size) < 128)
115         return false;
116
117     if (ext4_get32(s, first_inode) < 11)
118         return false;
119
120     if (ext4_sb_get_desc_size(s) <
121             EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
122         return false;
123
124     if (ext4_sb_get_desc_size(s)  >
125     EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
126         return false;
127
128     return true;
129 }
130
131 /**
132  * @}
133  */