FIX: list buffer size is not correctly returned on ext4_listxattr calls.
[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_fs.h"
47 #include "ext4_blockdev.h"
48 #include "ext4_block_group.h"
49 #include "ext4_bitmap.h"
50
51 /**@brief  Convert i-node number to relative index in block group.
52  * @param sb    Superblock
53  * @param inode I-node number to be converted
54  * @return Index of the i-node in the block group
55  */
56 static uint32_t ext4_ialloc_inode2index_in_group(struct ext4_sblock *sb,
57                                                  uint32_t inode)
58 {
59         uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
60         return (inode - 1) % inodes_per_group;
61 }
62
63 /**@brief Convert relative index of i-node to absolute i-node number.
64  * @param sb    Superblock
65  * @param index Index to be converted
66  * @return Absolute number of the i-node
67  *
68  */
69 static uint32_t ext4_ialloc_index_in_group2inode(struct ext4_sblock *sb,
70                                                  uint32_t index, uint32_t bgid)
71 {
72         uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
73         return bgid * inodes_per_group + (index + 1);
74 }
75
76 /**@brief Compute block group number from the i-node number.
77  * @param sb    Superblock
78  * @param inode I-node number to be found the block group for
79  * @return Block group number computed from i-node number
80  */
81 static uint32_t ext4_ialloc_get_bgid_of_inode(struct ext4_sblock *sb,
82                                               uint32_t inode)
83 {
84         uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
85         return (inode - 1) / inodes_per_group;
86 }
87
88 int ext4_ialloc_free_inode(struct ext4_fs *fs, uint32_t index, bool is_dir)
89 {
90         struct ext4_sblock *sb = &fs->sb;
91
92         /* Compute index of block group and load it */
93         uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
94
95         struct ext4_block_group_ref bg_ref;
96         int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
97         if (rc != EOK)
98                 return rc;
99
100         /* Load i-node bitmap */
101         uint32_t bitmap_block_addr =
102             ext4_bg_get_inode_bitmap(bg_ref.block_group, sb);
103
104         struct ext4_block bitmap_block;
105         rc = ext4_block_get(fs->bdev, &bitmap_block, bitmap_block_addr);
106         if (rc != EOK)
107                 return rc;
108
109         /* Free i-node in the bitmap */
110         uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
111         ext4_bmap_bit_clr(bitmap_block.data, index_in_group);
112         bitmap_block.dirty = true;
113
114         /* Put back the block with bitmap */
115         rc = ext4_block_set(fs->bdev, &bitmap_block);
116         if (rc != EOK) {
117                 /* Error in saving bitmap */
118                 ext4_fs_put_block_group_ref(&bg_ref);
119                 return rc;
120         }
121
122         /* If released i-node is a directory, decrement used directories count
123          */
124         if (is_dir) {
125                 uint32_t bg_used_dirs =
126                     ext4_bg_get_used_dirs_count(bg_ref.block_group, sb);
127                 bg_used_dirs--;
128                 ext4_bg_set_used_dirs_count(bg_ref.block_group, sb,
129                                             bg_used_dirs);
130         }
131
132         /* Update block group free inodes count */
133         uint32_t free_inodes =
134             ext4_bg_get_free_inodes_count(bg_ref.block_group, sb);
135         free_inodes++;
136         ext4_bg_set_free_inodes_count(bg_ref.block_group, sb, free_inodes);
137
138         bg_ref.dirty = true;
139
140         /* Put back the modified block group */
141         rc = ext4_fs_put_block_group_ref(&bg_ref);
142         if (rc != EOK)
143                 return rc;
144
145         /* Update superblock free inodes count */
146         ext4_set32(sb, free_inodes_count,
147                    ext4_get32(sb, free_inodes_count) + 1);
148
149         return EOK;
150 }
151
152 int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *index, bool is_dir)
153 {
154         struct ext4_sblock *sb = &fs->sb;
155
156         uint32_t bgid = fs->last_inode_bg_id;
157         uint32_t bg_count = ext4_block_group_cnt(sb);
158         uint32_t sb_free_inodes = ext4_get32(sb, free_inodes_count);
159         bool rewind = false;
160
161         /* Try to find free i-node in all block groups */
162         while (bgid <= bg_count) {
163
164                 if (bgid == bg_count) {
165                         if (rewind)
166                                 break;
167                         bg_count = fs->last_inode_bg_id;
168                         bgid = 0;
169                         rewind = true;
170                         continue;
171                 }
172
173                 /* Load block group to check */
174                 struct ext4_block_group_ref bg_ref;
175                 int rc = ext4_fs_get_block_group_ref(fs, bgid, &bg_ref);
176                 if (rc != EOK)
177                         return rc;
178
179                 struct ext4_bgroup *bg = bg_ref.block_group;
180
181                 /* Read necessary values for algorithm */
182                 uint32_t free_inodes = ext4_bg_get_free_inodes_count(bg, sb);
183                 uint32_t used_dirs = ext4_bg_get_used_dirs_count(bg, sb);
184
185                 /* Check if this block group is good candidate for allocation */
186                 if (free_inodes > 0) {
187                         /* Load block with bitmap */
188                         uint32_t bitmap_block_addr =
189                             ext4_bg_get_inode_bitmap(bg_ref.block_group, sb);
190
191                         struct ext4_block bitmap_block;
192                         rc = ext4_block_get(fs->bdev, &bitmap_block,
193                                             bitmap_block_addr);
194                         if (rc != EOK) {
195                                 ext4_fs_put_block_group_ref(&bg_ref);
196                                 return rc;
197                         }
198
199                         /* Try to allocate i-node in the bitmap */
200                         uint32_t inodes_in_group =
201                             ext4_inodes_in_group_cnt(sb, bgid);
202                         uint32_t index_in_group;
203
204                         rc = ext4_bmap_bit_find_clr(bitmap_block.data, 0,
205                                                     inodes_in_group,
206                                                     &index_in_group);
207                         /* Block group has not any free i-node */
208                         if (rc == ENOSPC) {
209                                 rc = ext4_block_set(fs->bdev, &bitmap_block);
210                                 if (rc != EOK) {
211                                         ext4_fs_put_block_group_ref(&bg_ref);
212                                         return rc;
213                                 }
214
215                                 rc = ext4_fs_put_block_group_ref(&bg_ref);
216                                 if (rc != EOK)
217                                         return rc;
218
219                                 continue;
220                         }
221
222                         ext4_bmap_bit_set(bitmap_block.data, index_in_group);
223
224                         /* Free i-node found, save the bitmap */
225                         bitmap_block.dirty = true;
226
227                         ext4_block_set(fs->bdev, &bitmap_block);
228                         if (rc != EOK) {
229                                 ext4_fs_put_block_group_ref(&bg_ref);
230                                 return rc;
231                         }
232
233                         /* Modify filesystem counters */
234                         free_inodes--;
235                         ext4_bg_set_free_inodes_count(bg, sb, free_inodes);
236
237                         /* Increment used directories counter */
238                         if (is_dir) {
239                                 used_dirs++;
240                                 ext4_bg_set_used_dirs_count(bg, sb, used_dirs);
241                         }
242
243                         /* Decrease unused inodes count */
244                         if (ext4_bg_has_flag(bg,
245                                              EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
246                                 uint32_t unused =
247                                     ext4_bg_get_itable_unused(bg, sb);
248
249                                 uint32_t inodes_in_group =
250                                     ext4_inodes_in_group_cnt(sb, bgid);
251
252                                 uint32_t free = inodes_in_group - unused;
253
254                                 if (index_in_group >= free) {
255                                         unused = inodes_in_group -
256                                                  (index_in_group + 1);
257                                         ext4_bg_set_itable_unused(bg, sb,
258                                                                   unused);
259                                 }
260                         }
261
262                         /* Save modified block group */
263                         bg_ref.dirty = true;
264
265                         rc = ext4_fs_put_block_group_ref(&bg_ref);
266                         if (rc != EOK)
267                                 return rc;
268
269                         /* Update superblock */
270                         sb_free_inodes--;
271                         ext4_set32(sb, free_inodes_count, sb_free_inodes);
272
273                         /* Compute the absolute i-nodex number */
274                         *index = ext4_ialloc_index_in_group2inode(
275                             sb, index_in_group, bgid);
276
277                         fs->last_inode_bg_id = bgid;
278
279                         return EOK;
280                 }
281
282                 /* Block group not modified, put it and jump to the next block
283                  * group */
284                 ext4_fs_put_block_group_ref(&bg_ref);
285                 if (rc != EOK)
286                         return rc;
287
288                 ++bgid;
289         }
290
291         return ENOSPC;
292 }
293
294 /**
295  * @}
296  */