| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | errcode_t ext2fs_allocate_tables(ext2_filsys fs) |
| 30 | { |
| 31 | errcode_t retval; |
| Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 32 | blk_t group_blk, start_blk, last_blk, new_blk, blk; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 33 | int i, j; |
| 34 | |
| 35 | group_blk = fs->super->s_first_data_block; |
| 36 | for (i = 0; i < fs->group_desc_count; i++) { |
| 37 | last_blk = group_blk + fs->super->s_blocks_per_group; |
| Theodore Ts'o | 2ecc6fe | 1997-04-29 17:57:00 +0000 | [diff] [blame] | 38 | if (last_blk >= fs->super->s_blocks_count) |
| 39 | last_blk = fs->super->s_blocks_count - 1; |
| 40 | |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 41 | /* |
| Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 42 | * Allocate the inode table |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 43 | */ |
| Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 44 | start_blk = group_blk + 3 + fs->desc_blocks; |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame^] | 45 | if (start_blk > last_blk) |
| 46 | start_blk = group_blk; |
| Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 47 | retval = ext2fs_get_free_blocks(fs, start_blk, last_blk, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 48 | fs->inode_blocks_per_group, |
| 49 | fs->block_map, &new_blk); |
| 50 | if (retval) |
| 51 | return retval; |
| 52 | for (j=0, blk = new_blk; |
| 53 | j < fs->inode_blocks_per_group; |
| 54 | j++, blk++) |
| 55 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
| 56 | fs->group_desc[i].bg_inode_table = new_blk; |
| 57 | |
| 58 | /* |
| Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 59 | * Allocate the block and inode bitmaps |
| 60 | */ |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame^] | 61 | if (fs->stride) { |
| 62 | start_blk += fs->inode_blocks_per_group; |
| 63 | start_blk += ((fs->stride * i) % |
| 64 | (last_blk - start_blk)); |
| 65 | if (start_blk > last_blk) |
| 66 | /* should never happen */ |
| 67 | start_blk = group_blk; |
| 68 | } else |
| 69 | start_blk = group_blk; |
| Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 70 | retval = ext2fs_get_free_blocks(fs, start_blk, last_blk, |
| 71 | 1, fs->block_map, &new_blk); |
| 72 | if (retval) |
| 73 | return retval; |
| 74 | ext2fs_mark_block_bitmap(fs->block_map, new_blk); |
| 75 | fs->group_desc[i].bg_block_bitmap = new_blk; |
| 76 | |
| 77 | retval = ext2fs_get_free_blocks(fs, start_blk, last_blk, |
| 78 | 1, fs->block_map, &new_blk); |
| 79 | if (retval) |
| 80 | return retval; |
| 81 | ext2fs_mark_block_bitmap(fs->block_map, new_blk); |
| 82 | fs->group_desc[i].bg_inode_bitmap = new_blk; |
| 83 | |
| 84 | /* |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 85 | * Increment the start of the block group |
| 86 | */ |
| 87 | group_blk += fs->super->s_blocks_per_group; |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |