alloc_tables.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include <stdio.h>
  13. #include <string.h>
  14. #if HAVE_UNISTD_H
  15. #include <unistd.h>
  16. #endif
  17. #include <fcntl.h>
  18. #include <time.h>
  19. #if HAVE_SYS_STAT_H
  20. #include <sys/stat.h>
  21. #endif
  22. #if HAVE_SYS_TYPES_H
  23. #include <sys/types.h>
  24. #endif
  25. #include "ext2_fs.h"
  26. #include "ext2fs.h"
  27. errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
  28. ext2fs_block_bitmap bmap)
  29. {
  30. errcode_t retval;
  31. blk_t group_blk, start_blk, last_blk, new_blk, blk;
  32. int j;
  33. group_blk = fs->super->s_first_data_block +
  34. (group * fs->super->s_blocks_per_group);
  35. last_blk = group_blk + fs->super->s_blocks_per_group;
  36. if (last_blk >= fs->super->s_blocks_count)
  37. last_blk = fs->super->s_blocks_count - 1;
  38. if (!bmap)
  39. bmap = fs->block_map;
  40. /*
  41. * Allocate the block and inode bitmaps, if necessary
  42. */
  43. if (fs->stride) {
  44. start_blk = group_blk + fs->inode_blocks_per_group;
  45. start_blk += ((fs->stride * group) %
  46. (last_blk - start_blk));
  47. if (start_blk > last_blk)
  48. start_blk = group_blk;
  49. } else
  50. start_blk = group_blk;
  51. if (!fs->group_desc[group].bg_block_bitmap) {
  52. retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
  53. 1, bmap, &new_blk);
  54. if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
  55. retval = ext2fs_get_free_blocks(fs, group_blk,
  56. last_blk, 1, bmap, &new_blk);
  57. if (retval)
  58. return retval;
  59. ext2fs_mark_block_bitmap(bmap, new_blk);
  60. fs->group_desc[group].bg_block_bitmap = new_blk;
  61. }
  62. if (!fs->group_desc[group].bg_inode_bitmap) {
  63. retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
  64. 1, bmap, &new_blk);
  65. if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
  66. retval = ext2fs_get_free_blocks(fs, group_blk,
  67. last_blk, 1, bmap, &new_blk);
  68. if (retval)
  69. return retval;
  70. ext2fs_mark_block_bitmap(bmap, new_blk);
  71. fs->group_desc[group].bg_inode_bitmap = new_blk;
  72. }
  73. /*
  74. * Allocate the inode table
  75. */
  76. if (!fs->group_desc[group].bg_inode_table) {
  77. retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
  78. fs->inode_blocks_per_group,
  79. bmap, &new_blk);
  80. if (retval)
  81. return retval;
  82. for (j=0, blk = new_blk;
  83. j < fs->inode_blocks_per_group;
  84. j++, blk++)
  85. ext2fs_mark_block_bitmap(bmap, blk);
  86. fs->group_desc[group].bg_inode_table = new_blk;
  87. }
  88. return 0;
  89. }
  90. errcode_t ext2fs_allocate_tables(ext2_filsys fs)
  91. {
  92. errcode_t retval;
  93. dgrp_t i;
  94. for (i = 0; i < fs->group_desc_count; i++) {
  95. retval = ext2fs_allocate_group_table(fs, i, fs->block_map);
  96. if (retval)
  97. return retval;
  98. }
  99. return 0;
  100. }