read_bb.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * read_bb --- read the bad blocks inode
  3. *
  4. * Copyright (C) 1994 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. struct read_bb_record {
  27. ext2_badblocks_list bb_list;
  28. errcode_t err;
  29. };
  30. /*
  31. * Helper function for ext2fs_read_bb_inode()
  32. */
  33. #ifdef __TURBOC__
  34. #pragma argsused
  35. #endif
  36. static int mark_bad_block(ext2_filsys fs, blk_t *block_nr,
  37. e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
  38. blk_t ref_block EXT2FS_ATTR((unused)),
  39. int ref_offset EXT2FS_ATTR((unused)),
  40. void *priv_data)
  41. {
  42. struct read_bb_record *rb = (struct read_bb_record *) priv_data;
  43. if (blockcnt < 0)
  44. return 0;
  45. if ((*block_nr < fs->super->s_first_data_block) ||
  46. (*block_nr >= fs->super->s_blocks_count))
  47. return 0; /* Ignore illegal blocks */
  48. rb->err = ext2fs_badblocks_list_add(rb->bb_list, *block_nr);
  49. if (rb->err)
  50. return BLOCK_ABORT;
  51. return 0;
  52. }
  53. /*
  54. * Reads the current bad blocks from the bad blocks inode.
  55. */
  56. errcode_t ext2fs_read_bb_inode(ext2_filsys fs, ext2_badblocks_list *bb_list)
  57. {
  58. errcode_t retval;
  59. struct read_bb_record rb;
  60. struct ext2_inode inode;
  61. blk_t numblocks;
  62. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  63. if (!*bb_list) {
  64. retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
  65. if (retval)
  66. return retval;
  67. if (inode.i_blocks < 500)
  68. numblocks = (inode.i_blocks /
  69. (fs->blocksize / 512)) + 20;
  70. else
  71. numblocks = 500;
  72. retval = ext2fs_badblocks_list_create(bb_list, numblocks);
  73. if (retval)
  74. return retval;
  75. }
  76. rb.bb_list = *bb_list;
  77. rb.err = 0;
  78. retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO, 0, 0,
  79. mark_bad_block, &rb);
  80. if (retval)
  81. return retval;
  82. return rb.err;
  83. }