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