blob: 474a61a2a45b75ad25618bed947e08a04bc94742 [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{
86 errcode_t retval;
Theodore Ts'ofccdbac2014-01-19 16:47:21 -050087 blk64_t group_blk, start_blk, last_blk, new_blk;
Theodore Ts'o8895f432008-06-07 11:53:56 -040088 dgrp_t last_grp = 0;
Theodore Ts'od32c9152011-07-07 13:50:22 -040089 int rem_grps = 0, flexbg_size = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000090
Theodore Ts'ob49f78f2009-10-25 21:24:06 -040091 group_blk = ext2fs_group_first_block2(fs, group);
92 last_blk = ext2fs_group_last_block2(fs, group);
Theodore Ts'o2ecc6fe1997-04-29 17:57:00 +000093
Theodore Ts'o1e1da291997-06-09 14:51:29 +000094 if (!bmap)
95 bmap = fs->block_map;
Theodore Ts'o9ba40002008-04-22 08:27:01 -040096
97 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
98 EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
99 fs->super->s_log_groups_per_flex) {
100 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
101 last_grp = group | (flexbg_size - 1);
Theodore Ts'o25567a72011-07-05 13:42:07 -0400102 if (last_grp > fs->group_desc_count-1)
103 last_grp = fs->group_desc_count-1;
104 rem_grps = last_grp - group + 1;
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400105 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400106
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000107 /*
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000108 * Allocate the block and inode bitmaps, if necessary
109 */
110 if (fs->stride) {
Theodore Ts'oda3fc252010-06-13 09:00:00 -0400111 retval = ext2fs_get_free_blocks2(fs, group_blk, last_blk,
112 1, bmap, &start_blk);
Theodore Ts'obc2699f2006-05-13 08:46:37 -0400113 if (retval)
114 return retval;
115 start_blk += fs->inode_blocks_per_group;
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000116 start_blk += ((fs->stride * group) %
Eric Sandeenabf23432006-09-12 14:56:16 -0400117 (last_blk - start_blk + 1));
118 if (start_blk >= last_blk)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000119 start_blk = group_blk;
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000120 } else
121 start_blk = group_blk;
122
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400123 if (flexbg_size) {
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400124 blk64_t prev_block = 0;
125
Theodore Ts'o25567a72011-07-05 13:42:07 -0400126 if (group % flexbg_size)
127 prev_block = ext2fs_block_bitmap_loc(fs, group - 1) + 1;
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400128 start_blk = flexbg_offset(fs, group, prev_block, bmap,
Theodore Ts'o25567a72011-07-05 13:42:07 -0400129 rem_grps, 1);
Theodore Ts'ob49f78f2009-10-25 21:24:06 -0400130 last_blk = ext2fs_group_last_block2(fs, last_grp);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400131 }
132
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400133 if (!ext2fs_block_bitmap_loc(fs, group)) {
Theodore Ts'oda3fc252010-06-13 09:00:00 -0400134 retval = ext2fs_get_free_blocks2(fs, start_blk, last_blk,
135 1, bmap, &new_blk);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400136 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
Theodore Ts'oda3fc252010-06-13 09:00:00 -0400137 retval = ext2fs_get_free_blocks2(fs, group_blk,
Theodore Ts'o03673db1998-06-10 20:39:43 +0000138 last_blk, 1, bmap, &new_blk);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000139 if (retval)
140 return retval;
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400141 ext2fs_mark_block_bitmap2(bmap, new_blk);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400142 ext2fs_block_bitmap_loc_set(fs, group, new_blk);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400143 if (flexbg_size) {
Theodore Ts'o6493f8e2009-10-25 20:50:15 -0400144 dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400145 ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400146 ext2fs_free_blocks_count_add(fs->super, -1);
Eric Sandeene633b582009-10-25 21:41:32 -0400147 ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400148 ext2fs_group_desc_csum_set(fs, gr);
149 }
150 }
151
152 if (flexbg_size) {
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400153 blk64_t prev_block = 0;
Theodore Ts'o25567a72011-07-05 13:42:07 -0400154 if (group % flexbg_size)
155 prev_block = ext2fs_inode_bitmap_loc(fs, group - 1) + 1;
156 else
157 prev_block = ext2fs_block_bitmap_loc(fs, group) +
158 flexbg_size;
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400159 start_blk = flexbg_offset(fs, group, prev_block, bmap,
Theodore Ts'o25567a72011-07-05 13:42:07 -0400160 rem_grps, 1);
Theodore Ts'ob49f78f2009-10-25 21:24:06 -0400161 last_blk = ext2fs_group_last_block2(fs, last_grp);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000162 }
Theodore Ts'o521e3681997-04-29 17:48:10 +0000163
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400164 if (!ext2fs_inode_bitmap_loc(fs, group)) {
Theodore Ts'oda3fc252010-06-13 09:00:00 -0400165 retval = ext2fs_get_free_blocks2(fs, start_blk, last_blk,
166 1, bmap, &new_blk);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400167 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
Theodore Ts'oda3fc252010-06-13 09:00:00 -0400168 retval = ext2fs_get_free_blocks2(fs, group_blk,
169 last_blk, 1, bmap, &new_blk);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000170 if (retval)
171 return retval;
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400172 ext2fs_mark_block_bitmap2(bmap, new_blk);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400173 ext2fs_inode_bitmap_loc_set(fs, group, new_blk);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400174 if (flexbg_size) {
Theodore Ts'o6493f8e2009-10-25 20:50:15 -0400175 dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400176 ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400177 ext2fs_free_blocks_count_add(fs->super, -1);
Eric Sandeene633b582009-10-25 21:41:32 -0400178 ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400179 ext2fs_group_desc_csum_set(fs, gr);
180 }
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000181 }
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500182
183 /*
184 * Allocate the inode table
185 */
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400186 if (flexbg_size) {
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400187 blk64_t prev_block = 0;
Theodore Ts'o25567a72011-07-05 13:42:07 -0400188
189 if (group % flexbg_size)
190 prev_block = ext2fs_inode_table_loc(fs, group - 1) +
191 fs->inode_blocks_per_group;
192 else
193 prev_block = ext2fs_inode_bitmap_loc(fs, group) +
194 flexbg_size;
195
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400196 group_blk = flexbg_offset(fs, group, prev_block, bmap,
Theodore Ts'o25567a72011-07-05 13:42:07 -0400197 rem_grps, fs->inode_blocks_per_group);
Theodore Ts'ob49f78f2009-10-25 21:24:06 -0400198 last_blk = ext2fs_group_last_block2(fs, last_grp);
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400199 }
200
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400201 if (!ext2fs_inode_table_loc(fs, group)) {
Theodore Ts'oda3fc252010-06-13 09:00:00 -0400202 retval = ext2fs_get_free_blocks2(fs, group_blk, last_blk,
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500203 fs->inode_blocks_per_group,
204 bmap, &new_blk);
205 if (retval)
206 return retval;
Theodore Ts'ofccdbac2014-01-19 16:47:21 -0500207 if (flexbg_size)
208 ext2fs_block_alloc_stats_range(fs, new_blk,
209 fs->inode_blocks_per_group, +1);
210 else
211 ext2fs_mark_block_bitmap_range2(fs->block_map,
212 new_blk, fs->inode_blocks_per_group);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400213 ext2fs_inode_table_loc_set(fs, group, new_blk);
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500214 }
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500215 ext2fs_group_desc_csum_set(fs, group);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000216 return 0;
217}
Theodore Ts'o521e3681997-04-29 17:48:10 +0000218
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000219errcode_t ext2fs_allocate_tables(ext2_filsys fs)
220{
221 errcode_t retval;
Theodore Ts'o2eb374c1998-09-03 01:22:57 +0000222 dgrp_t i;
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400223 struct ext2fs_numeric_progress_struct progress;
224
Theodore Ts'odfe74c52012-07-30 17:16:51 -0400225 if (fs->progress_ops && fs->progress_ops->init)
226 (fs->progress_ops->init)(fs, &progress, NULL,
227 fs->group_desc_count);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000228
229 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'odfe74c52012-07-30 17:16:51 -0400230 if (fs->progress_ops && fs->progress_ops->update)
231 (fs->progress_ops->update)(fs, &progress, i);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000232 retval = ext2fs_allocate_group_table(fs, i, fs->block_map);
233 if (retval)
234 return retval;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000235 }
Theodore Ts'odfe74c52012-07-30 17:16:51 -0400236 if (fs->progress_ops && fs->progress_ops->close)
237 (fs->progress_ops->close)(fs, &progress, NULL);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000238 return 0;
239}
240