yaffs_bitmap.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2011 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include "yaffs_bitmap.h"
  14. #include "yaffs_trace.h"
  15. /*
  16. * Chunk bitmap manipulations
  17. */
  18. static inline u8 *yaffs_block_bits(struct yaffs_dev *dev, int blk)
  19. {
  20. if (blk < dev->internal_start_block || blk > dev->internal_end_block) {
  21. yaffs_trace(YAFFS_TRACE_ERROR,
  22. "BlockBits block %d is not valid",
  23. blk);
  24. BUG();
  25. }
  26. return dev->chunk_bits +
  27. (dev->chunk_bit_stride * (blk - dev->internal_start_block));
  28. }
  29. void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk)
  30. {
  31. if (blk < dev->internal_start_block || blk > dev->internal_end_block ||
  32. chunk < 0 || chunk >= dev->param.chunks_per_block) {
  33. yaffs_trace(YAFFS_TRACE_ERROR,
  34. "Chunk Id (%d:%d) invalid",
  35. blk, chunk);
  36. BUG();
  37. }
  38. }
  39. void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk)
  40. {
  41. u8 *blk_bits = yaffs_block_bits(dev, blk);
  42. memset(blk_bits, 0, dev->chunk_bit_stride);
  43. }
  44. void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
  45. {
  46. u8 *blk_bits = yaffs_block_bits(dev, blk);
  47. yaffs_verify_chunk_bit_id(dev, blk, chunk);
  48. blk_bits[chunk / 8] &= ~(1 << (chunk & 7));
  49. }
  50. void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
  51. {
  52. u8 *blk_bits = yaffs_block_bits(dev, blk);
  53. yaffs_verify_chunk_bit_id(dev, blk, chunk);
  54. blk_bits[chunk / 8] |= (1 << (chunk & 7));
  55. }
  56. int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
  57. {
  58. u8 *blk_bits = yaffs_block_bits(dev, blk);
  59. yaffs_verify_chunk_bit_id(dev, blk, chunk);
  60. return (blk_bits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
  61. }
  62. int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk)
  63. {
  64. u8 *blk_bits = yaffs_block_bits(dev, blk);
  65. int i;
  66. for (i = 0; i < dev->chunk_bit_stride; i++) {
  67. if (*blk_bits)
  68. return 1;
  69. blk_bits++;
  70. }
  71. return 0;
  72. }
  73. int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk)
  74. {
  75. u8 *blk_bits = yaffs_block_bits(dev, blk);
  76. int i;
  77. int n = 0;
  78. for (i = 0; i < dev->chunk_bit_stride; i++, blk_bits++)
  79. n += hweight8(*blk_bits);
  80. return n;
  81. }