Linux Audio

Check our new training course

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