sparse.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * sparse.c --- find the groups in an ext2 filesystem with metadata backups
  4. *
  5. * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
  6. * Copyright (C) 2002 Andreas Dilger.
  7. *
  8. * %Begin-Header%
  9. * This file may be redistributed under the terms of the GNU Public
  10. * License.
  11. * %End-Header%
  12. */
  13. #include <stdio.h>
  14. #include "ext2_fs.h"
  15. #include "ext2fsP.h"
  16. static int test_root(int a, int b)
  17. {
  18. if (a == 0)
  19. return 1;
  20. while (1) {
  21. if (a == 1)
  22. return 1;
  23. if (a % b)
  24. return 0;
  25. a = a / b;
  26. }
  27. }
  28. int ext2fs_bg_has_super(ext2_filsys fs, int group_block)
  29. {
  30. if (!(fs->super->s_feature_ro_compat &
  31. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
  32. return 1;
  33. if (test_root(group_block, 3) || (test_root(group_block, 5)) ||
  34. test_root(group_block, 7))
  35. return 1;
  36. return 0;
  37. }
  38. /*
  39. * Iterate through the groups which hold BACKUP superblock/GDT copies in an
  40. * ext3 filesystem. The counters should be initialized to 1, 5, and 7 before
  41. * calling this for the first time. In a sparse filesystem it will be the
  42. * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
  43. * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
  44. */
  45. unsigned int ext2fs_list_backups(ext2_filsys fs, unsigned int *three,
  46. unsigned int *five, unsigned int *seven)
  47. {
  48. unsigned int *min = three;
  49. int mult = 3;
  50. unsigned int ret;
  51. if (!(fs->super->s_feature_ro_compat &
  52. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
  53. ret = *min;
  54. *min += 1;
  55. return ret;
  56. }
  57. if (*five < *min) {
  58. min = five;
  59. mult = 5;
  60. }
  61. if (*seven < *min) {
  62. min = seven;
  63. mult = 7;
  64. }
  65. ret = *min;
  66. *min *= mult;
  67. return ret;
  68. }