Whamcloud - gitweb
Add 64-bit getsize interface.
[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 Public
8  * License.
9  * %End-Header%
10  *
11  */
12
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         blk_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             !(fs->group_desc[group].bg_flags & 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_loc(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                 if ((blk == super_blk) ||
58                     (old_desc_blk && old_desc_blocks &&
59                      (blk >= old_desc_blk) &&
60                      (blk < old_desc_blk + old_desc_blocks)) ||
61                     (new_desc_blk && (blk == new_desc_blk)) ||
62                     (blk == fs->group_desc[group].bg_block_bitmap) ||
63                     (blk == fs->group_desc[group].bg_inode_bitmap) ||
64                     (blk >= fs->group_desc[group].bg_inode_table &&
65                      (blk < fs->group_desc[group].bg_inode_table
66                       + fs->inode_blocks_per_group)))
67                         ext2fs_fast_mark_block_bitmap(map, blk);
68                 else
69                         ext2fs_fast_unmark_block_bitmap(map, blk);
70         }
71         fs->group_desc[group].bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
72         ext2fs_group_desc_csum_set(fs, group);
73 }
74
75 /*
76  * Check for uninit inode bitmaps and deal with them appropriately
77  */
78 static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
79                           dgrp_t group)
80 {
81         ext2_ino_t      i, ino;
82
83         if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
84                                          EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
85             !(fs->group_desc[group].bg_flags & EXT2_BG_INODE_UNINIT))
86                 return;
87
88         ino = (group * fs->super->s_inodes_per_group) + 1;
89         for (i=0; i < fs->super->s_inodes_per_group; i++, ino++)
90                 ext2fs_fast_unmark_inode_bitmap(map, ino);
91
92         fs->group_desc[group].bg_flags &= ~EXT2_BG_INODE_UNINIT;
93         check_block_uninit(fs, fs->block_map, group);
94 }
95
96 /*
97  * Right now, just search forward from the parent directory's block
98  * group to find the next free inode.
99  *
100  * Should have a special policy for directories.
101  */
102 errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
103                            int mode EXT2FS_ATTR((unused)),
104                            ext2fs_inode_bitmap map, ext2_ino_t *ret)
105 {
106         ext2_ino_t      dir_group = 0;
107         ext2_ino_t      i;
108         ext2_ino_t      start_inode;
109
110         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
111
112         if (!map)
113                 map = fs->inode_map;
114         if (!map)
115                 return EXT2_ET_NO_INODE_BITMAP;
116
117         if (dir > 0)
118                 dir_group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
119
120         start_inode = (dir_group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
121         if (start_inode < EXT2_FIRST_INODE(fs->super))
122                 start_inode = EXT2_FIRST_INODE(fs->super);
123         i = start_inode;
124
125         do {
126                 if (((i - 1) % EXT2_INODES_PER_GROUP(fs->super)) == 0)
127                         check_inode_uninit(fs, map, (i - 1) /
128                                            EXT2_INODES_PER_GROUP(fs->super));
129
130                 if (!ext2fs_fast_test_inode_bitmap(map, i))
131                         break;
132                 i++;
133                 if (i > fs->super->s_inodes_count)
134                         i = EXT2_FIRST_INODE(fs->super);
135         } while (i != start_inode);
136
137         if (ext2fs_test_inode_bitmap(map, i))
138                 return EXT2_ET_INODE_ALLOC_FAIL;
139         *ret = i;
140         return 0;
141 }
142
143 /*
144  * Stupid algorithm --- we now just search forward starting from the
145  * goal.  Should put in a smarter one someday....
146  */
147 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
148                            ext2fs_block_bitmap map, blk_t *ret)
149 {
150         blk_t   i;
151
152         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
153
154         if (!map)
155                 map = fs->block_map;
156         if (!map)
157                 return EXT2_ET_NO_BLOCK_BITMAP;
158         if (!goal || (goal >= fs->super->s_blocks_count))
159                 goal = fs->super->s_first_data_block;
160         i = goal;
161         check_block_uninit(fs, map,
162                            (i - fs->super->s_first_data_block) /
163                            EXT2_BLOCKS_PER_GROUP(fs->super));
164         do {
165                 if (((i - fs->super->s_first_data_block) %
166                      EXT2_BLOCKS_PER_GROUP(fs->super)) == 0)
167                         check_block_uninit(fs, map,
168                                            (i - fs->super->s_first_data_block) /
169                                            EXT2_BLOCKS_PER_GROUP(fs->super));
170
171                 if (!ext2fs_fast_test_block_bitmap(map, i)) {
172                         *ret = i;
173                         return 0;
174                 }
175                 i++;
176                 if (i >= fs->super->s_blocks_count)
177                         i = fs->super->s_first_data_block;
178         } while (i != goal);
179         return EXT2_ET_BLOCK_ALLOC_FAIL;
180 }
181
182 /*
183  * This function zeros out the allocated block, and updates all of the
184  * appropriate filesystem records.
185  */
186 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
187                              char *block_buf, blk_t *ret)
188 {
189         errcode_t       retval;
190         blk_t           block;
191         char            *buf = 0;
192
193         if (!block_buf) {
194                 retval = ext2fs_get_mem(fs->blocksize, &buf);
195                 if (retval)
196                         return retval;
197                 block_buf = buf;
198         }
199         memset(block_buf, 0, fs->blocksize);
200
201         if (fs->get_alloc_block) {
202                 blk64_t new;
203
204                 retval = (fs->get_alloc_block)(fs, (blk64_t) goal, &new);
205                 if (retval)
206                         goto fail;
207                 block = (blk_t) new;
208         } else {
209                 if (!fs->block_map) {
210                         retval = ext2fs_read_block_bitmap(fs);
211                         if (retval)
212                                 goto fail;
213                 }
214
215                 retval = ext2fs_new_block(fs, goal, 0, &block);
216                 if (retval)
217                         goto fail;
218         }
219
220         retval = io_channel_write_blk(fs->io, block, 1, block_buf);
221         if (retval)
222                 goto fail;
223
224         ext2fs_block_alloc_stats(fs, block, +1);
225         *ret = block;
226
227 fail:
228         if (buf)
229                 ext2fs_free_mem(&buf);
230         return retval;
231 }
232
233 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
234                                  int num, ext2fs_block_bitmap map, blk_t *ret)
235 {
236         blk_t   b = start;
237
238         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
239
240         if (!map)
241                 map = fs->block_map;
242         if (!map)
243                 return EXT2_ET_NO_BLOCK_BITMAP;
244         if (!b)
245                 b = fs->super->s_first_data_block;
246         if (!finish)
247                 finish = start;
248         if (!num)
249                 num = 1;
250         do {
251                 if (b+num-1 > fs->super->s_blocks_count)
252                         b = fs->super->s_first_data_block;
253                 if (ext2fs_fast_test_block_bitmap_range(map, b, num)) {
254                         *ret = b;
255                         return 0;
256                 }
257                 b++;
258         } while (b != finish);
259         return EXT2_ET_BLOCK_ALLOC_FAIL;
260 }
261
262 void ext2fs_set_alloc_block_callback(ext2_filsys fs,
263                                      errcode_t (*func)(ext2_filsys fs,
264                                                        blk64_t goal,
265                                                        blk64_t *ret),
266                                      errcode_t (**old)(ext2_filsys fs,
267                                                        blk64_t goal,
268                                                        blk64_t *ret))
269 {
270         if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
271                 return;
272
273         if (old)
274                 *old = fs->get_alloc_block;
275
276         fs->get_alloc_block = func;
277 }