3
0

check_desc.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * check_desc.c --- Check the group descriptors of an ext2 filesystem
  4. *
  5. * Copyright (C) 1993, 1994, 1995, 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. /*
  28. * This routine sanity checks the group descriptors
  29. */
  30. errcode_t ext2fs_check_desc(ext2_filsys fs)
  31. {
  32. dgrp_t i;
  33. blk_t block = fs->super->s_first_data_block;
  34. blk_t next;
  35. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  36. for (i = 0; i < fs->group_desc_count; i++) {
  37. next = block + fs->super->s_blocks_per_group;
  38. /*
  39. * Check to make sure block bitmap for group is
  40. * located within the group.
  41. */
  42. if (fs->group_desc[i].bg_block_bitmap < block ||
  43. fs->group_desc[i].bg_block_bitmap >= next)
  44. return EXT2_ET_GDESC_BAD_BLOCK_MAP;
  45. /*
  46. * Check to make sure inode bitmap for group is
  47. * located within the group
  48. */
  49. if (fs->group_desc[i].bg_inode_bitmap < block ||
  50. fs->group_desc[i].bg_inode_bitmap >= next)
  51. return EXT2_ET_GDESC_BAD_INODE_MAP;
  52. /*
  53. * Check to make sure inode table for group is located
  54. * within the group
  55. */
  56. if (fs->group_desc[i].bg_inode_table < block ||
  57. ((fs->group_desc[i].bg_inode_table +
  58. fs->inode_blocks_per_group) >= next))
  59. return EXT2_ET_GDESC_BAD_INODE_TABLE;
  60. block = next;
  61. }
  62. return 0;
  63. }