rs_bitmap.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * rs_bitmap.c --- routine for changing the size of a bitmap
  4. *
  5. * Copyright (C) 1996, 1997 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. #ifdef HAVE_SYS_STAT_H
  20. #include <sys/stat.h>
  21. #endif
  22. #ifdef HAVE_SYS_TYPES_H
  23. #include <sys/types.h>
  24. #endif
  25. #include "ext2_fs.h"
  26. #include "ext2fs.h"
  27. errcode_t ext2fs_resize_generic_bitmap(__u32 new_end, __u32 new_real_end,
  28. ext2fs_generic_bitmap bmap)
  29. {
  30. errcode_t retval;
  31. size_t size, new_size;
  32. __u32 bitno;
  33. if (!bmap)
  34. return EXT2_ET_INVALID_ARGUMENT;
  35. EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_GENERIC_BITMAP);
  36. /*
  37. * If we're expanding the bitmap, make sure all of the new
  38. * parts of the bitmap are zero.
  39. */
  40. if (new_end > bmap->end) {
  41. bitno = bmap->real_end;
  42. if (bitno > new_end)
  43. bitno = new_end;
  44. for (; bitno > bmap->end; bitno--)
  45. ext2fs_clear_bit(bitno - bmap->start, bmap->bitmap);
  46. }
  47. if (new_real_end == bmap->real_end) {
  48. bmap->end = new_end;
  49. return 0;
  50. }
  51. size = ((bmap->real_end - bmap->start) / 8) + 1;
  52. new_size = ((new_real_end - bmap->start) / 8) + 1;
  53. if (size != new_size) {
  54. retval = ext2fs_resize_mem(size, new_size, &bmap->bitmap);
  55. if (retval)
  56. return retval;
  57. }
  58. if (new_size > size)
  59. memset(bmap->bitmap + size, 0, new_size - size);
  60. bmap->end = new_end;
  61. bmap->real_end = new_real_end;
  62. return 0;
  63. }
  64. errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
  65. ext2fs_inode_bitmap bmap)
  66. {
  67. errcode_t retval;
  68. if (!bmap)
  69. return EXT2_ET_INVALID_ARGUMENT;
  70. EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_INODE_BITMAP);
  71. bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
  72. retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
  73. bmap);
  74. bmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
  75. return retval;
  76. }
  77. errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
  78. ext2fs_block_bitmap bmap)
  79. {
  80. errcode_t retval;
  81. if (!bmap)
  82. return EXT2_ET_INVALID_ARGUMENT;
  83. EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
  84. bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
  85. retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
  86. bmap);
  87. bmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
  88. return retval;
  89. }