Multiple style fixes in ext4_fs
[lwext4.git] / lwext4 / ext4_extent.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_extent.h
39  * @brief More complex filesystem functions.
40  */
41 #ifndef EXT4_EXTENT_H_
42 #define EXT4_EXTENT_H_
43
44 #include "ext4_config.h"
45 #include "ext4_types.h"
46 #include "ext4_inode.h"
47
48
49 /**@brief Get logical number of the block covered by extent.
50  * @param extent Extent to load number from
51  * @return Logical number of the first block covered by extent */
52 static inline uint32_t ext4_extent_get_first_block(struct ext4_extent *extent)
53 {
54         return to_le32(extent->first_block);
55 }
56
57 /**@brief Set logical number of the first block covered by extent.
58  * @param extent Extent to set number to
59  * @param iblock Logical number of the first block covered by extent */
60 static inline void ext4_extent_set_first_block(struct ext4_extent *extent,
61                 uint32_t iblock)
62 {
63         extent->first_block = to_le32(iblock);
64 }
65
66 /**@brief Get number of blocks covered by extent.
67  * @param extent Extent to load count from
68  * @return Number of blocks covered by extent */
69 static inline uint16_t ext4_extent_get_block_count(struct ext4_extent *extent)
70 {
71         if (EXT4_EXT_IS_UNWRITTEN(extent))
72                 return EXT4_EXT_GET_LEN_UNWRITTEN(extent);
73         else
74                 return EXT4_EXT_GET_LEN(extent);
75 }
76 /**@brief Set number of blocks covered by extent.
77  * @param extent Extent to load count from
78  * @param count  Number of blocks covered by extent
79  * @param unwritten Whether the extent is unwritten or not */
80 static inline void ext4_extent_set_block_count(struct ext4_extent *extent,
81                                                uint16_t count, bool unwritten)
82 {
83         EXT4_EXT_SET_LEN(extent, count);
84         if (unwritten)
85                 EXT4_EXT_SET_UNWRITTEN(extent);
86 }
87
88 /**@brief Get physical number of the first block covered by extent.
89  * @param extent Extent to load number
90  * @return Physical number of the first block covered by extent */
91 static inline uint64_t ext4_extent_get_start(struct ext4_extent *extent)
92 {
93         return ((uint64_t)to_le16(extent->start_hi)) << 32 |
94                ((uint64_t)to_le32(extent->start_lo));
95 }
96
97
98 /**@brief Set physical number of the first block covered by extent.
99  * @param extent Extent to load number
100  * @param fblock Physical number of the first block covered by extent */
101 static inline void ext4_extent_set_start(struct ext4_extent *extent, uint64_t fblock)
102 {
103         extent->start_lo = to_le32((fblock << 32) >> 32);
104         extent->start_hi = to_le16((uint16_t)(fblock >> 32));
105 }
106
107
108 /**@brief Get logical number of the block covered by extent index.
109  * @param index Extent index to load number from
110  * @return Logical number of the first block covered by extent index */
111 static inline uint32_t
112 ext4_extent_index_get_first_block(struct ext4_extent_index *index)
113 {
114         return to_le32(index->first_block);
115 }
116
117 /**@brief Set logical number of the block covered by extent index.
118  * @param index  Extent index to set number to
119  * @param iblock Logical number of the first block covered by extent index */
120 static inline void
121 ext4_extent_index_set_first_block(struct ext4_extent_index *index,
122                                   uint32_t iblock)
123 {
124         index->first_block = to_le32(iblock);
125 }
126
127 /**@brief Get physical number of block where the child node is located.
128  * @param index Extent index to load number from
129  * @return Physical number of the block with child node */
130 static inline uint64_t
131 ext4_extent_index_get_leaf(struct ext4_extent_index *index)
132 {
133         return ((uint64_t)to_le16(index->leaf_hi)) << 32 |
134                ((uint64_t)to_le32(index->leaf_lo));
135 }
136
137 /**@brief Set physical number of block where the child node is located.
138  * @param index  Extent index to set number to
139  * @param fblock Ohysical number of the block with child node */
140 static inline void ext4_extent_index_set_leaf(struct ext4_extent_index *index,
141                                               uint64_t fblock)
142 {
143         index->leaf_lo = to_le32((fblock << 32) >> 32);
144         index->leaf_hi = to_le16((uint16_t)(fblock >> 32));
145 }
146
147 /**@brief Get magic value from extent header.
148  * @param header Extent header to load value from
149  * @return Magic value of extent header */
150 static inline uint16_t
151 ext4_extent_header_get_magic(struct ext4_extent_header *header)
152 {
153         return to_le16(header->magic);
154 }
155
156 /**@brief Set magic value to extent header.
157  * @param header Extent header to set value to
158  * @param magic  Magic value of extent header */
159 static inline void ext4_extent_header_set_magic(struct ext4_extent_header *header,
160                                                 uint16_t magic)
161 {
162         header->magic = to_le16(magic);
163 }
164
165 /**@brief Get number of entries from extent header
166  * @param header Extent header to get value from
167  * @return Number of entries covered by extent header */
168 static inline uint16_t
169 ext4_extent_header_get_entries_count(struct ext4_extent_header *header)
170 {
171         return to_le16(header->entries_count);
172 }
173
174 /**@brief Set number of entries to extent header
175  * @param header Extent header to set value to
176  * @param count  Number of entries covered by extent header */
177 static inline void
178 ext4_extent_header_set_entries_count(struct ext4_extent_header *header,
179                                      uint16_t count)
180 {
181         header->entries_count = to_le16(count);
182 }
183
184 /**@brief Get maximum number of entries from extent header
185  * @param header Extent header to get value from
186  * @return Maximum number of entries covered by extent header */
187 static inline uint16_t
188 ext4_extent_header_get_max_entries_count(struct ext4_extent_header *header)
189 {
190         return to_le16(header->max_entries_count);
191 }
192
193 /**@brief Set maximum number of entries to extent header
194  * @param header    Extent header to set value to
195  * @param max_count Maximum number of entries covered by extent header */
196 static inline void
197 ext4_extent_header_set_max_entries_count(struct ext4_extent_header *header,
198                                               uint16_t max_count)
199 {
200         header->max_entries_count = to_le16(max_count);
201 }
202
203 /**@brief Get depth of extent subtree.
204  * @param header Extent header to get value from
205  * @return Depth of extent subtree */
206 static inline uint16_t
207 ext4_extent_header_get_depth(struct ext4_extent_header *header)
208 {
209         return to_le16(header->depth);
210 }
211
212 /**@brief Set depth of extent subtree.
213  * @param header Extent header to set value to
214  * @param depth  Depth of extent subtree */
215 static inline void
216 ext4_extent_header_set_depth(struct ext4_extent_header *header, uint16_t depth)
217 {
218         header->depth = to_le16(depth);
219 }
220
221 /**@brief Get generation from extent header
222  * @param header Extent header to get value from
223  * @return Generation */
224 static inline uint32_t
225 ext4_extent_header_get_generation(struct ext4_extent_header *header)
226 {
227         return to_le32(header->generation);
228 }
229
230 /**@brief Set generation to extent header
231  * @param header     Extent header to set value to
232  * @param generation Generation */
233 static inline void
234 ext4_extent_header_set_generation(struct ext4_extent_header *header,
235                                        uint32_t generation)
236 {
237         header->generation = to_le32(generation);
238 }
239
240 /******************************************************************************/
241
242 /**TODO:  */
243 static inline void ext4_extent_tree_init(struct ext4_inode_ref *inode_ref)
244 {
245         /* Initialize extent root header */
246         struct ext4_extent_header *header =
247                         ext4_inode_get_extent_header(inode_ref->inode);
248         ext4_extent_header_set_depth(header, 0);
249         ext4_extent_header_set_entries_count(header, 0);
250         ext4_extent_header_set_generation(header, 0);
251         ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
252
253         uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
254                         sizeof(struct ext4_extent_header)) /
255                                         sizeof(struct ext4_extent);
256
257         ext4_extent_header_set_max_entries_count(header, max_entries);
258         inode_ref->dirty  = true;
259 }
260
261
262
263 /**TODO:  */
264 int ext4_extent_get_blocks(struct ext4_inode_ref *inode_ref, ext4_fsblk_t iblock,
265                            uint32_t max_blocks, ext4_fsblk_t *result, bool create,
266                            uint32_t *blocks_count);
267
268
269 /**@brief Release all data blocks starting from specified logical block.
270  * @param inode_ref   I-node to release blocks from
271  * @param iblock_from First logical block to release
272  * @return Error code */
273 int ext4_extent_remove_space(struct ext4_inode_ref *inode_ref, ext4_lblk_t from,
274                              ext4_lblk_t to);
275
276
277 #endif /* EXT4_EXTENT_H_ */
278 /**
279 * @}
280 */