3
0

rs_bitmap.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * rs_bitmap.c --- routine for changing the size of a bitmap
  3. *
  4. * Copyright (C) 1996, 1997 Theodore Ts'o.
  5. *
  6. * %Begin-Header%
  7. * This file may be redistributed under the terms of the GNU Public
  8. * License.
  9. * %End-Header%
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #if HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #include <fcntl.h>
  17. #include <time.h>
  18. #ifdef HAVE_SYS_STAT_H
  19. #include <sys/stat.h>
  20. #endif
  21. #ifdef HAVE_SYS_TYPES_H
  22. #include <sys/types.h>
  23. #endif
  24. #include "ext2_fs.h"
  25. #include "ext2fs.h"
  26. errcode_t ext2fs_resize_generic_bitmap(__u32 new_end, __u32 new_real_end,
  27. ext2fs_generic_bitmap bmap)
  28. {
  29. errcode_t retval;
  30. size_t size, new_size;
  31. __u32 bitno;
  32. if (!bmap)
  33. return EXT2_ET_INVALID_ARGUMENT;
  34. EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_GENERIC_BITMAP);
  35. /*
  36. * If we're expanding the bitmap, make sure all of the new
  37. * parts of the bitmap are zero.
  38. */
  39. if (new_end > bmap->end) {
  40. bitno = bmap->real_end;
  41. if (bitno > new_end)
  42. bitno = new_end;
  43. for (; bitno > bmap->end; bitno--)
  44. ext2fs_clear_bit(bitno - bmap->start, bmap->bitmap);
  45. }
  46. if (new_real_end == bmap->real_end) {
  47. bmap->end = new_end;
  48. return 0;
  49. }
  50. size = ((bmap->real_end - bmap->start) / 8) + 1;
  51. new_size = ((new_real_end - bmap->start) / 8) + 1;
  52. if (size != new_size) {
  53. retval = ext2fs_resize_mem(size, new_size, &bmap->bitmap);
  54. if (retval)
  55. return retval;
  56. }
  57. if (new_size > size)
  58. memset(bmap->bitmap + size, 0, new_size - size);
  59. bmap->end = new_end;
  60. bmap->real_end = new_real_end;
  61. return 0;
  62. }
  63. errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
  64. ext2fs_inode_bitmap bmap)
  65. {
  66. errcode_t retval;
  67. if (!bmap)
  68. return EXT2_ET_INVALID_ARGUMENT;
  69. EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_INODE_BITMAP);
  70. bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
  71. retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
  72. bmap);
  73. bmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
  74. return retval;
  75. }
  76. errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
  77. ext2fs_block_bitmap bmap)
  78. {
  79. errcode_t retval;
  80. if (!bmap)
  81. return EXT2_ET_INVALID_ARGUMENT;
  82. EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
  83. bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
  84. retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
  85. bmap);
  86. bmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
  87. return retval;
  88. }