blob: 9f3d4e04a98646e268b34238bdf8eb3b533cde35 [file] [log] [blame]
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001/*
2 * alloc_tables.c --- Allocate tables for a newly initialized
3 * filesystem. Used by mke2fs when initializing a filesystem
4 *
5 * Copyright (C) 1996 Theodore Ts'o.
6 *
7 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04008 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
Theodore Ts'o21c84b71997-04-29 16:15:03 +000010 * %End-Header%
11 */
12
Theodore Ts'od1154eb2011-09-18 17:34:37 -040013#include "config.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000014#include <stdio.h>
15#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#if HAVE_UNISTD_H
Theodore Ts'o21c84b71997-04-29 16:15:03 +000017#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000018#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000019#include <fcntl.h>
20#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000021#if HAVE_SYS_STAT_H
Theodore Ts'o21c84b71997-04-29 16:15:03 +000022#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000023#endif
24#if HAVE_SYS_TYPES_H
Theodore Ts'o21c84b71997-04-29 16:15:03 +000025#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000026#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000027
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000028#include "ext2_fs.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000029#include "ext2fs.h"
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -040030#include "ext2fsP.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000031
Theodore Ts'o9ba40002008-04-22 08:27:01 -040032/*
33 * This routine searches for free blocks that can allocate a full
34 * group of bitmaps or inode tables for a flexbg group. Returns the
35 * block number with a correct offset were the bitmaps and inode
36 * tables can be allocated continously and in order.
37 */
Theodore Ts'oda3fc252010-06-13 09:00:00 -040038static blk64_t flexbg_offset(ext2_filsys fs, dgrp_t group, blk64_t start_blk,
Theodore Ts'o25567a72011-07-05 13:42:07 -040039 ext2fs_block_bitmap bmap, int rem_grp,
Theodore Ts'oda3fc252010-06-13 09:00:00 -040040 int elem_size)
Theodore Ts'o9ba40002008-04-22 08:27:01 -040041{
Theodore Ts'o25567a72011-07-05 13:42:07 -040042 int flexbg, flexbg_size, size;
Theodore Ts'oda3fc252010-06-13 09:00:00 -040043 blk64_t last_blk, first_free = 0;
Theodore Ts'o9ba40002008-04-22 08:27:01 -040044 dgrp_t last_grp;
45
Theodore Ts'oc4d2d432011-07-07 13:50:49 -040046 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
47 flexbg = group / flexbg_size;
Theodore Ts'o25567a72011-07-05 13:42:07 -040048 size = rem_grp * elem_size;
Theodore Ts'o9ba40002008-04-22 08:27:01 -040049
Theodore Ts'o8895f432008-06-07 11:53:56 -040050 if (size > (int) (fs->super->s_blocks_per_group / 8))
51 size = (int) fs->super->s_blocks_per_group / 8;
Theodore Ts'o9ba40002008-04-22 08:27:01 -040052
53 /*
Theodore Ts'oba4a0212008-07-07 16:11:11 -040054 * Don't do a long search if the previous block
Theodore Ts'o9ba40002008-04-22 08:27:01 -040055 * search is still valid.
56 */
Theodore Ts'o25567a72011-07-05 13:42:07 -040057 if (start_blk && ext2fs_test_block_bitmap_range2(bmap, start_blk,
58 elem_size))
59 return start_blk;
Theodore Ts'o9ba40002008-04-22 08:27:01 -040060
Theodore Ts'ob49f78f2009-10-25 21:24:06 -040061 start_blk = ext2fs_group_first_block2(fs, flexbg_size * flexbg);
Theodore Ts'o9ba40002008-04-22 08:27:01 -040062 last_grp = group | (flexbg_size - 1);
Theodore Ts'o25567a72011-07-05 13:42:07 -040063 if (last_grp > fs->group_desc_count-1)
64 last_grp = fs->group_desc_count-1;
Theodore Ts'ob49f78f2009-10-25 21:24:06 -040065 last_blk = ext2fs_group_last_block2(fs, last_grp);
Theodore Ts'o9ba40002008-04-22 08:27:01 -040066
67 /* Find the first available block */
Theodore Ts'o25567a72011-07-05 13:42:07 -040068 if (ext2fs_get_free_blocks2(fs, start_blk, last_blk, size,
69 bmap, &first_free) == 0)
Theodore Ts'o9ba40002008-04-22 08:27:01 -040070 return first_free;
71
Theodore Ts'o25567a72011-07-05 13:42:07 -040072 if (ext2fs_get_free_blocks2(fs, start_blk, last_blk, elem_size,
73 bmap, &first_free) == 0)
74 return first_free;
75
76 if (ext2fs_get_free_blocks2(fs, 0, last_blk, elem_size, bmap,
77 &first_free) == 0)
Theodore Ts'o9ba40002008-04-22 08:27:01 -040078 return first_free;
79
80 return first_free;
81}
82
Theodore Ts'o2eb374c1998-09-03 01:22:57 +000083errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
Theodore Ts'o1e1da291997-06-09 14:51:29 +000084 ext2fs_block_bitmap bmap)
Theodore Ts'o21c84b71997-04-29 16:15:03 +000085{
Theodore Ts'od32c9152011-07-07 13:50:22 -040086 unsigned int j;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000087 errcode_t retval;
Theodore Ts'oda3fc252010-06-13 09:00:00 -040088 blk64_t group_blk, start_blk, last_blk, new_blk, blk;
Theodore Ts'o8895f432008-06-07 11:53:56 -040089 dgrp_t last_grp = 0;
Theodore Ts'od32c9152011-07-07 13:50:22 -040090 int rem_grps = 0, flexbg_size = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000091
Theodore Ts'ob49f78f2009-10-25 21:24:06 -040092 group_blk = ext2fs_group_first_block2(fs, group);
93 last_blk = ext2fs_group_last_block2(fs, group);
Theodore Ts'o2ecc6fe1997-04-29 17:57:00 +000094
Theodore Ts'o1e1da291997-06-09 14:51:29 +000095 if (!bmap)
96 bmap = fs->block_map;
Theodore Ts'o9ba40002008-04-22 08:27:01 -040097
98 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
99 EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
100 fs->super->s_log_groups_per_flex) {
101 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
102 last_grp = group | (flexbg_size - 1);
Theodore Ts'o25567a72011-07-05 13:42:07 -0400103 if (last_grp > fs->group_desc_count-1)
104 last_grp = fs->group_desc_count-1;
105 rem_grps = last_grp - group + 1;
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400106 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400107
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000108 /*
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000109 * Allocate the block and inode bitmaps, if necessary
110 */
111 if (fs->stride) {
Theodore Ts'oda3fc252010-06-13 09:00:00 -0400112 retval = ext2fs_get_free_blocks2(fs, group_blk, last_blk,
113 1, bmap, &start_blk);
Theodore Ts'obc2699f2006-05-13 08:46:37 -0400114 if (retval)
115 return retval;
116 start_blk += fs->inode_blocks_per_group;
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000117 start_blk += ((fs->stride * group) %
Eric Sandeenabf23432006-09-12 14:56:16 -0400118 (last_blk - start_blk + 1));
119 if (start_blk >= last_blk)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000120 start_blk = group_blk;
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000121 } else
122 start_blk = group_blk;
123
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400124 if (flexbg_size) {
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400125 blk64_t prev_block = 0;
126
Theodore Ts'o25567a72011-07-05 13:42:07 -0400127 if (group % flexbg_size)
128 prev_block = ext2fs_block_bitmap_loc(fs, group - 1) + 1;
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400129 start_blk = flexbg_offset(fs, group, prev_block, bmap,
Theodore Ts'o25567a72011-07-05 13:42:07 -0400130 rem_grps, 1);
Theodore Ts'ob49f78f2009-10-25 21:24:06 -0400131 last_blk = ext2fs_group_last_block2(fs, last_grp);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400132 }
133
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400134 if (!ext2fs_block_bitmap_loc(fs, group)) {
Theodore Ts'oda3fc252010-06-13 09:00:00 -0400135 retval = ext2fs_get_free_blocks2(fs, start_blk, last_blk,
136 1, bmap, &new_blk);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400137 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
Theodore Ts'oda3fc252010-06-13 09:00:00 -0400138 retval = ext2fs_get_free_blocks2(fs, group_blk,
Theodore Ts'o03673db1998-06-10 20:39:43 +0000139 last_blk, 1, bmap, &new_blk);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000140 if (retval)
141 return retval;
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400142 ext2fs_mark_block_bitmap2(bmap, new_blk);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400143 ext2fs_block_bitmap_loc_set(fs, group, new_blk);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400144 if (flexbg_size) {
Theodore Ts'o6493f8e2009-10-25 20:50:15 -0400145 dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400146 ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400147 ext2fs_free_blocks_count_add(fs->super, -1);
Eric Sandeene633b582009-10-25 21:41:32 -0400148 ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400149 ext2fs_group_desc_csum_set(fs, gr);
150 }
151 }
152
153 if (flexbg_size) {
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400154 blk64_t prev_block = 0;
Theodore Ts'o25567a72011-07-05 13:42:07 -0400155 if (group % flexbg_size)
156 prev_block = ext2fs_inode_bitmap_loc(fs, group - 1) + 1;
157 else
158 prev_block = ext2fs_block_bitmap_loc(fs, group) +
159 flexbg_size;
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400160 start_blk = flexbg_offset(fs, group, prev_block, bmap,
Theodore Ts'o25567a72011-07-05 13:42:07 -0400161 rem_grps, 1);
Theodore Ts'ob49f78f2009-10-25 21:24:06 -0400162 last_blk = ext2fs_group_last_block2(fs, last_grp);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000163 }
Theodore Ts'o521e3681997-04-29 17:48:10 +0000164
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400165 if (!ext2fs_inode_bitmap_loc(fs, group)) {
Theodore Ts'oda3fc252010-06-13 09:00:00 -0400166 retval = ext2fs_get_free_blocks2(fs, start_blk, last_blk,
167 1, bmap, &new_blk);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400168 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
Theodore Ts'oda3fc252010-06-13 09:00:00 -0400169 retval = ext2fs_get_free_blocks2(fs, group_blk,
170 last_blk, 1, bmap, &new_blk);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000171 if (retval)
172 return retval;
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400173 ext2fs_mark_block_bitmap2(bmap, new_blk);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400174 ext2fs_inode_bitmap_loc_set(fs, group, new_blk);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400175 if (flexbg_size) {
Theodore Ts'o6493f8e2009-10-25 20:50:15 -0400176 dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400177 ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400178 ext2fs_free_blocks_count_add(fs->super, -1);
Eric Sandeene633b582009-10-25 21:41:32 -0400179 ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400180 ext2fs_group_desc_csum_set(fs, gr);
181 }
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000182 }
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500183
184 /*
185 * Allocate the inode table
186 */
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400187 if (flexbg_size) {
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400188 blk64_t prev_block = 0;
Theodore Ts'o25567a72011-07-05 13:42:07 -0400189
190 if (group % flexbg_size)
191 prev_block = ext2fs_inode_table_loc(fs, group - 1) +
192 fs->inode_blocks_per_group;
193 else
194 prev_block = ext2fs_inode_bitmap_loc(fs, group) +
195 flexbg_size;
196
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400197 group_blk = flexbg_offset(fs, group, prev_block, bmap,
Theodore Ts'o25567a72011-07-05 13:42:07 -0400198 rem_grps, fs->inode_blocks_per_group);
Theodore Ts'ob49f78f2009-10-25 21:24:06 -0400199 last_blk = ext2fs_group_last_block2(fs, last_grp);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400200 }
201
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400202 if (!ext2fs_inode_table_loc(fs, group)) {
Theodore Ts'oda3fc252010-06-13 09:00:00 -0400203 retval = ext2fs_get_free_blocks2(fs, group_blk, last_blk,
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500204 fs->inode_blocks_per_group,
205 bmap, &new_blk);
206 if (retval)
207 return retval;
208 for (j=0, blk = new_blk;
209 j < fs->inode_blocks_per_group;
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400210 j++, blk++) {
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400211 ext2fs_mark_block_bitmap2(bmap, blk);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400212 if (flexbg_size) {
Theodore Ts'o6493f8e2009-10-25 20:50:15 -0400213 dgrp_t gr = ext2fs_group_of_blk2(fs, blk);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400214 ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400215 ext2fs_free_blocks_count_add(fs->super, -1);
Eric Sandeene633b582009-10-25 21:41:32 -0400216 ext2fs_bg_flags_clear(fs, gr,
Theodore Ts'o732c8cd2009-09-07 21:15:12 -0400217 EXT2_BG_BLOCK_UNINIT);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400218 ext2fs_group_desc_csum_set(fs, gr);
219 }
220 }
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400221 ext2fs_inode_table_loc_set(fs, group, new_blk);
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500222 }
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500223 ext2fs_group_desc_csum_set(fs, group);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000224 return 0;
225}
Theodore Ts'o521e3681997-04-29 17:48:10 +0000226
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000227errcode_t ext2fs_allocate_tables(ext2_filsys fs)
228{
229 errcode_t retval;
Theodore Ts'o2eb374c1998-09-03 01:22:57 +0000230 dgrp_t i;
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400231 struct ext2fs_numeric_progress_struct progress;
232
233 ext2fs_numeric_progress_init(fs, &progress, NULL,
234 fs->group_desc_count);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000235
236 for (i = 0; i < fs->group_desc_count; i++) {
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400237 ext2fs_numeric_progress_update(fs, &progress, i);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000238 retval = ext2fs_allocate_group_table(fs, i, fs->block_map);
239 if (retval)
240 return retval;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000241 }
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400242 ext2fs_numeric_progress_close(fs, &progress, NULL);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000243 return 0;
244}
245