blob: 0c829edb02688132c2ab5a934cb5a4cc86230fb9 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * alloc.c --- allocate new inodes, blocks for ext2fs
3 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o21c84b71997-04-29 16:15:03 +00009 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000013#include <stdio.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000017#include <time.h>
Theodore Ts'o30fab291997-10-25 22:37:42 +000018#include <string.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000019#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000021#endif
22#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000024#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000025
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000026#include "ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include "ext2fs.h"
28
29/*
Theodore Ts'oc71d7812008-08-22 02:52:12 -040030 * Check for uninit block bitmaps and deal with them appropriately
31 */
Theodore Ts'o03fa6f82008-11-16 10:03:00 -050032static void check_block_uninit(ext2_filsys fs, ext2fs_block_bitmap map,
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -040033 dgrp_t group)
Theodore Ts'oc71d7812008-08-22 02:52:12 -040034{
Theodore Ts'o03fa6f82008-11-16 10:03:00 -050035 blk_t i;
Jose R. Santos20f2ccb2009-07-11 21:29:30 -040036 blk64_t blk, super_blk, old_desc_blk, new_desc_blk;
Theodore Ts'oc71d7812008-08-22 02:52:12 -040037 int old_desc_blocks;
38
39 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
40 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
Theodore Ts'ocd65a242009-10-25 21:42:12 -040041 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)))
Theodore Ts'oc71d7812008-08-22 02:52:12 -040042 return;
43
Theodore Ts'o027b0572013-01-03 13:42:38 -050044 blk = ext2fs_group_first_block2(fs, group);
Theodore Ts'oc71d7812008-08-22 02:52:12 -040045
Jose R. Santos20f2ccb2009-07-11 21:29:30 -040046 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
47 &old_desc_blk, &new_desc_blk, 0);
Theodore Ts'oc71d7812008-08-22 02:52:12 -040048
49 if (fs->super->s_feature_incompat &
50 EXT2_FEATURE_INCOMPAT_META_BG)
51 old_desc_blocks = fs->super->s_first_meta_bg;
52 else
53 old_desc_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
54
Theodore Ts'ob0ecb782011-06-10 18:51:58 -040055 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++)
56 ext2fs_fast_unmark_block_bitmap2(map, blk);
57
Theodore Ts'o027b0572013-01-03 13:42:38 -050058 blk = ext2fs_group_first_block2(fs, group);
Theodore Ts'oc71d7812008-08-22 02:52:12 -040059 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
60 if ((blk == super_blk) ||
61 (old_desc_blk && old_desc_blocks &&
62 (blk >= old_desc_blk) &&
63 (blk < old_desc_blk + old_desc_blocks)) ||
64 (new_desc_blk && (blk == new_desc_blk)) ||
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -040065 (blk == ext2fs_block_bitmap_loc(fs, group)) ||
66 (blk == ext2fs_inode_bitmap_loc(fs, group)) ||
67 (blk >= ext2fs_inode_table_loc(fs, group) &&
68 (blk < ext2fs_inode_table_loc(fs, group)
Theodore Ts'oc71d7812008-08-22 02:52:12 -040069 + fs->inode_blocks_per_group)))
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -040070 ext2fs_fast_mark_block_bitmap2(map, blk);
Theodore Ts'oc71d7812008-08-22 02:52:12 -040071 }
Eric Sandeene633b582009-10-25 21:41:32 -040072 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
Theodore Ts'oc71d7812008-08-22 02:52:12 -040073 ext2fs_group_desc_csum_set(fs, group);
Theodore Ts'o538eb872012-03-25 22:24:10 -040074 ext2fs_mark_super_dirty(fs);
75 ext2fs_mark_bb_dirty(fs);
Theodore Ts'oc71d7812008-08-22 02:52:12 -040076}
77
78/*
79 * Check for uninit inode bitmaps and deal with them appropriately
80 */
Theodore Ts'o03fa6f82008-11-16 10:03:00 -050081static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
Theodore Ts'oc71d7812008-08-22 02:52:12 -040082 dgrp_t group)
83{
Theodore Ts'o03fa6f82008-11-16 10:03:00 -050084 ext2_ino_t i, ino;
Theodore Ts'oc71d7812008-08-22 02:52:12 -040085
86 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
87 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
Theodore Ts'ocd65a242009-10-25 21:42:12 -040088 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
Theodore Ts'oc71d7812008-08-22 02:52:12 -040089 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++)
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -040093 ext2fs_fast_unmark_inode_bitmap2(map, ino);
Theodore Ts'oc71d7812008-08-22 02:52:12 -040094
Eric Sandeene633b582009-10-25 21:41:32 -040095 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
Theodore Ts'o538eb872012-03-25 22:24:10 -040096 ext2fs_group_desc_csum_set(fs, group);
97 ext2fs_mark_ib_dirty(fs);
98 ext2fs_mark_super_dirty(fs);
Theodore Ts'oc71d7812008-08-22 02:52:12 -040099 check_block_uninit(fs, fs->block_map, group);
100}
101
102/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 * Right now, just search forward from the parent directory's block
104 * group to find the next free inode.
105 *
106 * Should have a special policy for directories.
107 */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400108errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
Theodore Ts'o54434922003-12-07 01:28:50 -0500109 int mode EXT2FS_ATTR((unused)),
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000110 ext2fs_inode_bitmap map, ext2_ino_t *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111{
Sami Liedesc1a1e7f2012-03-10 22:36:12 +0200112 ext2_ino_t start_inode = 0;
113 ext2_ino_t i, ino_in_group, upto, first_zero;
114 errcode_t retval;
115 dgrp_t group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116
Theodore Ts'of3db3561997-04-26 13:34:30 +0000117 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400118
Theodore Ts'o3839e651997-04-26 13:21:57 +0000119 if (!map)
120 map = fs->inode_map;
121 if (!map)
122 return EXT2_ET_NO_INODE_BITMAP;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400123
Sami Liedesc1a1e7f2012-03-10 22:36:12 +0200124 if (dir > 0) {
125 group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
126 start_inode = (group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
127 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000128 if (start_inode < EXT2_FIRST_INODE(fs->super))
129 start_inode = EXT2_FIRST_INODE(fs->super);
Theodore Ts'oa93d4062009-01-22 15:55:49 -0500130 if (start_inode > fs->super->s_inodes_count)
131 return EXT2_ET_INODE_ALLOC_FAIL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000132 i = start_inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000133 do {
Sami Liedesc1a1e7f2012-03-10 22:36:12 +0200134 ino_in_group = (i - 1) % EXT2_INODES_PER_GROUP(fs->super);
135 group = (i - 1) / EXT2_INODES_PER_GROUP(fs->super);
Theodore Ts'oc71d7812008-08-22 02:52:12 -0400136
Sami Liedesc1a1e7f2012-03-10 22:36:12 +0200137 check_inode_uninit(fs, map, group);
138 upto = i + (EXT2_INODES_PER_GROUP(fs->super) - ino_in_group);
139 if (i < start_inode && upto >= start_inode)
140 upto = start_inode - 1;
141 if (upto > fs->super->s_inodes_count)
142 upto = fs->super->s_inodes_count;
143
144 retval = ext2fs_find_first_zero_inode_bitmap2(map, i, upto,
145 &first_zero);
146 if (retval == 0) {
147 i = first_zero;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000148 break;
Sami Liedes75556772012-03-22 19:42:38 -0400149 }
Sami Liedesc1a1e7f2012-03-10 22:36:12 +0200150 if (retval != ENOENT)
151 return EXT2_ET_INODE_ALLOC_FAIL;
152 i = upto + 1;
153 if (i > fs->super->s_inodes_count)
154 i = EXT2_FIRST_INODE(fs->super);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155 } while (i != start_inode);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400156
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400157 if (ext2fs_test_inode_bitmap2(map, i))
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000158 return EXT2_ET_INODE_ALLOC_FAIL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000159 *ret = i;
160 return 0;
161}
162
163/*
164 * Stupid algorithm --- we now just search forward starting from the
165 * goal. Should put in a smarter one someday....
166 */
Jose R. Santos8a5e1812009-06-01 16:15:40 -0400167errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
168 ext2fs_block_bitmap map, blk64_t *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169{
Jose R. Santos8a5e1812009-06-01 16:15:40 -0400170 blk64_t i;
Theodore Ts'ob0ecb782011-06-10 18:51:58 -0400171 int c_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172
Theodore Ts'of3db3561997-04-26 13:34:30 +0000173 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
174
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175 if (!map)
176 map = fs->block_map;
177 if (!map)
178 return EXT2_ET_NO_BLOCK_BITMAP;
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400179 if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000180 goal = fs->super->s_first_data_block;
181 i = goal;
Theodore Ts'ob0ecb782011-06-10 18:51:58 -0400182 c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
Theodore Ts'o81c63772011-07-07 22:29:00 -0400183 if (c_ratio > 1)
184 goal &= ~EXT2FS_CLUSTER_MASK(fs);
Theodore Ts'oc71d7812008-08-22 02:52:12 -0400185 check_block_uninit(fs, map,
186 (i - fs->super->s_first_data_block) /
187 EXT2_BLOCKS_PER_GROUP(fs->super));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000188 do {
Theodore Ts'oc71d7812008-08-22 02:52:12 -0400189 if (((i - fs->super->s_first_data_block) %
190 EXT2_BLOCKS_PER_GROUP(fs->super)) == 0)
191 check_block_uninit(fs, map,
192 (i - fs->super->s_first_data_block) /
193 EXT2_BLOCKS_PER_GROUP(fs->super));
194
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400195 if (!ext2fs_fast_test_block_bitmap2(map, i)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000196 *ret = i;
197 return 0;
198 }
Theodore Ts'ob0ecb782011-06-10 18:51:58 -0400199 i = (i + c_ratio) & ~(c_ratio - 1);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400200 if (i >= ext2fs_blocks_count(fs->super))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000201 i = fs->super->s_first_data_block;
202 } while (i != goal);
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000203 return EXT2_ET_BLOCK_ALLOC_FAIL;
Theodore Ts'oc555aeb1997-10-25 04:16:53 +0000204}
205
Jose R. Santos8a5e1812009-06-01 16:15:40 -0400206errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
207 ext2fs_block_bitmap map, blk_t *ret)
208{
209 errcode_t retval;
210 blk64_t val;
211 retval = ext2fs_new_block2(fs, goal, map, &val);
212 if (!retval)
213 *ret = (blk_t) val;
214 return retval;
215}
216
Theodore Ts'oc555aeb1997-10-25 04:16:53 +0000217/*
Theodore Ts'o30fab291997-10-25 22:37:42 +0000218 * This function zeros out the allocated block, and updates all of the
219 * appropriate filesystem records.
Theodore Ts'oc555aeb1997-10-25 04:16:53 +0000220 */
Jose R. Santos8a5e1812009-06-01 16:15:40 -0400221errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
222 char *block_buf, blk64_t *ret)
Theodore Ts'oc555aeb1997-10-25 04:16:53 +0000223{
224 errcode_t retval;
Jose R. Santos8a5e1812009-06-01 16:15:40 -0400225 blk64_t block;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000226 char *buf = 0;
Theodore Ts'oc555aeb1997-10-25 04:16:53 +0000227
Theodore Ts'o30fab291997-10-25 22:37:42 +0000228 if (!block_buf) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400229 retval = ext2fs_get_mem(fs->blocksize, &buf);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000230 if (retval)
231 return retval;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000232 block_buf = buf;
233 }
234 memset(block_buf, 0, fs->blocksize);
Theodore Ts'oc555aeb1997-10-25 04:16:53 +0000235
Theodore Ts'of5c562e2008-06-02 17:21:37 -0400236 if (fs->get_alloc_block) {
Jose R. Santos8a5e1812009-06-01 16:15:40 -0400237 retval = (fs->get_alloc_block)(fs, goal, &block);
Theodore Ts'of5c562e2008-06-02 17:21:37 -0400238 if (retval)
239 goto fail;
Theodore Ts'of5c562e2008-06-02 17:21:37 -0400240 } else {
241 if (!fs->block_map) {
242 retval = ext2fs_read_block_bitmap(fs);
243 if (retval)
244 goto fail;
245 }
246
Jose R. Santos8a5e1812009-06-01 16:15:40 -0400247 retval = ext2fs_new_block2(fs, goal, 0, &block);
Theodore Ts'o30fab291997-10-25 22:37:42 +0000248 if (retval)
249 goto fail;
250 }
251
Jose R. Santos8a5e1812009-06-01 16:15:40 -0400252 retval = io_channel_write_blk64(fs->io, block, 1, block_buf);
Theodore Ts'o30fab291997-10-25 22:37:42 +0000253 if (retval)
254 goto fail;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400255
Jose R. Santos8a5e1812009-06-01 16:15:40 -0400256 ext2fs_block_alloc_stats2(fs, block, +1);
Theodore Ts'o30fab291997-10-25 22:37:42 +0000257 *ret = block;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000258
259fail:
260 if (buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400261 ext2fs_free_mem(&buf);
Theodore Ts'o30fab291997-10-25 22:37:42 +0000262 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000263}
264
Jose R. Santos8a5e1812009-06-01 16:15:40 -0400265errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
266 char *block_buf, blk_t *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000267{
Jose R. Santos8a5e1812009-06-01 16:15:40 -0400268 errcode_t retval;
269 blk64_t val;
270 retval = ext2fs_alloc_block2(fs, goal, block_buf, &val);
271 if (!retval)
272 *ret = (blk_t) val;
273 return retval;
274}
275
276errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
277 int num, ext2fs_block_bitmap map, blk64_t *ret)
278{
Andreas Dilger96367ad2011-06-15 22:17:38 -0400279 blk64_t b = start;
Theodore Ts'ob0ecb782011-06-10 18:51:58 -0400280 int c_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000281
Theodore Ts'of3db3561997-04-26 13:34:30 +0000282 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
283
Theodore Ts'o3839e651997-04-26 13:21:57 +0000284 if (!map)
285 map = fs->block_map;
286 if (!map)
287 return EXT2_ET_NO_BLOCK_BITMAP;
288 if (!b)
289 b = fs->super->s_first_data_block;
290 if (!finish)
291 finish = start;
292 if (!num)
293 num = 1;
Theodore Ts'ob0ecb782011-06-10 18:51:58 -0400294 c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
295 b &= ~(c_ratio - 1);
296 finish &= ~(c_ratio -1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000297 do {
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400298 if (b+num-1 > ext2fs_blocks_count(fs->super))
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000299 b = fs->super->s_first_data_block;
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400300 if (ext2fs_fast_test_block_bitmap_range2(map, b, num)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301 *ret = b;
302 return 0;
303 }
Theodore Ts'ob0ecb782011-06-10 18:51:58 -0400304 b += c_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 } while (b != finish);
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000306 return EXT2_ET_BLOCK_ALLOC_FAIL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307}
308
Jose R. Santos8a5e1812009-06-01 16:15:40 -0400309errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
310 int num, ext2fs_block_bitmap map, blk_t *ret)
311{
312 errcode_t retval;
313 blk64_t val;
314 retval = ext2fs_get_free_blocks2(fs, start, finish, num, map, &val);
315 if(!retval)
316 *ret = (blk_t) val;
317 return retval;
318}
319
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400320void ext2fs_set_alloc_block_callback(ext2_filsys fs,
Theodore Ts'of5c562e2008-06-02 17:21:37 -0400321 errcode_t (*func)(ext2_filsys fs,
322 blk64_t goal,
323 blk64_t *ret),
324 errcode_t (**old)(ext2_filsys fs,
325 blk64_t goal,
326 blk64_t *ret))
327{
328 if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
329 return;
330
331 if (old)
332 *old = fs->get_alloc_block;
333
334 fs->get_alloc_block = func;
335}