Fix compile warning
[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_inode2index_in_group(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_index_in_group2inode(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,
91                                         void *bitmap)
92 {
93         uint32_t checksum = 0;
94         if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
95                 uint32_t inodes_per_group =
96                         ext4_get32(sb, inodes_per_group);
97
98                 /* First calculate crc32 checksum against fs uuid */
99                 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
100                                 sizeof(sb->uuid));
101                 /* Then calculate crc32 checksum against inode bitmap */
102                 checksum = ext4_crc32c(checksum, bitmap,
103                                      (inodes_per_group + 7) / 8);
104         }
105         return checksum;
106 }
107 #else
108 #define ext4_ialloc_bitmap_csum(...) 0
109 #endif
110
111 void ext4_ialloc_set_bitmap_csum(struct ext4_sblock *sb,
112                                  struct ext4_bgroup *bg,
113                                  void *bitmap __unused)
114 {
115         int desc_size = ext4_sb_get_desc_size(sb);
116         uint32_t checksum = ext4_ialloc_bitmap_csum(sb, bitmap);
117         uint16_t lo_checksum = to_le16(checksum & 0xFFFF),
118                  hi_checksum = to_le16(checksum >> 16);
119         
120         if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
121                 return;
122
123         /* See if we need to assign a 32bit checksum */
124         bg->inode_bitmap_csum_lo = lo_checksum;
125         if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
126                 bg->inode_bitmap_csum_hi = hi_checksum;
127
128 }
129
130 #if CONFIG_META_CSUM_ENABLE
131 static bool
132 ext4_ialloc_verify_bitmap_csum(struct ext4_sblock *sb,
133                                struct ext4_bgroup *bg,
134                                void *bitmap __unused)
135 {
136
137         int desc_size = ext4_sb_get_desc_size(sb);
138         uint32_t checksum = ext4_ialloc_bitmap_csum(sb, bitmap);
139         uint16_t lo_checksum = to_le16(checksum & 0xFFFF),
140                  hi_checksum = to_le16(checksum >> 16);
141
142         if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
143                 return true;
144         
145         if (bg->inode_bitmap_csum_lo != lo_checksum)
146                 return false;
147
148         if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
149                 if (bg->inode_bitmap_csum_hi != hi_checksum)
150                         return false;
151
152         return true;
153 }
154 #else
155 #define ext4_ialloc_verify_bitmap_csum(...) true
156 #endif
157
158 int ext4_ialloc_free_inode(struct ext4_fs *fs, uint32_t index, bool is_dir)
159 {
160         struct ext4_sblock *sb = &fs->sb;
161
162         /* Compute index of block group and load it */
163         uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
164
165         struct ext4_block_group_ref bg_ref;
166         int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
167         if (rc != EOK)
168                 return rc;
169
170         /* Load i-node bitmap */
171         uint32_t bitmap_block_addr =
172             ext4_bg_get_inode_bitmap(bg_ref.block_group, sb);
173
174         struct ext4_block bitmap_block;
175         rc = ext4_block_get(fs->bdev, &bitmap_block, bitmap_block_addr);
176         if (rc != EOK)
177                 return rc;
178
179         if (!ext4_ialloc_verify_bitmap_csum(sb,
180                                bg_ref.block_group,
181                                bitmap_block.data)) {
182                 ext4_dbg(DEBUG_IALLOC,
183                         DBG_WARN "Bitmap checksum failed."
184                         "Group: %" PRIu32"\n",
185                         bg_ref.index);
186         }
187
188         /* Free i-node in the bitmap */
189         uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
190         ext4_bmap_bit_clr(bitmap_block.data, index_in_group);
191         ext4_ialloc_set_bitmap_csum(sb, bg_ref.block_group,
192                                     bitmap_block.data);
193         bitmap_block.dirty = true;
194
195         /* Put back the block with bitmap */
196         rc = ext4_block_set(fs->bdev, &bitmap_block);
197         if (rc != EOK) {
198                 /* Error in saving bitmap */
199                 ext4_fs_put_block_group_ref(&bg_ref);
200                 return rc;
201         }
202
203         /* If released i-node is a directory, decrement used directories count
204          */
205         if (is_dir) {
206                 uint32_t bg_used_dirs =
207                     ext4_bg_get_used_dirs_count(bg_ref.block_group, sb);
208                 bg_used_dirs--;
209                 ext4_bg_set_used_dirs_count(bg_ref.block_group, sb,
210                                             bg_used_dirs);
211         }
212
213         /* Update block group free inodes count */
214         uint32_t free_inodes =
215             ext4_bg_get_free_inodes_count(bg_ref.block_group, sb);
216         free_inodes++;
217         ext4_bg_set_free_inodes_count(bg_ref.block_group, sb, free_inodes);
218
219         bg_ref.dirty = true;
220
221         /* Put back the modified block group */
222         rc = ext4_fs_put_block_group_ref(&bg_ref);
223         if (rc != EOK)
224                 return rc;
225
226         /* Update superblock free inodes count */
227         ext4_set32(sb, free_inodes_count,
228                    ext4_get32(sb, free_inodes_count) + 1);
229
230         return EOK;
231 }
232
233 int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *index, bool is_dir)
234 {
235         struct ext4_sblock *sb = &fs->sb;
236
237         uint32_t bgid = fs->last_inode_bg_id;
238         uint32_t bg_count = ext4_block_group_cnt(sb);
239         uint32_t sb_free_inodes = ext4_get32(sb, free_inodes_count);
240         bool rewind = false;
241
242         /* Try to find free i-node in all block groups */
243         while (bgid <= bg_count) {
244
245                 if (bgid == bg_count) {
246                         if (rewind)
247                                 break;
248                         bg_count = fs->last_inode_bg_id;
249                         bgid = 0;
250                         rewind = true;
251                         continue;
252                 }
253
254                 /* Load block group to check */
255                 struct ext4_block_group_ref bg_ref;
256                 int rc = ext4_fs_get_block_group_ref(fs, bgid, &bg_ref);
257                 if (rc != EOK)
258                         return rc;
259
260                 struct ext4_bgroup *bg = bg_ref.block_group;
261
262                 /* Read necessary values for algorithm */
263                 uint32_t free_inodes = ext4_bg_get_free_inodes_count(bg, sb);
264                 uint32_t used_dirs = ext4_bg_get_used_dirs_count(bg, sb);
265
266                 /* Check if this block group is good candidate for allocation */
267                 if (free_inodes > 0) {
268                         /* Load block with bitmap */
269                         uint32_t bitmap_block_addr =
270                             ext4_bg_get_inode_bitmap(bg_ref.block_group, sb);
271
272                         struct ext4_block bitmap_block;
273                         rc = ext4_block_get(fs->bdev, &bitmap_block,
274                                             bitmap_block_addr);
275                         if (rc != EOK) {
276                                 ext4_fs_put_block_group_ref(&bg_ref);
277                                 return rc;
278                         }
279
280                         if (!ext4_ialloc_verify_bitmap_csum(sb,
281                                                bg_ref.block_group,
282                                                bitmap_block.data)) {
283                                 ext4_dbg(DEBUG_IALLOC,
284                                         DBG_WARN "Bitmap checksum failed."
285                                         "Group: %" PRIu32"\n",
286                                         bg_ref.index);
287                         }
288
289                         /* Try to allocate i-node in the bitmap */
290                         uint32_t inodes_in_group =
291                             ext4_inodes_in_group_cnt(sb, bgid);
292                         uint32_t index_in_group;
293
294                         rc = ext4_bmap_bit_find_clr(bitmap_block.data, 0,
295                                                     inodes_in_group,
296                                                     &index_in_group);
297                         /* Block group has not any free i-node */
298                         if (rc == ENOSPC) {
299                                 rc = ext4_block_set(fs->bdev, &bitmap_block);
300                                 if (rc != EOK) {
301                                         ext4_fs_put_block_group_ref(&bg_ref);
302                                         return rc;
303                                 }
304
305                                 rc = ext4_fs_put_block_group_ref(&bg_ref);
306                                 if (rc != EOK)
307                                         return rc;
308
309                                 continue;
310                         }
311
312                         ext4_bmap_bit_set(bitmap_block.data, index_in_group);
313
314                         /* Free i-node found, save the bitmap */
315                         ext4_ialloc_set_bitmap_csum(sb, bg_ref.block_group,
316                                                     bitmap_block.data);
317                         bitmap_block.dirty = true;
318
319                         ext4_block_set(fs->bdev, &bitmap_block);
320                         if (rc != EOK) {
321                                 ext4_fs_put_block_group_ref(&bg_ref);
322                                 return rc;
323                         }
324
325                         /* Modify filesystem counters */
326                         free_inodes--;
327                         ext4_bg_set_free_inodes_count(bg, sb, free_inodes);
328
329                         /* Increment used directories counter */
330                         if (is_dir) {
331                                 used_dirs++;
332                                 ext4_bg_set_used_dirs_count(bg, sb, used_dirs);
333                         }
334
335                         /* Decrease unused inodes count */
336                         uint32_t unused =
337                             ext4_bg_get_itable_unused(bg, sb);
338
339                         uint32_t free = inodes_in_group - unused;
340
341                         if (index_in_group >= free) {
342                                 unused = inodes_in_group -
343                                          (index_in_group + 1);
344                                 ext4_bg_set_itable_unused(bg, sb,
345                                                           unused);
346                         }
347
348                         /* Save modified block group */
349                         bg_ref.dirty = true;
350
351                         rc = ext4_fs_put_block_group_ref(&bg_ref);
352                         if (rc != EOK)
353                                 return rc;
354
355                         /* Update superblock */
356                         sb_free_inodes--;
357                         ext4_set32(sb, free_inodes_count, sb_free_inodes);
358
359                         /* Compute the absolute i-nodex number */
360                         *index = ext4_ialloc_index_in_group2inode(
361                             sb, index_in_group, bgid);
362
363                         fs->last_inode_bg_id = bgid;
364
365                         return EOK;
366                 }
367
368                 /* Block group not modified, put it and jump to the next block
369                  * group */
370                 ext4_fs_put_block_group_ref(&bg_ref);
371                 if (rc != EOK)
372                         return rc;
373
374                 ++bgid;
375         }
376
377         return ENOSPC;
378 }
379
380 /**
381  * @}
382  */