blob: 1c885571f9fc550e241a6af67ebecfc762bf6e74 [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%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
11 */
12
13#include <stdio.h>
14#include <string.h>
15#include <unistd.h>
16#include <stdlib.h>
17#include <fcntl.h>
18#include <time.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#if HAVE_ERRNO_H
22#include <errno.h>
23#endif
24
25#include <linux/ext2_fs.h>
26
27#include "ext2fs.h"
28
Theodore Ts'o1e1da291997-06-09 14:51:29 +000029errcode_t ext2fs_allocate_group_table(ext2_filsys fs, int group,
30 ext2fs_block_bitmap bmap)
Theodore Ts'o21c84b71997-04-29 16:15:03 +000031{
32 errcode_t retval;
Theodore Ts'o521e3681997-04-29 17:48:10 +000033 blk_t group_blk, start_blk, last_blk, new_blk, blk;
Theodore Ts'o1e1da291997-06-09 14:51:29 +000034 int j;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000035
Theodore Ts'o1e1da291997-06-09 14:51:29 +000036 group_blk = fs->super->s_first_data_block +
37 (group * fs->super->s_blocks_per_group);
38
39 last_blk = group_blk + fs->super->s_blocks_per_group;
40 if (last_blk >= fs->super->s_blocks_count)
41 last_blk = fs->super->s_blocks_count - 1;
Theodore Ts'o2ecc6fe1997-04-29 17:57:00 +000042
Theodore Ts'o1e1da291997-06-09 14:51:29 +000043 start_blk = group_blk + 3 + fs->desc_blocks;
44 if (start_blk > last_blk)
45 start_blk = group_blk;
46
47 if (!bmap)
48 bmap = fs->block_map;
49
50 /*
51 * Allocate the inode table
52 */
53 if (!fs->group_desc[group].bg_inode_table) {
Theodore Ts'o521e3681997-04-29 17:48:10 +000054 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000055 fs->inode_blocks_per_group,
Theodore Ts'o1e1da291997-06-09 14:51:29 +000056 bmap, &new_blk);
Theodore Ts'o21c84b71997-04-29 16:15:03 +000057 if (retval)
58 return retval;
59 for (j=0, blk = new_blk;
60 j < fs->inode_blocks_per_group;
61 j++, blk++)
Theodore Ts'o1e1da291997-06-09 14:51:29 +000062 ext2fs_mark_block_bitmap(bmap, blk);
63 fs->group_desc[group].bg_inode_table = new_blk;
64 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +000065
Theodore Ts'o1e1da291997-06-09 14:51:29 +000066 /*
67 * Allocate the block and inode bitmaps, if necessary
68 */
69 if (fs->stride) {
70 start_blk += fs->inode_blocks_per_group;
71 start_blk += ((fs->stride * group) %
72 (last_blk - start_blk));
73 if (start_blk > last_blk)
74 /* should never happen */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000075 start_blk = group_blk;
Theodore Ts'o1e1da291997-06-09 14:51:29 +000076 } else
77 start_blk = group_blk;
78
79 if (!fs->group_desc[group].bg_block_bitmap) {
Theodore Ts'o521e3681997-04-29 17:48:10 +000080 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
Theodore Ts'o1e1da291997-06-09 14:51:29 +000081 1, bmap, &new_blk);
Theodore Ts'o521e3681997-04-29 17:48:10 +000082 if (retval)
83 return retval;
Theodore Ts'o1e1da291997-06-09 14:51:29 +000084 ext2fs_mark_block_bitmap(bmap, new_blk);
85 fs->group_desc[group].bg_block_bitmap = new_blk;
86 }
Theodore Ts'o521e3681997-04-29 17:48:10 +000087
Theodore Ts'o1e1da291997-06-09 14:51:29 +000088 if (!fs->group_desc[group].bg_inode_bitmap) {
Theodore Ts'o521e3681997-04-29 17:48:10 +000089 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
Theodore Ts'o1e1da291997-06-09 14:51:29 +000090 1, bmap, &new_blk);
Theodore Ts'o521e3681997-04-29 17:48:10 +000091 if (retval)
92 return retval;
Theodore Ts'o1e1da291997-06-09 14:51:29 +000093 ext2fs_mark_block_bitmap(bmap, new_blk);
94 fs->group_desc[group].bg_inode_bitmap = new_blk;
95 }
96 return 0;
97}
Theodore Ts'o521e3681997-04-29 17:48:10 +000098
Theodore Ts'o1e1da291997-06-09 14:51:29 +000099
100
101errcode_t ext2fs_allocate_tables(ext2_filsys fs)
102{
103 errcode_t retval;
104 int i;
105
106 for (i = 0; i < fs->group_desc_count; i++) {
107 retval = ext2fs_allocate_group_table(fs, i, fs->block_map);
108 if (retval)
109 return retval;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000110 }
111 return 0;
112}
113