bitmap.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * linux/fs/affs/bitmap.c
  3. *
  4. * (c) 1996 Hans-Joachim Widmaier
  5. *
  6. * bitmap.c contains the code that handles all bitmap related stuff -
  7. * block allocation, deallocation, calculation of free space.
  8. */
  9. #include <linux/slab.h>
  10. #include "affs.h"
  11. /* This is, of course, shamelessly stolen from fs/minix */
  12. static const int nibblemap[] = { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4 };
  13. static u32
  14. affs_count_free_bits(u32 blocksize, const void *data)
  15. {
  16. const u32 *map;
  17. u32 free;
  18. u32 tmp;
  19. map = data;
  20. free = 0;
  21. for (blocksize /= 4; blocksize > 0; blocksize--) {
  22. tmp = *map++;
  23. while (tmp) {
  24. free += nibblemap[tmp & 0xf];
  25. tmp >>= 4;
  26. }
  27. }
  28. return free;
  29. }
  30. u32
  31. affs_count_free_blocks(struct super_block *sb)
  32. {
  33. struct affs_bm_info *bm;
  34. u32 free;
  35. int i;
  36. pr_debug("AFFS: count_free_blocks()\n");
  37. if (sb->s_flags & MS_RDONLY)
  38. return 0;
  39. mutex_lock(&AFFS_SB(sb)->s_bmlock);
  40. bm = AFFS_SB(sb)->s_bitmap;
  41. free = 0;
  42. for (i = AFFS_SB(sb)->s_bmap_count; i > 0; bm++, i--)
  43. free += bm->bm_free;
  44. mutex_unlock(&AFFS_SB(sb)->s_bmlock);
  45. return free;
  46. }
  47. void
  48. affs_free_block(struct super_block *sb, u32 block)
  49. {
  50. struct affs_sb_info *sbi = AFFS_SB(sb);
  51. struct affs_bm_info *bm;
  52. struct buffer_head *bh;
  53. u32 blk, bmap, bit, mask, tmp;
  54. __be32 *data;
  55. pr_debug("AFFS: free_block(%u)\n", block);
  56. if (block > sbi->s_partition_size)
  57. goto err_range;
  58. blk = block - sbi->s_reserved;
  59. bmap = blk / sbi->s_bmap_bits;
  60. bit = blk % sbi->s_bmap_bits;
  61. bm = &sbi->s_bitmap[bmap];
  62. mutex_lock(&sbi->s_bmlock);
  63. bh = sbi->s_bmap_bh;
  64. if (sbi->s_last_bmap != bmap) {
  65. affs_brelse(bh);
  66. bh = affs_bread(sb, bm->bm_key);
  67. if (!bh)
  68. goto err_bh_read;
  69. sbi->s_bmap_bh = bh;
  70. sbi->s_last_bmap = bmap;
  71. }
  72. mask = 1 << (bit & 31);
  73. data = (__be32 *)bh->b_data + bit / 32 + 1;
  74. /* mark block free */
  75. tmp = be32_to_cpu(*data);
  76. if (tmp & mask)
  77. goto err_free;
  78. *data = cpu_to_be32(tmp | mask);
  79. /* fix checksum */
  80. tmp = be32_to_cpu(*(__be32 *)bh->b_data);
  81. *(__be32 *)bh->b_data = cpu_to_be32(tmp - mask);
  82. mark_buffer_dirty(bh);
  83. affs_mark_sb_dirty(sb);
  84. bm->bm_free++;
  85. mutex_unlock(&sbi->s_bmlock);
  86. return;
  87. err_free:
  88. affs_warning(sb,"affs_free_block","Trying to free block %u which is already free", block);
  89. mutex_unlock(&sbi->s_bmlock);
  90. return;
  91. err_bh_read:
  92. affs_error(sb,"affs_free_block","Cannot read bitmap block %u", bm->bm_key);
  93. sbi->s_bmap_bh = NULL;
  94. sbi->s_last_bmap = ~0;
  95. mutex_unlock(&sbi->s_bmlock);
  96. return;
  97. err_range:
  98. affs_error(sb, "affs_free_block","Block %u outside partition", block);
  99. return;
  100. }
  101. /*
  102. * Allocate a block in the given allocation zone.
  103. * Since we have to byte-swap the bitmap on little-endian
  104. * machines, this is rather expensive. Therefore we will
  105. * preallocate up to 16 blocks from the same word, if
  106. * possible. We are not doing preallocations in the
  107. * header zone, though.
  108. */
  109. u32
  110. affs_alloc_block(struct inode *inode, u32 goal)
  111. {
  112. struct super_block *sb;
  113. struct affs_sb_info *sbi;
  114. struct affs_bm_info *bm;
  115. struct buffer_head *bh;
  116. __be32 *data, *enddata;
  117. u32 blk, bmap, bit, mask, mask2, tmp;
  118. int i;
  119. sb = inode->i_sb;
  120. sbi = AFFS_SB(sb);
  121. pr_debug("AFFS: balloc(inode=%lu,goal=%u): ", inode->i_ino, goal);
  122. if (AFFS_I(inode)->i_pa_cnt) {
  123. pr_debug("%d\n", AFFS_I(inode)->i_lastalloc+1);
  124. AFFS_I(inode)->i_pa_cnt--;
  125. return ++AFFS_I(inode)->i_lastalloc;
  126. }
  127. if (!goal || goal > sbi->s_partition_size) {
  128. if (goal)
  129. affs_warning(sb, "affs_balloc", "invalid goal %d", goal);
  130. //if (!AFFS_I(inode)->i_last_block)
  131. // affs_warning(sb, "affs_balloc", "no last alloc block");
  132. goal = sbi->s_reserved;
  133. }
  134. blk = goal - sbi->s_reserved;
  135. bmap = blk / sbi->s_bmap_bits;
  136. bm = &sbi->s_bitmap[bmap];
  137. mutex_lock(&sbi->s_bmlock);
  138. if (bm->bm_free)
  139. goto find_bmap_bit;
  140. find_bmap:
  141. /* search for the next bmap buffer with free bits */
  142. i = sbi->s_bmap_count;
  143. do {
  144. if (--i < 0)
  145. goto err_full;
  146. bmap++;
  147. bm++;
  148. if (bmap < sbi->s_bmap_count)
  149. continue;
  150. /* restart search at zero */
  151. bmap = 0;
  152. bm = sbi->s_bitmap;
  153. } while (!bm->bm_free);
  154. blk = bmap * sbi->s_bmap_bits;
  155. find_bmap_bit:
  156. bh = sbi->s_bmap_bh;
  157. if (sbi->s_last_bmap != bmap) {
  158. affs_brelse(bh);
  159. bh = affs_bread(sb, bm->bm_key);
  160. if (!bh)
  161. goto err_bh_read;
  162. sbi->s_bmap_bh = bh;
  163. sbi->s_last_bmap = bmap;
  164. }
  165. /* find an unused block in this bitmap block */
  166. bit = blk % sbi->s_bmap_bits;
  167. data = (__be32 *)bh->b_data + bit / 32 + 1;
  168. enddata = (__be32 *)((u8 *)bh->b_data + sb->s_blocksize);
  169. mask = ~0UL << (bit & 31);
  170. blk &= ~31UL;
  171. tmp = be32_to_cpu(*data);
  172. if (tmp & mask)
  173. goto find_bit;
  174. /* scan the rest of the buffer */
  175. do {
  176. blk += 32;
  177. if (++data >= enddata)
  178. /* didn't find something, can only happen
  179. * if scan didn't start at 0, try next bmap
  180. */
  181. goto find_bmap;
  182. } while (!*data);
  183. tmp = be32_to_cpu(*data);
  184. mask = ~0;
  185. find_bit:
  186. /* finally look for a free bit in the word */
  187. bit = ffs(tmp & mask) - 1;
  188. blk += bit + sbi->s_reserved;
  189. mask2 = mask = 1 << (bit & 31);
  190. AFFS_I(inode)->i_lastalloc = blk;
  191. /* prealloc as much as possible within this word */
  192. while ((mask2 <<= 1)) {
  193. if (!(tmp & mask2))
  194. break;
  195. AFFS_I(inode)->i_pa_cnt++;
  196. mask |= mask2;
  197. }
  198. bm->bm_free -= AFFS_I(inode)->i_pa_cnt + 1;
  199. *data = cpu_to_be32(tmp & ~mask);
  200. /* fix checksum */
  201. tmp = be32_to_cpu(*(__be32 *)bh->b_data);
  202. *(__be32 *)bh->b_data = cpu_to_be32(tmp + mask);
  203. mark_buffer_dirty(bh);
  204. affs_mark_sb_dirty(sb);
  205. mutex_unlock(&sbi->s_bmlock);
  206. pr_debug("%d\n", blk);
  207. return blk;
  208. err_bh_read:
  209. affs_error(sb,"affs_read_block","Cannot read bitmap block %u", bm->bm_key);
  210. sbi->s_bmap_bh = NULL;
  211. sbi->s_last_bmap = ~0;
  212. err_full:
  213. mutex_unlock(&sbi->s_bmlock);
  214. pr_debug("failed\n");
  215. return 0;
  216. }
  217. int affs_init_bitmap(struct super_block *sb, int *flags)
  218. {
  219. struct affs_bm_info *bm;
  220. struct buffer_head *bmap_bh = NULL, *bh = NULL;
  221. __be32 *bmap_blk;
  222. u32 size, blk, end, offset, mask;
  223. int i, res = 0;
  224. struct affs_sb_info *sbi = AFFS_SB(sb);
  225. if (*flags & MS_RDONLY)
  226. return 0;
  227. if (!AFFS_ROOT_TAIL(sb, sbi->s_root_bh)->bm_flag) {
  228. printk(KERN_NOTICE "AFFS: Bitmap invalid - mounting %s read only\n",
  229. sb->s_id);
  230. *flags |= MS_RDONLY;
  231. return 0;
  232. }
  233. sbi->s_last_bmap = ~0;
  234. sbi->s_bmap_bh = NULL;
  235. sbi->s_bmap_bits = sb->s_blocksize * 8 - 32;
  236. sbi->s_bmap_count = (sbi->s_partition_size - sbi->s_reserved +
  237. sbi->s_bmap_bits - 1) / sbi->s_bmap_bits;
  238. size = sbi->s_bmap_count * sizeof(*bm);
  239. bm = sbi->s_bitmap = kzalloc(size, GFP_KERNEL);
  240. if (!sbi->s_bitmap) {
  241. printk(KERN_ERR "AFFS: Bitmap allocation failed\n");
  242. return -ENOMEM;
  243. }
  244. bmap_blk = (__be32 *)sbi->s_root_bh->b_data;
  245. blk = sb->s_blocksize / 4 - 49;
  246. end = blk + 25;
  247. for (i = sbi->s_bmap_count; i > 0; bm++, i--) {
  248. affs_brelse(bh);
  249. bm->bm_key = be32_to_cpu(bmap_blk[blk]);
  250. bh = affs_bread(sb, bm->bm_key);
  251. if (!bh) {
  252. printk(KERN_ERR "AFFS: Cannot read bitmap\n");
  253. res = -EIO;
  254. goto out;
  255. }
  256. if (affs_checksum_block(sb, bh)) {
  257. printk(KERN_WARNING "AFFS: Bitmap %u invalid - mounting %s read only.\n",
  258. bm->bm_key, sb->s_id);
  259. *flags |= MS_RDONLY;
  260. goto out;
  261. }
  262. pr_debug("AFFS: read bitmap block %d: %d\n", blk, bm->bm_key);
  263. bm->bm_free = affs_count_free_bits(sb->s_blocksize - 4, bh->b_data + 4);
  264. /* Don't try read the extension if this is the last block,
  265. * but we also need the right bm pointer below
  266. */
  267. if (++blk < end || i == 1)
  268. continue;
  269. if (bmap_bh)
  270. affs_brelse(bmap_bh);
  271. bmap_bh = affs_bread(sb, be32_to_cpu(bmap_blk[blk]));
  272. if (!bmap_bh) {
  273. printk(KERN_ERR "AFFS: Cannot read bitmap extension\n");
  274. res = -EIO;
  275. goto out;
  276. }
  277. bmap_blk = (__be32 *)bmap_bh->b_data;
  278. blk = 0;
  279. end = sb->s_blocksize / 4 - 1;
  280. }
  281. offset = (sbi->s_partition_size - sbi->s_reserved) % sbi->s_bmap_bits;
  282. mask = ~(0xFFFFFFFFU << (offset & 31));
  283. pr_debug("last word: %d %d %d\n", offset, offset / 32 + 1, mask);
  284. offset = offset / 32 + 1;
  285. if (mask) {
  286. u32 old, new;
  287. /* Mark unused bits in the last word as allocated */
  288. old = be32_to_cpu(((__be32 *)bh->b_data)[offset]);
  289. new = old & mask;
  290. //if (old != new) {
  291. ((__be32 *)bh->b_data)[offset] = cpu_to_be32(new);
  292. /* fix checksum */
  293. //new -= old;
  294. //old = be32_to_cpu(*(__be32 *)bh->b_data);
  295. //*(__be32 *)bh->b_data = cpu_to_be32(old - new);
  296. //mark_buffer_dirty(bh);
  297. //}
  298. /* correct offset for the bitmap count below */
  299. //offset++;
  300. }
  301. while (++offset < sb->s_blocksize / 4)
  302. ((__be32 *)bh->b_data)[offset] = 0;
  303. ((__be32 *)bh->b_data)[0] = 0;
  304. ((__be32 *)bh->b_data)[0] = cpu_to_be32(-affs_checksum_block(sb, bh));
  305. mark_buffer_dirty(bh);
  306. /* recalculate bitmap count for last block */
  307. bm--;
  308. bm->bm_free = affs_count_free_bits(sb->s_blocksize - 4, bh->b_data + 4);
  309. out:
  310. affs_brelse(bh);
  311. affs_brelse(bmap_bh);
  312. return res;
  313. }
  314. void affs_free_bitmap(struct super_block *sb)
  315. {
  316. struct affs_sb_info *sbi = AFFS_SB(sb);
  317. if (!sbi->s_bitmap)
  318. return;
  319. affs_brelse(sbi->s_bmap_bh);
  320. sbi->s_bmap_bh = NULL;
  321. sbi->s_last_bmap = ~0;
  322. kfree(sbi->s_bitmap);
  323. sbi->s_bitmap = NULL;
  324. }