3
0

alloc_tables.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * alloc_tables.c --- Allocate tables for a newly initialized
  4. * filesystem. Used by mke2fs when initializing a filesystem
  5. *
  6. * Copyright (C) 1996 Theodore Ts'o.
  7. *
  8. * %Begin-Header%
  9. * This file may be redistributed under the terms of the GNU Public
  10. * License.
  11. * %End-Header%
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #if HAVE_UNISTD_H
  16. #include <unistd.h>
  17. #endif
  18. #include <fcntl.h>
  19. #include <time.h>
  20. #if HAVE_SYS_STAT_H
  21. #include <sys/stat.h>
  22. #endif
  23. #if HAVE_SYS_TYPES_H
  24. #include <sys/types.h>
  25. #endif
  26. #include "ext2_fs.h"
  27. #include "ext2fs.h"
  28. errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
  29. ext2fs_block_bitmap bmap)
  30. {
  31. errcode_t retval;
  32. blk_t group_blk, start_blk, last_blk, new_blk, blk;
  33. int j;
  34. group_blk = fs->super->s_first_data_block +
  35. (group * fs->super->s_blocks_per_group);
  36. last_blk = group_blk + fs->super->s_blocks_per_group;
  37. if (last_blk >= fs->super->s_blocks_count)
  38. last_blk = fs->super->s_blocks_count - 1;
  39. if (!bmap)
  40. bmap = fs->block_map;
  41. /*
  42. * Allocate the block and inode bitmaps, if necessary
  43. */
  44. if (fs->stride) {
  45. start_blk = group_blk + fs->inode_blocks_per_group;
  46. start_blk += ((fs->stride * group) %
  47. (last_blk - start_blk));
  48. if (start_blk > last_blk)
  49. start_blk = group_blk;
  50. } else
  51. start_blk = group_blk;
  52. if (!fs->group_desc[group].bg_block_bitmap) {
  53. retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
  54. 1, bmap, &new_blk);
  55. if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
  56. retval = ext2fs_get_free_blocks(fs, group_blk,
  57. last_blk, 1, bmap, &new_blk);
  58. if (retval)
  59. return retval;
  60. ext2fs_mark_block_bitmap(bmap, new_blk);
  61. fs->group_desc[group].bg_block_bitmap = new_blk;
  62. }
  63. if (!fs->group_desc[group].bg_inode_bitmap) {
  64. retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
  65. 1, bmap, &new_blk);
  66. if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
  67. retval = ext2fs_get_free_blocks(fs, group_blk,
  68. last_blk, 1, bmap, &new_blk);
  69. if (retval)
  70. return retval;
  71. ext2fs_mark_block_bitmap(bmap, new_blk);
  72. fs->group_desc[group].bg_inode_bitmap = new_blk;
  73. }
  74. /*
  75. * Allocate the inode table
  76. */
  77. if (!fs->group_desc[group].bg_inode_table) {
  78. retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
  79. fs->inode_blocks_per_group,
  80. bmap, &new_blk);
  81. if (retval)
  82. return retval;
  83. for (j=0, blk = new_blk;
  84. j < fs->inode_blocks_per_group;
  85. j++, blk++)
  86. ext2fs_mark_block_bitmap(bmap, blk);
  87. fs->group_desc[group].bg_inode_table = new_blk;
  88. }
  89. return 0;
  90. }
  91. errcode_t ext2fs_allocate_tables(ext2_filsys fs)
  92. {
  93. errcode_t retval;
  94. dgrp_t i;
  95. for (i = 0; i < fs->group_desc_count; i++) {
  96. retval = ext2fs_allocate_group_table(fs, i, fs->block_map);
  97. if (retval)
  98. return retval;
  99. }
  100. return 0;
  101. }