2 * linux/fs/affs/bitmap.c
4 * (c) 1996 Hans-Joachim Widmaier
6 * bitmap.c contains the code that handles all bitmap related stuff -
7 * block allocation, deallocation, calculation of free space.
12 /* This is, of course, shamelessly stolen from fs/minix */
14 static int nibblemap
[] = { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4 };
17 affs_count_free_bits(u32 blocksize
, const void *data
)
25 for (blocksize
/= 4; blocksize
> 0; blocksize
--) {
28 free
+= nibblemap
[tmp
& 0xf];
37 affs_count_free_blocks(struct super_block
*sb
)
39 struct affs_bm_info
*bm
;
43 pr_debug("AFFS: count_free_blocks()\n");
45 if (sb
->s_flags
& MS_RDONLY
)
48 down(&AFFS_SB(sb
)->s_bmlock
);
50 bm
= AFFS_SB(sb
)->s_bitmap
;
52 for (i
= AFFS_SB(sb
)->s_bmap_count
; i
> 0; bm
++, i
--)
55 up(&AFFS_SB(sb
)->s_bmlock
);
61 affs_free_block(struct super_block
*sb
, u32 block
)
63 struct affs_sb_info
*sbi
= AFFS_SB(sb
);
64 struct affs_bm_info
*bm
;
65 struct buffer_head
*bh
;
66 u32 blk
, bmap
, bit
, mask
, tmp
;
69 pr_debug("AFFS: free_block(%u)\n", block
);
71 if (block
> sbi
->s_partition_size
)
74 blk
= block
- sbi
->s_reserved
;
75 bmap
= blk
/ sbi
->s_bmap_bits
;
76 bit
= blk
% sbi
->s_bmap_bits
;
77 bm
= &sbi
->s_bitmap
[bmap
];
82 if (sbi
->s_last_bmap
!= bmap
) {
84 bh
= affs_bread(sb
, bm
->bm_key
);
88 sbi
->s_last_bmap
= bmap
;
91 mask
= 1 << (bit
& 31);
92 data
= (__be32
*)bh
->b_data
+ bit
/ 32 + 1;
95 tmp
= be32_to_cpu(*data
);
98 *data
= cpu_to_be32(tmp
| mask
);
101 tmp
= be32_to_cpu(*(__be32
*)bh
->b_data
);
102 *(__be32
*)bh
->b_data
= cpu_to_be32(tmp
- mask
);
104 mark_buffer_dirty(bh
);
112 affs_warning(sb
,"affs_free_block","Trying to free block %u which is already free", block
);
117 affs_error(sb
,"affs_free_block","Cannot read bitmap block %u", bm
->bm_key
);
118 sbi
->s_bmap_bh
= NULL
;
119 sbi
->s_last_bmap
= ~0;
124 affs_error(sb
, "affs_free_block","Block %u outside partition", block
);
129 * Allocate a block in the given allocation zone.
130 * Since we have to byte-swap the bitmap on little-endian
131 * machines, this is rather expensive. Therefor we will
132 * preallocate up to 16 blocks from the same word, if
133 * possible. We are not doing preallocations in the
134 * header zone, though.
138 affs_alloc_block(struct inode
*inode
, u32 goal
)
140 struct super_block
*sb
;
141 struct affs_sb_info
*sbi
;
142 struct affs_bm_info
*bm
;
143 struct buffer_head
*bh
;
144 __be32
*data
, *enddata
;
145 u32 blk
, bmap
, bit
, mask
, mask2
, tmp
;
151 pr_debug("AFFS: balloc(inode=%lu,goal=%u): ", inode
->i_ino
, goal
);
153 if (AFFS_I(inode
)->i_pa_cnt
) {
154 pr_debug("%d\n", AFFS_I(inode
)->i_lastalloc
+1);
155 AFFS_I(inode
)->i_pa_cnt
--;
156 return ++AFFS_I(inode
)->i_lastalloc
;
159 if (!goal
|| goal
> sbi
->s_partition_size
) {
161 affs_warning(sb
, "affs_balloc", "invalid goal %d", goal
);
162 //if (!AFFS_I(inode)->i_last_block)
163 // affs_warning(sb, "affs_balloc", "no last alloc block");
164 goal
= sbi
->s_reserved
;
167 blk
= goal
- sbi
->s_reserved
;
168 bmap
= blk
/ sbi
->s_bmap_bits
;
169 bm
= &sbi
->s_bitmap
[bmap
];
171 down(&sbi
->s_bmlock
);
177 /* search for the next bmap buffer with free bits */
178 i
= sbi
->s_bmap_count
;
184 if (bmap
< sbi
->s_bmap_count
)
186 /* restart search at zero */
189 } while (!bm
->bm_free
);
190 blk
= bmap
* sbi
->s_bmap_bits
;
195 if (sbi
->s_last_bmap
!= bmap
) {
197 bh
= affs_bread(sb
, bm
->bm_key
);
201 sbi
->s_last_bmap
= bmap
;
204 /* find an unused block in this bitmap block */
205 bit
= blk
% sbi
->s_bmap_bits
;
206 data
= (__be32
*)bh
->b_data
+ bit
/ 32 + 1;
207 enddata
= (__be32
*)((u8
*)bh
->b_data
+ sb
->s_blocksize
);
208 mask
= ~0UL << (bit
& 31);
211 tmp
= be32_to_cpu(*data
);
215 /* scan the rest of the buffer */
218 if (++data
>= enddata
)
219 /* didn't find something, can only happen
220 * if scan didn't start at 0, try next bmap
224 tmp
= be32_to_cpu(*data
);
228 /* finally look for a free bit in the word */
229 bit
= ffs(tmp
& mask
) - 1;
230 blk
+= bit
+ sbi
->s_reserved
;
231 mask2
= mask
= 1 << (bit
& 31);
232 AFFS_I(inode
)->i_lastalloc
= blk
;
234 /* prealloc as much as possible within this word */
235 while ((mask2
<<= 1)) {
238 AFFS_I(inode
)->i_pa_cnt
++;
241 bm
->bm_free
-= AFFS_I(inode
)->i_pa_cnt
+ 1;
243 *data
= cpu_to_be32(tmp
& ~mask
);
246 tmp
= be32_to_cpu(*(__be32
*)bh
->b_data
);
247 *(__be32
*)bh
->b_data
= cpu_to_be32(tmp
+ mask
);
249 mark_buffer_dirty(bh
);
254 pr_debug("%d\n", blk
);
258 affs_error(sb
,"affs_read_block","Cannot read bitmap block %u", bm
->bm_key
);
259 sbi
->s_bmap_bh
= NULL
;
260 sbi
->s_last_bmap
= ~0;
263 pr_debug("failed\n");
267 int affs_init_bitmap(struct super_block
*sb
, int *flags
)
269 struct affs_bm_info
*bm
;
270 struct buffer_head
*bmap_bh
= NULL
, *bh
= NULL
;
272 u32 size
, blk
, end
, offset
, mask
;
274 struct affs_sb_info
*sbi
= AFFS_SB(sb
);
276 if (*flags
& MS_RDONLY
)
279 if (!AFFS_ROOT_TAIL(sb
, sbi
->s_root_bh
)->bm_flag
) {
280 printk(KERN_NOTICE
"AFFS: Bitmap invalid - mounting %s read only\n",
286 sbi
->s_last_bmap
= ~0;
287 sbi
->s_bmap_bh
= NULL
;
288 sbi
->s_bmap_bits
= sb
->s_blocksize
* 8 - 32;
289 sbi
->s_bmap_count
= (sbi
->s_partition_size
- sbi
->s_reserved
+
290 sbi
->s_bmap_bits
- 1) / sbi
->s_bmap_bits
;
291 size
= sbi
->s_bmap_count
* sizeof(*bm
);
292 bm
= sbi
->s_bitmap
= kmalloc(size
, GFP_KERNEL
);
293 if (!sbi
->s_bitmap
) {
294 printk(KERN_ERR
"AFFS: Bitmap allocation failed\n");
297 memset(sbi
->s_bitmap
, 0, size
);
299 bmap_blk
= (__be32
*)sbi
->s_root_bh
->b_data
;
300 blk
= sb
->s_blocksize
/ 4 - 49;
303 for (i
= sbi
->s_bmap_count
; i
> 0; bm
++, i
--) {
306 bm
->bm_key
= be32_to_cpu(bmap_blk
[blk
]);
307 bh
= affs_bread(sb
, bm
->bm_key
);
309 printk(KERN_ERR
"AFFS: Cannot read bitmap\n");
313 if (affs_checksum_block(sb
, bh
)) {
314 printk(KERN_WARNING
"AFFS: Bitmap %u invalid - mounting %s read only.\n",
315 bm
->bm_key
, sb
->s_id
);
319 pr_debug("AFFS: read bitmap block %d: %d\n", blk
, bm
->bm_key
);
320 bm
->bm_free
= affs_count_free_bits(sb
->s_blocksize
- 4, bh
->b_data
+ 4);
322 /* Don't try read the extension if this is the last block,
323 * but we also need the right bm pointer below
325 if (++blk
< end
|| i
== 1)
328 affs_brelse(bmap_bh
);
329 bmap_bh
= affs_bread(sb
, be32_to_cpu(bmap_blk
[blk
]));
331 printk(KERN_ERR
"AFFS: Cannot read bitmap extension\n");
335 bmap_blk
= (__be32
*)bmap_bh
->b_data
;
337 end
= sb
->s_blocksize
/ 4 - 1;
340 offset
= (sbi
->s_partition_size
- sbi
->s_reserved
) % sbi
->s_bmap_bits
;
341 mask
= ~(0xFFFFFFFFU
<< (offset
& 31));
342 pr_debug("last word: %d %d %d\n", offset
, offset
/ 32 + 1, mask
);
343 offset
= offset
/ 32 + 1;
348 /* Mark unused bits in the last word as allocated */
349 old
= be32_to_cpu(((__be32
*)bh
->b_data
)[offset
]);
352 ((__be32
*)bh
->b_data
)[offset
] = cpu_to_be32(new);
355 //old = be32_to_cpu(*(__be32 *)bh->b_data);
356 //*(__be32 *)bh->b_data = cpu_to_be32(old - new);
357 //mark_buffer_dirty(bh);
359 /* correct offset for the bitmap count below */
362 while (++offset
< sb
->s_blocksize
/ 4)
363 ((__be32
*)bh
->b_data
)[offset
] = 0;
364 ((__be32
*)bh
->b_data
)[0] = 0;
365 ((__be32
*)bh
->b_data
)[0] = cpu_to_be32(-affs_checksum_block(sb
, bh
));
366 mark_buffer_dirty(bh
);
368 /* recalculate bitmap count for last block */
370 bm
->bm_free
= affs_count_free_bits(sb
->s_blocksize
- 4, bh
->b_data
+ 4);
374 affs_brelse(bmap_bh
);
378 void affs_free_bitmap(struct super_block
*sb
)
380 struct affs_sb_info
*sbi
= AFFS_SB(sb
);
385 affs_brelse(sbi
->s_bmap_bh
);
386 sbi
->s_bmap_bh
= NULL
;
387 sbi
->s_last_bmap
= ~0;
388 kfree(sbi
->s_bitmap
);
389 sbi
->s_bitmap
= NULL
;