Whamcloud - gitweb
libext2fs: check for fallocate symbol before using it
[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 }
77
78 /*
79  * Check for uninit inode bitmaps and deal with them appropriately
80  */
81 static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
82                           dgrp_t group)
83 {
84         ext2_ino_t      i, ino;
85
86         if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
87                                          EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
88             !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
89                 return;
90
91         ino = (group * fs->super->s_inodes_per_group) + 1;
92         for (i=0; i < fs->super->s_inodes_per_group; i++, ino++)
93                 ext2fs_fast_unmark_inode_bitmap2(map, ino);
94
95         ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
96         check_block_uninit(fs, fs->block_map, group);
97 }
98
99 /*
100  * Right now, just search forward from the parent directory's block
101  * group to find the next free inode.
102  *
103  * Should have a special policy for directories.
104  */
105 errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
106                            int mode EXT2FS_ATTR((unused)),
107                            ext2fs_inode_bitmap map, ext2_ino_t *ret)
108 {
109         ext2_ino_t      dir_group = 0;
110         ext2_ino_t      i;
111         ext2_ino_t      start_inode;
112
113         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
114
115         if (!map)
116                 map = fs->inode_map;
117         if (!map)
118                 return EXT2_ET_NO_INODE_BITMAP;
119
120         if (dir > 0)
121                 dir_group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
122
123         start_inode = (dir_group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
124         if (start_inode < EXT2_FIRST_INODE(fs->super))
125                 start_inode = EXT2_FIRST_INODE(fs->super);
126         if (start_inode > fs->super->s_inodes_count)
127                 return EXT2_ET_INODE_ALLOC_FAIL;
128         i = start_inode;
129
130         do {
131                 if (((i - 1) % EXT2_INODES_PER_GROUP(fs->super)) == 0)
132                         check_inode_uninit(fs, map, (i - 1) /
133                                            EXT2_INODES_PER_GROUP(fs->super));
134
135                 if (!ext2fs_fast_test_inode_bitmap2(map, i))
136                         break;
137                 i++;
138                 if (i > fs->super->s_inodes_count)
139                         i = EXT2_FIRST_INODE(fs->super);
140         } while (i != start_inode);
141
142         if (ext2fs_test_inode_bitmap2(map, i))
143                 return EXT2_ET_INODE_ALLOC_FAIL;
144         *ret = i;
145         return 0;
146 }
147
148 /*
149  * Stupid algorithm --- we now just search forward starting from the
150  * goal.  Should put in a smarter one someday....
151  */
152 errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
153                            ext2fs_block_bitmap map, blk64_t *ret)
154 {
155         blk64_t i;
156         int     c_ratio;
157
158         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
159
160         if (!map)
161                 map = fs->block_map;
162         if (!map)
163                 return EXT2_ET_NO_BLOCK_BITMAP;
164         if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
165                 goal = fs->super->s_first_data_block;
166         i = goal;
167         c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
168         if (c_ratio > 1)
169                 goal &= ~EXT2FS_CLUSTER_MASK(fs);
170         check_block_uninit(fs, map,
171                            (i - fs->super->s_first_data_block) /
172                            EXT2_BLOCKS_PER_GROUP(fs->super));
173         do {
174                 if (((i - fs->super->s_first_data_block) %
175                      EXT2_BLOCKS_PER_GROUP(fs->super)) == 0)
176                         check_block_uninit(fs, map,
177                                            (i - fs->super->s_first_data_block) /
178                                            EXT2_BLOCKS_PER_GROUP(fs->super));
179
180                 if (!ext2fs_fast_test_block_bitmap2(map, i)) {
181                         *ret = i;
182                         return 0;
183                 }
184                 i = (i + c_ratio) & ~(c_ratio - 1);
185                 if (i >= ext2fs_blocks_count(fs->super))
186                         i = fs->super->s_first_data_block;
187         } while (i != goal);
188         return EXT2_ET_BLOCK_ALLOC_FAIL;
189 }
190
191 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
192                            ext2fs_block_bitmap map, blk_t *ret)
193 {
194         errcode_t retval;
195         blk64_t val;
196         retval = ext2fs_new_block2(fs, goal, map, &val);
197         if (!retval)
198                 *ret = (blk_t) val;
199         return retval;
200 }
201
202 /*
203  * This function zeros out the allocated block, and updates all of the
204  * appropriate filesystem records.
205  */
206 errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
207                              char *block_buf, blk64_t *ret)
208 {
209         errcode_t       retval;
210         blk64_t         block;
211         char            *buf = 0;
212
213         if (!block_buf) {
214                 retval = ext2fs_get_mem(fs->blocksize, &buf);
215                 if (retval)
216                         return retval;
217                 block_buf = buf;
218         }
219         memset(block_buf, 0, fs->blocksize);
220
221         if (fs->get_alloc_block) {
222                 retval = (fs->get_alloc_block)(fs, goal, &block);
223                 if (retval)
224                         goto fail;
225         } else {
226                 if (!fs->block_map) {
227                         retval = ext2fs_read_block_bitmap(fs);
228                         if (retval)
229                                 goto fail;
230                 }
231
232                 retval = ext2fs_new_block2(fs, goal, 0, &block);
233                 if (retval)
234                         goto fail;
235         }
236
237         retval = io_channel_write_blk64(fs->io, block, 1, block_buf);
238         if (retval)
239                 goto fail;
240
241         ext2fs_block_alloc_stats2(fs, block, +1);
242         *ret = block;
243
244 fail:
245         if (buf)
246                 ext2fs_free_mem(&buf);
247         return retval;
248 }
249
250 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
251                              char *block_buf, blk_t *ret)
252 {
253         errcode_t retval;
254         blk64_t val;
255         retval = ext2fs_alloc_block2(fs, goal, block_buf, &val);
256         if (!retval)
257                 *ret = (blk_t) val;
258         return retval;
259 }
260
261 errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
262                                  int num, ext2fs_block_bitmap map, blk64_t *ret)
263 {
264         blk64_t b = start;
265         int     c_ratio;
266
267         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
268
269         if (!map)
270                 map = fs->block_map;
271         if (!map)
272                 return EXT2_ET_NO_BLOCK_BITMAP;
273         if (!b)
274                 b = fs->super->s_first_data_block;
275         if (!finish)
276                 finish = start;
277         if (!num)
278                 num = 1;
279         c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
280         b &= ~(c_ratio - 1);
281         finish &= ~(c_ratio -1);
282         do {
283                 if (b+num-1 > ext2fs_blocks_count(fs->super))
284                         b = fs->super->s_first_data_block;
285                 if (ext2fs_fast_test_block_bitmap_range2(map, b, num)) {
286                         *ret = b;
287                         return 0;
288                 }
289                 b += c_ratio;
290         } while (b != finish);
291         return EXT2_ET_BLOCK_ALLOC_FAIL;
292 }
293
294 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
295                                  int num, ext2fs_block_bitmap map, blk_t *ret)
296 {
297         errcode_t retval;
298         blk64_t val;
299         retval = ext2fs_get_free_blocks2(fs, start, finish, num, map, &val);
300         if(!retval)
301                 *ret = (blk_t) val;
302         return retval;
303 }
304
305 void ext2fs_set_alloc_block_callback(ext2_filsys fs,
306                                      errcode_t (*func)(ext2_filsys fs,
307                                                        blk64_t goal,
308                                                        blk64_t *ret),
309                                      errcode_t (**old)(ext2_filsys fs,
310                                                        blk64_t goal,
311                                                        blk64_t *ret))
312 {
313         if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
314                 return;
315
316         if (old)
317                 *old = fs->get_alloc_block;
318
319         fs->get_alloc_block = func;
320 }