bitmap.c 8.3 KB

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