bitmap.c 3.6 KB

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