read_bb.c 2.0 KB

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