1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/affs/bitmap.c
5 * (c) 1996 Hans-Joachim Widmaier
7 * bitmap.c contains the code that handles all bitmap related stuff -
8 * block allocation, deallocation, calculation of free space.
11 #include <linux/slab.h>
15 affs_count_free_blocks(struct super_block
*sb
)
17 struct affs_bm_info
*bm
;
21 pr_debug("%s()\n", __func__
);
26 mutex_lock(&AFFS_SB(sb
)->s_bmlock
);
28 bm
= AFFS_SB(sb
)->s_bitmap
;
30 for (i
= AFFS_SB(sb
)->s_bmap_count
; i
> 0; bm
++, i
--)
33 mutex_unlock(&AFFS_SB(sb
)->s_bmlock
);
39 affs_free_block(struct super_block
*sb
, u32 block
)
41 struct affs_sb_info
*sbi
= AFFS_SB(sb
);
42 struct affs_bm_info
*bm
;
43 struct buffer_head
*bh
;
44 u32 blk
, bmap
, bit
, mask
, tmp
;
47 pr_debug("%s(%u)\n", __func__
, block
);
49 if (block
> sbi
->s_partition_size
)
52 blk
= block
- sbi
->s_reserved
;
53 bmap
= blk
/ sbi
->s_bmap_bits
;
54 bit
= blk
% sbi
->s_bmap_bits
;
55 bm
= &sbi
->s_bitmap
[bmap
];
57 mutex_lock(&sbi
->s_bmlock
);
60 if (sbi
->s_last_bmap
!= bmap
) {
62 bh
= affs_bread(sb
, bm
->bm_key
);
66 sbi
->s_last_bmap
= bmap
;
69 mask
= 1 << (bit
& 31);
70 data
= (__be32
*)bh
->b_data
+ bit
/ 32 + 1;
73 tmp
= be32_to_cpu(*data
);
76 *data
= cpu_to_be32(tmp
| mask
);
79 tmp
= be32_to_cpu(*(__be32
*)bh
->b_data
);
80 *(__be32
*)bh
->b_data
= cpu_to_be32(tmp
- mask
);
82 mark_buffer_dirty(bh
);
83 affs_mark_sb_dirty(sb
);
86 mutex_unlock(&sbi
->s_bmlock
);
90 affs_warning(sb
,"affs_free_block","Trying to free block %u which is already free", block
);
91 mutex_unlock(&sbi
->s_bmlock
);
95 affs_error(sb
,"affs_free_block","Cannot read bitmap block %u", bm
->bm_key
);
96 sbi
->s_bmap_bh
= NULL
;
97 sbi
->s_last_bmap
= ~0;
98 mutex_unlock(&sbi
->s_bmlock
);
102 affs_error(sb
, "affs_free_block","Block %u outside partition", block
);
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.
115 affs_alloc_block(struct inode
*inode
, u32 goal
)
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
;
128 pr_debug("balloc(inode=%lu,goal=%u): ", inode
->i_ino
, goal
);
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
;
136 if (!goal
|| goal
> sbi
->s_partition_size
) {
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
;
144 blk
= goal
- sbi
->s_reserved
;
145 bmap
= blk
/ sbi
->s_bmap_bits
;
146 bm
= &sbi
->s_bitmap
[bmap
];
148 mutex_lock(&sbi
->s_bmlock
);
154 /* search for the next bmap buffer with free bits */
155 i
= sbi
->s_bmap_count
;
161 if (bmap
< sbi
->s_bmap_count
)
163 /* restart search at zero */
166 } while (!bm
->bm_free
);
167 blk
= bmap
* sbi
->s_bmap_bits
;
172 if (sbi
->s_last_bmap
!= bmap
) {
174 bh
= affs_bread(sb
, bm
->bm_key
);
178 sbi
->s_last_bmap
= bmap
;
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);
188 tmp
= be32_to_cpu(*data
);
192 /* scan the rest of the buffer */
195 if (++data
>= enddata
)
196 /* didn't find something, can only happen
197 * if scan didn't start at 0, try next bmap
201 tmp
= be32_to_cpu(*data
);
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
;
211 /* prealloc as much as possible within this word */
212 while ((mask2
<<= 1)) {
215 AFFS_I(inode
)->i_pa_cnt
++;
218 bm
->bm_free
-= AFFS_I(inode
)->i_pa_cnt
+ 1;
220 *data
= cpu_to_be32(tmp
& ~mask
);
223 tmp
= be32_to_cpu(*(__be32
*)bh
->b_data
);
224 *(__be32
*)bh
->b_data
= cpu_to_be32(tmp
+ mask
);
226 mark_buffer_dirty(bh
);
227 affs_mark_sb_dirty(sb
);
229 mutex_unlock(&sbi
->s_bmlock
);
231 pr_debug("%d\n", blk
);
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;
239 mutex_unlock(&sbi
->s_bmlock
);
240 pr_debug("failed\n");
244 int affs_init_bitmap(struct super_block
*sb
, int *flags
)
246 struct affs_bm_info
*bm
;
247 struct buffer_head
*bmap_bh
= NULL
, *bh
= NULL
;
249 u32 size
, blk
, end
, offset
, mask
;
251 struct affs_sb_info
*sbi
= AFFS_SB(sb
);
253 if (*flags
& SB_RDONLY
)
256 if (!AFFS_ROOT_TAIL(sb
, sbi
->s_root_bh
)->bm_flag
) {
257 pr_notice("Bitmap invalid - mounting %s read only\n", sb
->s_id
);
262 sbi
->s_last_bmap
= ~0;
263 sbi
->s_bmap_bh
= NULL
;
264 sbi
->s_bmap_bits
= sb
->s_blocksize
* 8 - 32;
265 sbi
->s_bmap_count
= (sbi
->s_partition_size
- sbi
->s_reserved
+
266 sbi
->s_bmap_bits
- 1) / sbi
->s_bmap_bits
;
267 size
= sbi
->s_bmap_count
* sizeof(*bm
);
268 bm
= sbi
->s_bitmap
= kzalloc(size
, GFP_KERNEL
);
269 if (!sbi
->s_bitmap
) {
270 pr_err("Bitmap allocation failed\n");
274 bmap_blk
= (__be32
*)sbi
->s_root_bh
->b_data
;
275 blk
= sb
->s_blocksize
/ 4 - 49;
278 for (i
= sbi
->s_bmap_count
; i
> 0; bm
++, i
--) {
281 bm
->bm_key
= be32_to_cpu(bmap_blk
[blk
]);
282 bh
= affs_bread(sb
, bm
->bm_key
);
284 pr_err("Cannot read bitmap\n");
288 if (affs_checksum_block(sb
, bh
)) {
289 pr_warn("Bitmap %u invalid - mounting %s read only.\n",
290 bm
->bm_key
, sb
->s_id
);
294 pr_debug("read bitmap block %d: %d\n", blk
, bm
->bm_key
);
295 bm
->bm_free
= memweight(bh
->b_data
+ 4, sb
->s_blocksize
- 4);
297 /* Don't try read the extension if this is the last block,
298 * but we also need the right bm pointer below
300 if (++blk
< end
|| i
== 1)
303 affs_brelse(bmap_bh
);
304 bmap_bh
= affs_bread(sb
, be32_to_cpu(bmap_blk
[blk
]));
306 pr_err("Cannot read bitmap extension\n");
310 bmap_blk
= (__be32
*)bmap_bh
->b_data
;
312 end
= sb
->s_blocksize
/ 4 - 1;
315 offset
= (sbi
->s_partition_size
- sbi
->s_reserved
) % sbi
->s_bmap_bits
;
316 mask
= ~(0xFFFFFFFFU
<< (offset
& 31));
317 pr_debug("last word: %d %d %d\n", offset
, offset
/ 32 + 1, mask
);
318 offset
= offset
/ 32 + 1;
323 /* Mark unused bits in the last word as allocated */
324 old
= be32_to_cpu(((__be32
*)bh
->b_data
)[offset
]);
327 ((__be32
*)bh
->b_data
)[offset
] = cpu_to_be32(new);
330 //old = be32_to_cpu(*(__be32 *)bh->b_data);
331 //*(__be32 *)bh->b_data = cpu_to_be32(old - new);
332 //mark_buffer_dirty(bh);
334 /* correct offset for the bitmap count below */
337 while (++offset
< sb
->s_blocksize
/ 4)
338 ((__be32
*)bh
->b_data
)[offset
] = 0;
339 ((__be32
*)bh
->b_data
)[0] = 0;
340 ((__be32
*)bh
->b_data
)[0] = cpu_to_be32(-affs_checksum_block(sb
, bh
));
341 mark_buffer_dirty(bh
);
343 /* recalculate bitmap count for last block */
345 bm
->bm_free
= memweight(bh
->b_data
+ 4, sb
->s_blocksize
- 4);
349 affs_brelse(bmap_bh
);
353 void affs_free_bitmap(struct super_block
*sb
)
355 struct affs_sb_info
*sbi
= AFFS_SB(sb
);
360 affs_brelse(sbi
->s_bmap_bh
);
361 sbi
->s_bmap_bh
= NULL
;
362 sbi
->s_last_bmap
= ~0;
363 kfree(sbi
->s_bitmap
);
364 sbi
->s_bitmap
= NULL
;