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