Introduce initial support of ext3/4 journalling.
[lwext4.git] / lwext4 / ext4_ialloc.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_ialloc.c
39  * @brief Inode allocation procedures.
40  */
41
42 #include "ext4_config.h"
43 #include "ext4_types.h"
44 #include "ext4_ialloc.h"
45 #include "ext4_super.h"
46 #include "ext4_crc32c.h"
47 #include "ext4_fs.h"
48 #include "ext4_blockdev.h"
49 #include "ext4_block_group.h"
50 #include "ext4_bitmap.h"
51
52 /**@brief  Convert i-node number to relative index in block group.
53  * @param sb    Superblock
54  * @param inode I-node number to be converted
55  * @return Index of the i-node in the block group
56  */
57 static uint32_t ext4_ialloc_inode_to_bgidx(struct ext4_sblock *sb,
58                                            uint32_t inode)
59 {
60         uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
61         return (inode - 1) % inodes_per_group;
62 }
63
64 /**@brief Convert relative index of i-node to absolute i-node number.
65  * @param sb    Superblock
66  * @param index Index to be converted
67  * @return Absolute number of the i-node
68  *
69  */
70 static uint32_t ext4_ialloc_bgidx_to_inode(struct ext4_sblock *sb,
71                                            uint32_t index, uint32_t bgid)
72 {
73         uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
74         return bgid * inodes_per_group + (index + 1);
75 }
76
77 /**@brief Compute block group number from the i-node number.
78  * @param sb    Superblock
79  * @param inode I-node number to be found the block group for
80  * @return Block group number computed from i-node number
81  */
82 static uint32_t ext4_ialloc_get_bgid_of_inode(struct ext4_sblock *sb,
83                                               uint32_t inode)
84 {
85         uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
86         return (inode - 1) / inodes_per_group;
87 }
88
89 #if CONFIG_META_CSUM_ENABLE
90 static uint32_t ext4_ialloc_bitmap_csum(struct ext4_sblock *sb, void *bitmap)
91 {
92         uint32_t csum = 0;
93         if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
94                 uint32_t inodes_per_group =
95                         ext4_get32(sb, inodes_per_group);
96
97                 /* First calculate crc32 checksum against fs uuid */
98                 csum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, sizeof(sb->uuid));
99                 /* Then calculate crc32 checksum against inode bitmap */
100                 csum = ext4_crc32c(csum, bitmap, (inodes_per_group + 7) / 8);
101         }
102         return csum;
103 }
104 #else
105 #define ext4_ialloc_bitmap_csum(...) 0
106 #endif
107
108 void ext4_ialloc_set_bitmap_csum(struct ext4_sblock *sb, struct ext4_bgroup *bg,
109                                  void *bitmap __unused)
110 {
111         int desc_size = ext4_sb_get_desc_size(sb);
112         uint32_t csum = ext4_ialloc_bitmap_csum(sb, bitmap);
113         uint16_t lo_csum = to_le16(csum & 0xFFFF),
114                  hi_csum = to_le16(csum >> 16);
115
116         if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
117                 return;
118
119         /* See if we need to assign a 32bit checksum */
120         bg->inode_bitmap_csum_lo = lo_csum;
121         if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
122                 bg->inode_bitmap_csum_hi = hi_csum;
123
124 }
125
126 #if CONFIG_META_CSUM_ENABLE
127 static bool
128 ext4_ialloc_verify_bitmap_csum(struct ext4_sblock *sb, struct ext4_bgroup *bg,
129                                void *bitmap __unused)
130 {
131
132         int desc_size = ext4_sb_get_desc_size(sb);
133         uint32_t csum = ext4_ialloc_bitmap_csum(sb, bitmap);
134         uint16_t lo_csum = to_le16(csum & 0xFFFF),
135                  hi_csum = to_le16(csum >> 16);
136
137         if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
138                 return true;
139
140         if (bg->inode_bitmap_csum_lo != lo_csum)
141                 return false;
142
143         if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
144                 if (bg->inode_bitmap_csum_hi != hi_csum)
145                         return false;
146
147         return true;
148 }
149 #else
150 #define ext4_ialloc_verify_bitmap_csum(...) true
151 #endif
152
153 int ext4_ialloc_free_inode(struct ext4_fs *fs, uint32_t index, bool is_dir)
154 {
155         struct ext4_sblock *sb = &fs->sb;
156
157         /* Compute index of block group and load it */
158         uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
159
160         struct ext4_block_group_ref bg_ref;
161         int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
162         if (rc != EOK)
163                 return rc;
164
165         struct ext4_bgroup *bg = bg_ref.block_group;
166
167         /* Load i-node bitmap */
168         uint32_t bitmap_block_addr =
169             ext4_bg_get_inode_bitmap(bg, sb);
170
171         struct ext4_block b;
172         rc = ext4_trans_block_get(fs->bdev, &b, bitmap_block_addr);
173         if (rc != EOK)
174                 return rc;
175
176         if (!ext4_ialloc_verify_bitmap_csum(sb, bg, b.data)) {
177                 ext4_dbg(DEBUG_IALLOC,
178                         DBG_WARN "Bitmap checksum failed."
179                         "Group: %" PRIu32"\n",
180                         bg_ref.index);
181         }
182
183         /* Free i-node in the bitmap */
184         uint32_t index_in_group = ext4_ialloc_inode_to_bgidx(sb, index);
185         ext4_bmap_bit_clr(b.data, index_in_group);
186         ext4_ialloc_set_bitmap_csum(sb, bg, b.data);
187         ext4_trans_set_block_dirty(b.buf);
188
189         /* Put back the block with bitmap */
190         rc = ext4_block_set(fs->bdev, &b);
191         if (rc != EOK) {
192                 /* Error in saving bitmap */
193                 ext4_fs_put_block_group_ref(&bg_ref);
194                 return rc;
195         }
196
197         /* If released i-node is a directory, decrement used directories count
198          */
199         if (is_dir) {
200                 uint32_t bg_used_dirs = ext4_bg_get_used_dirs_count(bg, sb);
201                 bg_used_dirs--;
202                 ext4_bg_set_used_dirs_count(bg, sb, bg_used_dirs);
203         }
204
205         /* Update block group free inodes count */
206         uint32_t free_inodes = ext4_bg_get_free_inodes_count(bg, sb);
207         free_inodes++;
208         ext4_bg_set_free_inodes_count(bg, sb, free_inodes);
209
210         bg_ref.dirty = true;
211
212         /* Put back the modified block group */
213         rc = ext4_fs_put_block_group_ref(&bg_ref);
214         if (rc != EOK)
215                 return rc;
216
217         /* Update superblock free inodes count */
218         ext4_set32(sb, free_inodes_count,
219                    ext4_get32(sb, free_inodes_count) + 1);
220
221         return EOK;
222 }
223
224 int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *idx, bool is_dir)
225 {
226         struct ext4_sblock *sb = &fs->sb;
227
228         uint32_t bgid = fs->last_inode_bg_id;
229         uint32_t bg_count = ext4_block_group_cnt(sb);
230         uint32_t sb_free_inodes = ext4_get32(sb, free_inodes_count);
231         bool rewind = false;
232
233         /* Try to find free i-node in all block groups */
234         while (bgid <= bg_count) {
235
236                 if (bgid == bg_count) {
237                         if (rewind)
238                                 break;
239                         bg_count = fs->last_inode_bg_id;
240                         bgid = 0;
241                         rewind = true;
242                         continue;
243                 }
244
245                 /* Load block group to check */
246                 struct ext4_block_group_ref bg_ref;
247                 int rc = ext4_fs_get_block_group_ref(fs, bgid, &bg_ref);
248                 if (rc != EOK)
249                         return rc;
250
251                 struct ext4_bgroup *bg = bg_ref.block_group;
252
253                 /* Read necessary values for algorithm */
254                 uint32_t free_inodes = ext4_bg_get_free_inodes_count(bg, sb);
255                 uint32_t used_dirs = ext4_bg_get_used_dirs_count(bg, sb);
256
257                 /* Check if this block group is good candidate for allocation */
258                 if (free_inodes > 0) {
259                         /* Load block with bitmap */
260                         uint32_t bmp_blk_add = ext4_bg_get_inode_bitmap(bg, sb);
261
262                         struct ext4_block b;
263                         rc = ext4_trans_block_get(fs->bdev, &b, bmp_blk_add);
264                         if (rc != EOK) {
265                                 ext4_fs_put_block_group_ref(&bg_ref);
266                                 return rc;
267                         }
268
269                         if (!ext4_ialloc_verify_bitmap_csum(sb, bg, b.data)) {
270                                 ext4_dbg(DEBUG_IALLOC,
271                                         DBG_WARN "Bitmap checksum failed."
272                                         "Group: %" PRIu32"\n",
273                                         bg_ref.index);
274                         }
275
276                         /* Try to allocate i-node in the bitmap */
277                         uint32_t inodes_in_bg;
278                         uint32_t idx_in_bg;
279
280                         inodes_in_bg = ext4_inodes_in_group_cnt(sb, bgid);
281                         rc = ext4_bmap_bit_find_clr(b.data, 0, inodes_in_bg,
282                                                     &idx_in_bg);
283                         /* Block group has not any free i-node */
284                         if (rc == ENOSPC) {
285                                 rc = ext4_block_set(fs->bdev, &b);
286                                 if (rc != EOK) {
287                                         ext4_fs_put_block_group_ref(&bg_ref);
288                                         return rc;
289                                 }
290
291                                 rc = ext4_fs_put_block_group_ref(&bg_ref);
292                                 if (rc != EOK)
293                                         return rc;
294
295                                 continue;
296                         }
297
298                         ext4_bmap_bit_set(b.data, idx_in_bg);
299
300                         /* Free i-node found, save the bitmap */
301                         ext4_ialloc_set_bitmap_csum(sb,bg,
302                                                     b.data);
303                         ext4_trans_set_block_dirty(b.buf);
304
305                         ext4_block_set(fs->bdev, &b);
306                         if (rc != EOK) {
307                                 ext4_fs_put_block_group_ref(&bg_ref);
308                                 return rc;
309                         }
310
311                         /* Modify filesystem counters */
312                         free_inodes--;
313                         ext4_bg_set_free_inodes_count(bg, sb, free_inodes);
314
315                         /* Increment used directories counter */
316                         if (is_dir) {
317                                 used_dirs++;
318                                 ext4_bg_set_used_dirs_count(bg, sb, used_dirs);
319                         }
320
321                         /* Decrease unused inodes count */
322                         uint32_t unused =
323                             ext4_bg_get_itable_unused(bg, sb);
324
325                         uint32_t free = inodes_in_bg - unused;
326
327                         if (idx_in_bg >= free) {
328                                 unused = inodes_in_bg - (idx_in_bg + 1);
329                                 ext4_bg_set_itable_unused(bg, sb, unused);
330                         }
331
332                         /* Save modified block group */
333                         bg_ref.dirty = true;
334
335                         rc = ext4_fs_put_block_group_ref(&bg_ref);
336                         if (rc != EOK)
337                                 return rc;
338
339                         /* Update superblock */
340                         sb_free_inodes--;
341                         ext4_set32(sb, free_inodes_count, sb_free_inodes);
342
343                         /* Compute the absolute i-nodex number */
344                         *idx = ext4_ialloc_bgidx_to_inode(sb, idx_in_bg, bgid);
345
346                         fs->last_inode_bg_id = bgid;
347
348                         return EOK;
349                 }
350
351                 /* Block group not modified, put it and jump to the next block
352                  * group */
353                 ext4_fs_put_block_group_ref(&bg_ref);
354                 if (rc != EOK)
355                         return rc;
356
357                 ++bgid;
358         }
359
360         return ENOSPC;
361 }
362
363 /**
364  * @}
365  */