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.
10 #include <linux/slab.h>
14 affs_count_free_blocks(struct super_block
*sb
)
16 struct affs_bm_info
*bm
;
20 pr_debug("%s()\n", __func__
);
22 if (sb
->s_flags
& MS_RDONLY
)
25 mutex_lock(&AFFS_SB(sb
)->s_bmlock
);
27 bm
= AFFS_SB(sb
)->s_bitmap
;
29 for (i
= AFFS_SB(sb
)->s_bmap_count
; i
> 0; bm
++, i
--)
32 mutex_unlock(&AFFS_SB(sb
)->s_bmlock
);
38 affs_free_block(struct super_block
*sb
, u32 block
)
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
;
46 pr_debug("%s(%u)\n", __func__
, block
);
48 if (block
> sbi
->s_partition_size
)
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
];
56 mutex_lock(&sbi
->s_bmlock
);
59 if (sbi
->s_last_bmap
!= bmap
) {
61 bh
= affs_bread(sb
, bm
->bm_key
);
65 sbi
->s_last_bmap
= bmap
;
68 mask
= 1 << (bit
& 31);
69 data
= (__be32
*)bh
->b_data
+ bit
/ 32 + 1;
72 tmp
= be32_to_cpu(*data
);
75 *data
= cpu_to_be32(tmp
| mask
);
78 tmp
= be32_to_cpu(*(__be32
*)bh
->b_data
);
79 *(__be32
*)bh
->b_data
= cpu_to_be32(tmp
- mask
);
81 mark_buffer_dirty(bh
);
82 affs_mark_sb_dirty(sb
);
85 mutex_unlock(&sbi
->s_bmlock
);
89 affs_warning(sb
,"affs_free_block","Trying to free block %u which is already free", block
);
90 mutex_unlock(&sbi
->s_bmlock
);
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
);
101 affs_error(sb
, "affs_free_block","Block %u outside partition", block
);
105 * Allocate a block in the given allocation zone.
106 * Since we have to byte-swap the bitmap on little-endian
107 * machines, this is rather expensive. Therefore we will
108 * preallocate up to 16 blocks from the same word, if
109 * possible. We are not doing preallocations in the
110 * header zone, though.
114 affs_alloc_block(struct inode
*inode
, u32 goal
)
116 struct super_block
*sb
;
117 struct affs_sb_info
*sbi
;
118 struct affs_bm_info
*bm
;
119 struct buffer_head
*bh
;
120 __be32
*data
, *enddata
;
121 u32 blk
, bmap
, bit
, mask
, mask2
, tmp
;
127 pr_debug("balloc(inode=%lu,goal=%u): ", inode
->i_ino
, goal
);
129 if (AFFS_I(inode
)->i_pa_cnt
) {
130 pr_debug("%d\n", AFFS_I(inode
)->i_lastalloc
+1);
131 AFFS_I(inode
)->i_pa_cnt
--;
132 return ++AFFS_I(inode
)->i_lastalloc
;
135 if (!goal
|| goal
> sbi
->s_partition_size
) {
137 affs_warning(sb
, "affs_balloc", "invalid goal %d", goal
);
138 //if (!AFFS_I(inode)->i_last_block)
139 // affs_warning(sb, "affs_balloc", "no last alloc block");
140 goal
= sbi
->s_reserved
;
143 blk
= goal
- sbi
->s_reserved
;
144 bmap
= blk
/ sbi
->s_bmap_bits
;
145 bm
= &sbi
->s_bitmap
[bmap
];
147 mutex_lock(&sbi
->s_bmlock
);
153 /* search for the next bmap buffer with free bits */
154 i
= sbi
->s_bmap_count
;
160 if (bmap
< sbi
->s_bmap_count
)
162 /* restart search at zero */
165 } while (!bm
->bm_free
);
166 blk
= bmap
* sbi
->s_bmap_bits
;
171 if (sbi
->s_last_bmap
!= bmap
) {
173 bh
= affs_bread(sb
, bm
->bm_key
);
177 sbi
->s_last_bmap
= bmap
;
180 /* find an unused block in this bitmap block */
181 bit
= blk
% sbi
->s_bmap_bits
;
182 data
= (__be32
*)bh
->b_data
+ bit
/ 32 + 1;
183 enddata
= (__be32
*)((u8
*)bh
->b_data
+ sb
->s_blocksize
);
184 mask
= ~0UL << (bit
& 31);
187 tmp
= be32_to_cpu(*data
);
191 /* scan the rest of the buffer */
194 if (++data
>= enddata
)
195 /* didn't find something, can only happen
196 * if scan didn't start at 0, try next bmap
200 tmp
= be32_to_cpu(*data
);
204 /* finally look for a free bit in the word */
205 bit
= ffs(tmp
& mask
) - 1;
206 blk
+= bit
+ sbi
->s_reserved
;
207 mask2
= mask
= 1 << (bit
& 31);
208 AFFS_I(inode
)->i_lastalloc
= blk
;
210 /* prealloc as much as possible within this word */
211 while ((mask2
<<= 1)) {
214 AFFS_I(inode
)->i_pa_cnt
++;
217 bm
->bm_free
-= AFFS_I(inode
)->i_pa_cnt
+ 1;
219 *data
= cpu_to_be32(tmp
& ~mask
);
222 tmp
= be32_to_cpu(*(__be32
*)bh
->b_data
);
223 *(__be32
*)bh
->b_data
= cpu_to_be32(tmp
+ mask
);
225 mark_buffer_dirty(bh
);
226 affs_mark_sb_dirty(sb
);
228 mutex_unlock(&sbi
->s_bmlock
);
230 pr_debug("%d\n", blk
);
234 affs_error(sb
,"affs_read_block","Cannot read bitmap block %u", bm
->bm_key
);
235 sbi
->s_bmap_bh
= NULL
;
236 sbi
->s_last_bmap
= ~0;
238 mutex_unlock(&sbi
->s_bmlock
);
239 pr_debug("failed\n");
243 int affs_init_bitmap(struct super_block
*sb
, int *flags
)
245 struct affs_bm_info
*bm
;
246 struct buffer_head
*bmap_bh
= NULL
, *bh
= NULL
;
248 u32 size
, blk
, end
, offset
, mask
;
250 struct affs_sb_info
*sbi
= AFFS_SB(sb
);
252 if (*flags
& MS_RDONLY
)
255 if (!AFFS_ROOT_TAIL(sb
, sbi
->s_root_bh
)->bm_flag
) {
256 pr_notice("Bitmap invalid - mounting %s read only\n", sb
->s_id
);
261 sbi
->s_last_bmap
= ~0;
262 sbi
->s_bmap_bh
= NULL
;
263 sbi
->s_bmap_bits
= sb
->s_blocksize
* 8 - 32;
264 sbi
->s_bmap_count
= (sbi
->s_partition_size
- sbi
->s_reserved
+
265 sbi
->s_bmap_bits
- 1) / sbi
->s_bmap_bits
;
266 size
= sbi
->s_bmap_count
* sizeof(*bm
);
267 bm
= sbi
->s_bitmap
= kzalloc(size
, GFP_KERNEL
);
268 if (!sbi
->s_bitmap
) {
269 pr_err("Bitmap allocation failed\n");
273 bmap_blk
= (__be32
*)sbi
->s_root_bh
->b_data
;
274 blk
= sb
->s_blocksize
/ 4 - 49;
277 for (i
= sbi
->s_bmap_count
; i
> 0; bm
++, i
--) {
280 bm
->bm_key
= be32_to_cpu(bmap_blk
[blk
]);
281 bh
= affs_bread(sb
, bm
->bm_key
);
283 pr_err("Cannot read bitmap\n");
287 if (affs_checksum_block(sb
, bh
)) {
288 pr_warn("Bitmap %u invalid - mounting %s read only.\n",
289 bm
->bm_key
, sb
->s_id
);
293 pr_debug("read bitmap block %d: %d\n", blk
, bm
->bm_key
);
294 bm
->bm_free
= memweight(bh
->b_data
+ 4, sb
->s_blocksize
- 4);
296 /* Don't try read the extension if this is the last block,
297 * but we also need the right bm pointer below
299 if (++blk
< end
|| i
== 1)
302 affs_brelse(bmap_bh
);
303 bmap_bh
= affs_bread(sb
, be32_to_cpu(bmap_blk
[blk
]));
305 pr_err("Cannot read bitmap extension\n");
309 bmap_blk
= (__be32
*)bmap_bh
->b_data
;
311 end
= sb
->s_blocksize
/ 4 - 1;
314 offset
= (sbi
->s_partition_size
- sbi
->s_reserved
) % sbi
->s_bmap_bits
;
315 mask
= ~(0xFFFFFFFFU
<< (offset
& 31));
316 pr_debug("last word: %d %d %d\n", offset
, offset
/ 32 + 1, mask
);
317 offset
= offset
/ 32 + 1;
322 /* Mark unused bits in the last word as allocated */
323 old
= be32_to_cpu(((__be32
*)bh
->b_data
)[offset
]);
326 ((__be32
*)bh
->b_data
)[offset
] = cpu_to_be32(new);
329 //old = be32_to_cpu(*(__be32 *)bh->b_data);
330 //*(__be32 *)bh->b_data = cpu_to_be32(old - new);
331 //mark_buffer_dirty(bh);
333 /* correct offset for the bitmap count below */
336 while (++offset
< sb
->s_blocksize
/ 4)
337 ((__be32
*)bh
->b_data
)[offset
] = 0;
338 ((__be32
*)bh
->b_data
)[0] = 0;
339 ((__be32
*)bh
->b_data
)[0] = cpu_to_be32(-affs_checksum_block(sb
, bh
));
340 mark_buffer_dirty(bh
);
342 /* recalculate bitmap count for last block */
344 bm
->bm_free
= memweight(bh
->b_data
+ 4, sb
->s_blocksize
- 4);
348 affs_brelse(bmap_bh
);
352 void affs_free_bitmap(struct super_block
*sb
)
354 struct affs_sb_info
*sbi
= AFFS_SB(sb
);
359 affs_brelse(sbi
->s_bmap_bh
);
360 sbi
->s_bmap_bh
= NULL
;
361 sbi
->s_last_bmap
= ~0;
362 kfree(sbi
->s_bitmap
);
363 sbi
->s_bitmap
= NULL
;