bitmap.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * QNX4 file system, Linux implementation.
  3. *
  4. * Version : 0.2.1
  5. *
  6. * Using parts of the xiafs filesystem.
  7. *
  8. * History :
  9. *
  10. * 28-05-1998 by Richard Frowijn : first release.
  11. * 20-06-1998 by Frank Denis : basic optimisations.
  12. * 25-06-1998 by Frank Denis : qnx4_is_free, qnx4_set_bitmap, qnx4_bmap .
  13. * 28-06-1998 by Frank Denis : qnx4_free_inode (to be fixed) .
  14. */
  15. #include <linux/buffer_head.h>
  16. #include <linux/bitops.h>
  17. #include "qnx4.h"
  18. #if 0
  19. int qnx4_new_block(struct super_block *sb)
  20. {
  21. return 0;
  22. }
  23. #endif /* 0 */
  24. static void count_bits(register const char *bmPart, register int size,
  25. int *const tf)
  26. {
  27. char b;
  28. int tot = *tf;
  29. if (size > QNX4_BLOCK_SIZE) {
  30. size = QNX4_BLOCK_SIZE;
  31. }
  32. do {
  33. b = *bmPart++;
  34. if ((b & 1) == 0)
  35. tot++;
  36. if ((b & 2) == 0)
  37. tot++;
  38. if ((b & 4) == 0)
  39. tot++;
  40. if ((b & 8) == 0)
  41. tot++;
  42. if ((b & 16) == 0)
  43. tot++;
  44. if ((b & 32) == 0)
  45. tot++;
  46. if ((b & 64) == 0)
  47. tot++;
  48. if ((b & 128) == 0)
  49. tot++;
  50. size--;
  51. } while (size != 0);
  52. *tf = tot;
  53. }
  54. unsigned long qnx4_count_free_blocks(struct super_block *sb)
  55. {
  56. int start = le32_to_cpu(qnx4_sb(sb)->BitMap->di_first_xtnt.xtnt_blk) - 1;
  57. int total = 0;
  58. int total_free = 0;
  59. int offset = 0;
  60. int size = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size);
  61. struct buffer_head *bh;
  62. while (total < size) {
  63. if ((bh = sb_bread(sb, start + offset)) == NULL) {
  64. printk("qnx4: I/O error in counting free blocks\n");
  65. break;
  66. }
  67. count_bits(bh->b_data, size - total, &total_free);
  68. brelse(bh);
  69. total += QNX4_BLOCK_SIZE;
  70. offset++;
  71. }
  72. return total_free;
  73. }
  74. #ifdef CONFIG_QNX4FS_RW
  75. int qnx4_is_free(struct super_block *sb, long block)
  76. {
  77. int start = le32_to_cpu(qnx4_sb(sb)->BitMap->di_first_xtnt.xtnt_blk) - 1;
  78. int size = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size);
  79. struct buffer_head *bh;
  80. const char *g;
  81. int ret = -EIO;
  82. start += block / (QNX4_BLOCK_SIZE * 8);
  83. QNX4DEBUG(("qnx4: is_free requesting block [%lu], bitmap in block [%lu]\n",
  84. (unsigned long) block, (unsigned long) start));
  85. (void) size; /* CHECKME */
  86. bh = sb_bread(sb, start);
  87. if (bh == NULL) {
  88. return -EIO;
  89. }
  90. g = bh->b_data + (block % QNX4_BLOCK_SIZE);
  91. if (((*g) & (1 << (block % 8))) == 0) {
  92. QNX4DEBUG(("qnx4: is_free -> block is free\n"));
  93. ret = 1;
  94. } else {
  95. QNX4DEBUG(("qnx4: is_free -> block is busy\n"));
  96. ret = 0;
  97. }
  98. brelse(bh);
  99. return ret;
  100. }
  101. int qnx4_set_bitmap(struct super_block *sb, long block, int busy)
  102. {
  103. int start = le32_to_cpu(qnx4_sb(sb)->BitMap->di_first_xtnt.xtnt_blk) - 1;
  104. int size = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size);
  105. struct buffer_head *bh;
  106. char *g;
  107. start += block / (QNX4_BLOCK_SIZE * 8);
  108. QNX4DEBUG(("qnx4: set_bitmap requesting block [%lu], bitmap in block [%lu]\n",
  109. (unsigned long) block, (unsigned long) start));
  110. (void) size; /* CHECKME */
  111. bh = sb_bread(sb, start);
  112. if (bh == NULL) {
  113. return -EIO;
  114. }
  115. g = bh->b_data + (block % QNX4_BLOCK_SIZE);
  116. if (busy == 0) {
  117. (*g) &= ~(1 << (block % 8));
  118. } else {
  119. (*g) |= (1 << (block % 8));
  120. }
  121. mark_buffer_dirty(bh);
  122. brelse(bh);
  123. return 0;
  124. }
  125. static void qnx4_clear_inode(struct inode *inode)
  126. {
  127. struct qnx4_inode_entry *qnx4_ino = qnx4_raw_inode(inode);
  128. /* What for? */
  129. memset(qnx4_ino->di_fname, 0, sizeof qnx4_ino->di_fname);
  130. qnx4_ino->di_size = 0;
  131. qnx4_ino->di_num_xtnts = 0;
  132. qnx4_ino->di_mode = 0;
  133. qnx4_ino->di_status = 0;
  134. }
  135. void qnx4_free_inode(struct inode *inode)
  136. {
  137. if (inode->i_ino < 1) {
  138. printk("free_inode: inode 0 or nonexistent inode\n");
  139. return;
  140. }
  141. qnx4_clear_inode(inode);
  142. clear_inode(inode);
  143. }
  144. #endif