Whamcloud - gitweb
e2fsck: add support for xattrs in external inodes
[tools/e2fsprogs.git] / lib / ext2fs / alloc.c
1 /*
2  * alloc.c --- allocate new inodes, blocks for ext2fs
3  *
4  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
13 #include <stdio.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <time.h>
18 #include <string.h>
19 #if HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22 #if HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25
26 #include "ext2_fs.h"
27 #include "ext2fs.h"
28
29 /*
30  * Check for uninit block bitmaps and deal with them appropriately
31  */
32 static void check_block_uninit(ext2_filsys fs, ext2fs_block_bitmap map,
33                                dgrp_t group)
34 {
35         blk_t           i;
36         blk64_t         blk, super_blk, old_desc_blk, new_desc_blk;
37         int             old_desc_blocks;
38
39         if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
40                                          EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
41             !(ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)))
42                 return;
43
44         blk = (group * fs->super->s_blocks_per_group) +
45                 fs->super->s_first_data_block;
46
47         ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
48                                   &old_desc_blk, &new_desc_blk, 0);
49
50         if (fs->super->s_feature_incompat &
51             EXT2_FEATURE_INCOMPAT_META_BG)
52                 old_desc_blocks = fs->super->s_first_meta_bg;
53         else
54                 old_desc_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
55
56         for (i=0; i < fs->super->s_blocks_per_group; i++, blk++)
57                 ext2fs_fast_unmark_block_bitmap2(map, blk);
58
59         blk = (group * fs->super->s_blocks_per_group) +
60                 fs->super->s_first_data_block;
61         for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
62                 if ((blk == super_blk) ||
63                     (old_desc_blk && old_desc_blocks &&
64                      (blk >= old_desc_blk) &&
65                      (blk < old_desc_blk + old_desc_blocks)) ||
66                     (new_desc_blk && (blk == new_desc_blk)) ||
67                     (blk == ext2fs_block_bitmap_loc(fs, group)) ||
68                     (blk == ext2fs_inode_bitmap_loc(fs, group)) ||
69                     (blk >= ext2fs_inode_table_loc(fs, group) &&
70                      (blk < ext2fs_inode_table_loc(fs, group)
71                       + fs->inode_blocks_per_group)))
72                         ext2fs_fast_mark_block_bitmap2(map, blk);
73         }
74         ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
75         ext2fs_group_desc_csum_set(fs, group);
76         ext2fs_mark_super_dirty(fs);
77         ext2fs_mark_bb_dirty(fs);
78 }
79
80 /*
81  * Check for uninit inode bitmaps and deal with them appropriately
82  */
83 static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
84                           dgrp_t group)
85 {
86         ext2_ino_t      i, ino;
87
88         if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
89                                          EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
90             !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
91                 return;
92
93         ino = (group * fs->super->s_inodes_per_group) + 1;
94         for (i=0; i < fs->super->s_inodes_per_group; i++, ino++)
95                 ext2fs_fast_unmark_inode_bitmap2(map, ino);
96
97         ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
98         ext2fs_group_desc_csum_set(fs, group);
99         ext2fs_mark_ib_dirty(fs);
100         ext2fs_mark_super_dirty(fs);
101         check_block_uninit(fs, fs->block_map, group);
102 }
103
104 /*
105  * Right now, just search forward from the parent directory's block
106  * group to find the next free inode.
107  *
108  * Should have a special policy for directories.
109  */
110 errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
111                            int mode EXT2FS_ATTR((unused)),
112                            ext2fs_inode_bitmap map, ext2_ino_t *ret)
113 {
114         ext2_ino_t      start_inode = 0;
115         ext2_ino_t      i, ino_in_group, upto, first_zero;
116         errcode_t       retval;
117         dgrp_t          group;
118
119         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
120
121         if (!map)
122                 map = fs->inode_map;
123         if (!map)
124                 return EXT2_ET_NO_INODE_BITMAP;
125
126         if (dir > 0) {
127                 group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
128                 start_inode = (group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
129         }
130         if (start_inode < EXT2_FIRST_INODE(fs->super))
131                 start_inode = EXT2_FIRST_INODE(fs->super);
132         if (start_inode > fs->super->s_inodes_count)
133                 return EXT2_ET_INODE_ALLOC_FAIL;
134         i = start_inode;
135         do {
136                 ino_in_group = (i - 1) % EXT2_INODES_PER_GROUP(fs->super);
137                 group = (i - 1) / EXT2_INODES_PER_GROUP(fs->super);
138
139                 check_inode_uninit(fs, map, group);
140                 upto = i + (EXT2_INODES_PER_GROUP(fs->super) - ino_in_group);
141                 if (i < start_inode && upto >= start_inode)
142                         upto = start_inode - 1;
143                 if (upto > fs->super->s_inodes_count)
144                         upto = fs->super->s_inodes_count;
145
146                 retval = ext2fs_find_first_zero_inode_bitmap2(map, i, upto,
147                                                               &first_zero);
148                 if (retval == 0) {
149                         i = first_zero;
150                         break;
151                 }
152                 if (retval != ENOENT)
153                         return EXT2_ET_INODE_ALLOC_FAIL;
154                 i = upto + 1;
155                 if (i > fs->super->s_inodes_count)
156                         i = EXT2_FIRST_INODE(fs->super);
157         } while (i != start_inode);
158
159         if (ext2fs_test_inode_bitmap2(map, i))
160                 return EXT2_ET_INODE_ALLOC_FAIL;
161         *ret = i;
162         return 0;
163 }
164
165 /*
166  * Stupid algorithm --- we now just search forward starting from the
167  * goal.  Should put in a smarter one someday....
168  */
169 errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
170                            ext2fs_block_bitmap map, blk64_t *ret)
171 {
172         blk64_t i;
173         int     c_ratio;
174
175         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
176
177         if (!map)
178                 map = fs->block_map;
179         if (!map)
180                 return EXT2_ET_NO_BLOCK_BITMAP;
181         if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
182                 goal = fs->super->s_first_data_block;
183         i = goal;
184         c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
185         if (c_ratio > 1)
186                 goal &= ~EXT2FS_CLUSTER_MASK(fs);
187         check_block_uninit(fs, map,
188                            (i - fs->super->s_first_data_block) /
189                            EXT2_BLOCKS_PER_GROUP(fs->super));
190         do {
191                 if (((i - fs->super->s_first_data_block) %
192                      EXT2_BLOCKS_PER_GROUP(fs->super)) == 0)
193                         check_block_uninit(fs, map,
194                                            (i - fs->super->s_first_data_block) /
195                                            EXT2_BLOCKS_PER_GROUP(fs->super));
196
197                 if (!ext2fs_fast_test_block_bitmap2(map, i)) {
198                         *ret = i;
199                         return 0;
200                 }
201                 i = (i + c_ratio) & ~(c_ratio - 1);
202                 if (i >= ext2fs_blocks_count(fs->super))
203                         i = fs->super->s_first_data_block;
204         } while (i != goal);
205         return EXT2_ET_BLOCK_ALLOC_FAIL;
206 }
207
208 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
209                            ext2fs_block_bitmap map, blk_t *ret)
210 {
211         errcode_t retval;
212         blk64_t val;
213         retval = ext2fs_new_block2(fs, goal, map, &val);
214         if (!retval)
215                 *ret = (blk_t) val;
216         return retval;
217 }
218
219 /*
220  * This function zeros out the allocated block, and updates all of the
221  * appropriate filesystem records.
222  */
223 errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
224                              char *block_buf, blk64_t *ret)
225 {
226         errcode_t       retval;
227         blk64_t         block;
228         char            *buf = 0;
229
230         if (!block_buf) {
231                 retval = ext2fs_get_mem(fs->blocksize, &buf);
232                 if (retval)
233                         return retval;
234                 block_buf = buf;
235         }
236         memset(block_buf, 0, fs->blocksize);
237
238         if (fs->get_alloc_block) {
239                 retval = (fs->get_alloc_block)(fs, goal, &block);
240                 if (retval)
241                         goto fail;
242         } else {
243                 if (!fs->block_map) {
244                         retval = ext2fs_read_block_bitmap(fs);
245                         if (retval)
246                                 goto fail;
247                 }
248
249                 retval = ext2fs_new_block2(fs, goal, 0, &block);
250                 if (retval)
251                         goto fail;
252         }
253
254         retval = io_channel_write_blk64(fs->io, block, 1, block_buf);
255         if (retval)
256                 goto fail;
257
258         ext2fs_block_alloc_stats2(fs, block, +1);
259         *ret = block;
260
261 fail:
262         if (buf)
263                 ext2fs_free_mem(&buf);
264         return retval;
265 }
266
267 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
268                              char *block_buf, blk_t *ret)
269 {
270         errcode_t retval;
271         blk64_t val;
272         retval = ext2fs_alloc_block2(fs, goal, block_buf, &val);
273         if (!retval)
274                 *ret = (blk_t) val;
275         return retval;
276 }
277
278 errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
279                                  int num, ext2fs_block_bitmap map, blk64_t *ret)
280 {
281         blk64_t b = start;
282         int     c_ratio;
283
284         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
285
286         if (!map)
287                 map = fs->block_map;
288         if (!map)
289                 return EXT2_ET_NO_BLOCK_BITMAP;
290         if (!b)
291                 b = fs->super->s_first_data_block;
292         if (!finish)
293                 finish = start;
294         if (!num)
295                 num = 1;
296         c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
297         b &= ~(c_ratio - 1);
298         finish &= ~(c_ratio -1);
299         do {
300                 if (b+num-1 > ext2fs_blocks_count(fs->super))
301                         b = fs->super->s_first_data_block;
302                 if (ext2fs_fast_test_block_bitmap_range2(map, b, num)) {
303                         *ret = b;
304                         return 0;
305                 }
306                 b += c_ratio;
307         } while (b != finish);
308         return EXT2_ET_BLOCK_ALLOC_FAIL;
309 }
310
311 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
312                                  int num, ext2fs_block_bitmap map, blk_t *ret)
313 {
314         errcode_t retval;
315         blk64_t val;
316         retval = ext2fs_get_free_blocks2(fs, start, finish, num, map, &val);
317         if(!retval)
318                 *ret = (blk_t) val;
319         return retval;
320 }
321
322 void ext2fs_set_alloc_block_callback(ext2_filsys fs,
323                                      errcode_t (*func)(ext2_filsys fs,
324                                                        blk64_t goal,
325                                                        blk64_t *ret),
326                                      errcode_t (**old)(ext2_filsys fs,
327                                                        blk64_t goal,
328                                                        blk64_t *ret))
329 {
330         if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
331                 return;
332
333         if (old)
334                 *old = fs->get_alloc_block;
335
336         fs->get_alloc_block = func;
337 }