bitmap.c 3.6 KB

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