3
0

check_desc.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * check_desc.c --- Check the group descriptors of an ext2 filesystem
  3. *
  4. * Copyright (C) 1993, 1994, 1995, 1996 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. #if HAVE_SYS_STAT_H
  19. #include <sys/stat.h>
  20. #endif
  21. #if HAVE_SYS_TYPES_H
  22. #include <sys/types.h>
  23. #endif
  24. #include "ext2_fs.h"
  25. #include "ext2fs.h"
  26. /*
  27. * This routine sanity checks the group descriptors
  28. */
  29. errcode_t ext2fs_check_desc(ext2_filsys fs)
  30. {
  31. dgrp_t i;
  32. blk_t block = fs->super->s_first_data_block;
  33. blk_t next;
  34. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  35. for (i = 0; i < fs->group_desc_count; i++) {
  36. next = block + fs->super->s_blocks_per_group;
  37. /*
  38. * Check to make sure block bitmap for group is
  39. * located within the group.
  40. */
  41. if (fs->group_desc[i].bg_block_bitmap < block ||
  42. fs->group_desc[i].bg_block_bitmap >= next)
  43. return EXT2_ET_GDESC_BAD_BLOCK_MAP;
  44. /*
  45. * Check to make sure inode bitmap for group is
  46. * located within the group
  47. */
  48. if (fs->group_desc[i].bg_inode_bitmap < block ||
  49. fs->group_desc[i].bg_inode_bitmap >= next)
  50. return EXT2_ET_GDESC_BAD_INODE_MAP;
  51. /*
  52. * Check to make sure inode table for group is located
  53. * within the group
  54. */
  55. if (fs->group_desc[i].bg_inode_table < block ||
  56. ((fs->group_desc[i].bg_inode_table +
  57. fs->inode_blocks_per_group) >= next))
  58. return EXT2_ET_GDESC_BAD_INODE_TABLE;
  59. block = next;
  60. }
  61. return 0;
  62. }