bitmap.c 3.6 KB

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