bitmap.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * linux/fs/minix/bitmap.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Modified for 680x0 by Hamish Macdonald
  8. * Fixed for 680x0 by Andreas Schwab
  9. */
  10. /* bitmap.c contains the code that handles the inode and block bitmaps */
  11. #include "minix.h"
  12. #include <linux/buffer_head.h>
  13. #include <linux/bitops.h>
  14. #include <linux/sched.h>
  15. static DEFINE_SPINLOCK(bitmap_lock);
  16. /*
  17. * bitmap consists of blocks filled with 16bit words
  18. * bit set == busy, bit clear == free
  19. * endianness is a mess, but for counting zero bits it really doesn't matter...
  20. */
  21. static __u32 count_free(struct buffer_head *map[], unsigned blocksize, __u32 numbits)
  22. {
  23. __u32 sum = 0;
  24. unsigned blocks = DIV_ROUND_UP(numbits, blocksize * 8);
  25. while (blocks--) {
  26. unsigned words = blocksize / 2;
  27. __u16 *p = (__u16 *)(*map++)->b_data;
  28. while (words--)
  29. sum += 16 - hweight16(*p++);
  30. }
  31. return sum;
  32. }
  33. void minix_free_block(struct inode *inode, unsigned long block)
  34. {
  35. struct super_block *sb = inode->i_sb;
  36. struct minix_sb_info *sbi = minix_sb(sb);
  37. struct buffer_head *bh;
  38. int k = sb->s_blocksize_bits + 3;
  39. unsigned long bit, zone;
  40. if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
  41. printk("Trying to free block not in datazone\n");
  42. return;
  43. }
  44. zone = block - sbi->s_firstdatazone + 1;
  45. bit = zone & ((1<<k) - 1);
  46. zone >>= k;
  47. if (zone >= sbi->s_zmap_blocks) {
  48. printk("minix_free_block: nonexistent bitmap buffer\n");
  49. return;
  50. }
  51. bh = sbi->s_zmap[zone];
  52. spin_lock(&bitmap_lock);
  53. if (!minix_test_and_clear_bit(bit, bh->b_data))
  54. printk("minix_free_block (%s:%lu): bit already cleared\n",
  55. sb->s_id, block);
  56. spin_unlock(&bitmap_lock);
  57. mark_buffer_dirty(bh);
  58. return;
  59. }
  60. int minix_new_block(struct inode * inode)
  61. {
  62. struct minix_sb_info *sbi = minix_sb(inode->i_sb);
  63. int bits_per_zone = 8 * inode->i_sb->s_blocksize;
  64. int i;
  65. for (i = 0; i < sbi->s_zmap_blocks; i++) {
  66. struct buffer_head *bh = sbi->s_zmap[i];
  67. int j;
  68. spin_lock(&bitmap_lock);
  69. j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
  70. if (j < bits_per_zone) {
  71. minix_set_bit(j, bh->b_data);
  72. spin_unlock(&bitmap_lock);
  73. mark_buffer_dirty(bh);
  74. j += i * bits_per_zone + sbi->s_firstdatazone-1;
  75. if (j < sbi->s_firstdatazone || j >= sbi->s_nzones)
  76. break;
  77. return j;
  78. }
  79. spin_unlock(&bitmap_lock);
  80. }
  81. return 0;
  82. }
  83. unsigned long minix_count_free_blocks(struct super_block *sb)
  84. {
  85. struct minix_sb_info *sbi = minix_sb(sb);
  86. u32 bits = sbi->s_nzones - sbi->s_firstdatazone + 1;
  87. return (count_free(sbi->s_zmap, sb->s_blocksize, bits)
  88. << sbi->s_log_zone_size);
  89. }
  90. struct minix_inode *
  91. minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
  92. {
  93. int block;
  94. struct minix_sb_info *sbi = minix_sb(sb);
  95. struct minix_inode *p;
  96. if (!ino || ino > sbi->s_ninodes) {
  97. printk("Bad inode number on dev %s: %ld is out of range\n",
  98. sb->s_id, (long)ino);
  99. return NULL;
  100. }
  101. ino--;
  102. block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
  103. ino / MINIX_INODES_PER_BLOCK;
  104. *bh = sb_bread(sb, block);
  105. if (!*bh) {
  106. printk("Unable to read inode block\n");
  107. return NULL;
  108. }
  109. p = (void *)(*bh)->b_data;
  110. return p + ino % MINIX_INODES_PER_BLOCK;
  111. }
  112. struct minix2_inode *
  113. minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
  114. {
  115. int block;
  116. struct minix_sb_info *sbi = minix_sb(sb);
  117. struct minix2_inode *p;
  118. int minix2_inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode);
  119. *bh = NULL;
  120. if (!ino || ino > sbi->s_ninodes) {
  121. printk("Bad inode number on dev %s: %ld is out of range\n",
  122. sb->s_id, (long)ino);
  123. return NULL;
  124. }
  125. ino--;
  126. block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
  127. ino / minix2_inodes_per_block;
  128. *bh = sb_bread(sb, block);
  129. if (!*bh) {
  130. printk("Unable to read inode block\n");
  131. return NULL;
  132. }
  133. p = (void *)(*bh)->b_data;
  134. return p + ino % minix2_inodes_per_block;
  135. }
  136. /* Clear the link count and mode of a deleted inode on disk. */
  137. static void minix_clear_inode(struct inode *inode)
  138. {
  139. struct buffer_head *bh = NULL;
  140. if (INODE_VERSION(inode) == MINIX_V1) {
  141. struct minix_inode *raw_inode;
  142. raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
  143. if (raw_inode) {
  144. raw_inode->i_nlinks = 0;
  145. raw_inode->i_mode = 0;
  146. }
  147. } else {
  148. struct minix2_inode *raw_inode;
  149. raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
  150. if (raw_inode) {
  151. raw_inode->i_nlinks = 0;
  152. raw_inode->i_mode = 0;
  153. }
  154. }
  155. if (bh) {
  156. mark_buffer_dirty(bh);
  157. brelse (bh);
  158. }
  159. }
  160. void minix_free_inode(struct inode * inode)
  161. {
  162. struct super_block *sb = inode->i_sb;
  163. struct minix_sb_info *sbi = minix_sb(inode->i_sb);
  164. struct buffer_head *bh;
  165. int k = sb->s_blocksize_bits + 3;
  166. unsigned long ino, bit;
  167. ino = inode->i_ino;
  168. if (ino < 1 || ino > sbi->s_ninodes) {
  169. printk("minix_free_inode: inode 0 or nonexistent inode\n");
  170. return;
  171. }
  172. bit = ino & ((1<<k) - 1);
  173. ino >>= k;
  174. if (ino >= sbi->s_imap_blocks) {
  175. printk("minix_free_inode: nonexistent imap in superblock\n");
  176. return;
  177. }
  178. minix_clear_inode(inode); /* clear on-disk copy */
  179. bh = sbi->s_imap[ino];
  180. spin_lock(&bitmap_lock);
  181. if (!minix_test_and_clear_bit(bit, bh->b_data))
  182. printk("minix_free_inode: bit %lu already cleared\n", bit);
  183. spin_unlock(&bitmap_lock);
  184. mark_buffer_dirty(bh);
  185. }
  186. struct inode *minix_new_inode(const struct inode *dir, umode_t mode, int *error)
  187. {
  188. struct super_block *sb = dir->i_sb;
  189. struct minix_sb_info *sbi = minix_sb(sb);
  190. struct inode *inode = new_inode(sb);
  191. struct buffer_head * bh;
  192. int bits_per_zone = 8 * sb->s_blocksize;
  193. unsigned long j;
  194. int i;
  195. if (!inode) {
  196. *error = -ENOMEM;
  197. return NULL;
  198. }
  199. j = bits_per_zone;
  200. bh = NULL;
  201. *error = -ENOSPC;
  202. spin_lock(&bitmap_lock);
  203. for (i = 0; i < sbi->s_imap_blocks; i++) {
  204. bh = sbi->s_imap[i];
  205. j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
  206. if (j < bits_per_zone)
  207. break;
  208. }
  209. if (!bh || j >= bits_per_zone) {
  210. spin_unlock(&bitmap_lock);
  211. iput(inode);
  212. return NULL;
  213. }
  214. if (minix_test_and_set_bit(j, bh->b_data)) { /* shouldn't happen */
  215. spin_unlock(&bitmap_lock);
  216. printk("minix_new_inode: bit already set\n");
  217. iput(inode);
  218. return NULL;
  219. }
  220. spin_unlock(&bitmap_lock);
  221. mark_buffer_dirty(bh);
  222. j += i * bits_per_zone;
  223. if (!j || j > sbi->s_ninodes) {
  224. iput(inode);
  225. return NULL;
  226. }
  227. inode_init_owner(inode, dir, mode);
  228. inode->i_ino = j;
  229. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  230. inode->i_blocks = 0;
  231. memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u));
  232. insert_inode_hash(inode);
  233. mark_inode_dirty(inode);
  234. *error = 0;
  235. return inode;
  236. }
  237. unsigned long minix_count_free_inodes(struct super_block *sb)
  238. {
  239. struct minix_sb_info *sbi = minix_sb(sb);
  240. u32 bits = sbi->s_ninodes + 1;
  241. return count_free(sbi->s_imap, sb->s_blocksize, bits);
  242. }