0035deddfc8e968bf6bf9a117c65ac21e672a590
[lwext4.git] / lwext4 / ext4_super.h
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.c
39  * @brief Superblock operations.
40  */
41
42 #ifndef EXT4_SUPER_H_
43 #define EXT4_SUPER_H_
44
45
46 #include <ext4_config.h>
47 #include <ext4_types.h>
48
49
50 /****************************Unstandard access to superblock*****************/
51
52 /**@brief   Blocks count get stored in superblock.
53  * @param   s superblock descriptor
54  * @return  count of blocks*/
55 static inline uint64_t ext4_sb_get_blocks_cnt(struct ext4_sblock *s)
56 {
57     return ((uint64_t) to_le32(s->blocks_count_hi) << 32) |
58             to_le32(s->blocks_count_lo);
59 }
60
61 /**@brief   Free blocks count get stored in superblock.
62  * @param   s superblock descriptor
63  * @return  free blocks*/
64 static inline uint64_t ext4_sb_get_free_blocks_cnt(struct ext4_sblock *s)
65 {
66     return ((uint64_t) to_le32(s->free_blocks_count_hi) << 32) |
67             to_le32(s->free_blocks_count_lo);
68 }
69
70 /**@brief   Free blocks count set.
71  * @param   s superblock descriptor
72  * @param   cnt new value of free blocks*/
73 static inline void ext4_sb_set_free_blocks_cnt(struct ext4_sblock *s,
74     uint64_t cnt)
75 {
76     s->free_blocks_count_lo = to_le32((cnt << 32) >> 32);
77     s->free_blocks_count_hi = to_le32(cnt >> 32);
78 }
79
80 /**@brief   Block size get from superblock.
81  * @param   s superblock descriptor
82  * @return  block size in bytes*/
83 static inline uint32_t ext4_sb_get_block_size(struct ext4_sblock *s)
84 {
85     return 1024 << to_le32(s->log_block_size);
86 }
87
88 /**@brief   Block group descriptor size.
89  * @param   s superblock descriptor
90  * @return  block group descriptor size in bytes*/
91 static inline uint16_t ext4_sb_get_desc_size(struct ext4_sblock *s)
92 {
93     uint16_t size = to_le16(s->desc_size);
94
95     return size < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE ?
96             EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE : size;
97 }
98
99 /*************************Flags and features*********************************/
100
101 /**@brief   Support check of flag.
102  * @param   s superblock descriptor
103  * @param   v flag to check
104  * @return  true if flag is supported*/
105 static inline bool ext4_sb_check_flag(struct ext4_sblock *s, uint32_t v)
106 {
107     return to_le32(s->flags) & v;
108 }
109
110 /**@brief   Support check of feature compatible.
111  * @param   s superblock descriptor
112  * @param   v feature to check
113  * @return  true if feature is supported*/
114 static inline bool ext4_sb_check_feature_compatible(struct ext4_sblock *s,
115     uint32_t v)
116 {
117     return to_le32(s->features_compatible) & v;
118 }
119
120
121 /**@brief   Support check of feature incompatible.
122  * @param   s superblock descriptor
123  * @param   v feature to check
124  * @return  true if feature is supported*/
125 static inline bool ext4_sb_check_feature_incompatible(struct ext4_sblock *s,
126     uint32_t v)
127 {
128     return to_le32(s->features_incompatible) & v;
129 }
130
131
132 /**@brief   Support check of read only flag.
133  * @param   s superblock descriptor
134  * @param   v flag to check
135  * @return  true if flag is supported*/
136 static inline bool ext4_sb_check_read_only(struct ext4_sblock *s, uint32_t v)
137 {
138     return to_le32(s->features_read_only) & v;
139 }
140
141 /**************************More complex functions****************************/
142
143 /**@brief   Returns a block group count.
144  * @param   s superblock descriptor
145  * @return  count of block groups*/
146 uint32_t ext4_block_group_cnt(struct ext4_sblock *s);
147
148 /**@brief   Returns block count in block group
149  *          (last block group may have less blocks)
150  * @param   s superblock descriptor
151  * @param   bgid block group id
152  * @return  blocks count*/
153 uint32_t ext4_blocks_in_group_cnt(struct ext4_sblock *s, uint32_t bgid);
154
155 /**@brief   Returns inodes count in block group
156  *          (last block group may have less inodes)
157  * @param   s superblock descriptor
158  * @param   bgid block group id
159  * @return  inodes count*/
160 uint32_t ext4_inodes_in_group_cnt(struct ext4_sblock *s, uint32_t bgid);
161
162 /***************************Read/write/check superblock**********************/
163
164 /**@brief   Superblock write.
165  * @param   bdev block device descriptor.
166  * @param   s superblock descruptor
167  * @return  Standard error code */
168 int     ext4_sb_write(struct ext4_blockdev *bdev, struct ext4_sblock *s);
169
170 /**@brief   Superblock read.
171  * @param   bdev block device descriptor.
172  * @param   s superblock descruptor
173  * @return  Standard error code */
174 int     ext4_sb_read(struct ext4_blockdev *bdev, struct ext4_sblock *s);
175
176 /**@brief   Superblock simple validation.
177  * @param   s superblock dsecriptor
178  * @return  true if OK*/
179 bool ext4_sb_check(struct ext4_sblock *s);
180
181
182 #endif /* EXT4_SUPER_H_ */
183
184 /**
185  * @}
186  */
187