2 * modified for Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
7 * $FreeBSD: src/sys/gnu/ext2fs/ext2_linux_balloc.c,v 1.11.2.3 2001/08/14 18:03:19 gallatin Exp $
10 * linux/fs/ext2/balloc.c
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise Pascal
15 * Universite Pierre et Marie Curie (Paris VI)
17 * Enhanced block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
21 * The free blocks are managed by bitmaps. A file system contains several
22 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
23 * block for inodes, N blocks for the inode table and data blocks.
25 * The file system contains group descriptors which are located after the
26 * super block. Each descriptor contains the number of the bitmap block and
27 * the free blocks count in the block. The descriptors are loaded in memory
28 * when a file system is mounted (see ext2_read_super).
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/mount.h>
36 #include <sys/vnode.h>
38 #include <sys/thread2.h>
43 #include "ext2mount.h"
44 #include "ext2_extern.h"
46 #include "ext2_fs_sb.h"
50 #include "i386-bitops.h"
52 #include "ext2_bitops.h"
55 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
57 /* got rid of get_group_desc since it can already be found in
62 read_block_bitmap(struct mount *mp, unsigned int block_group,
63 unsigned long bitmap_nr)
65 struct ext2_sb_info *sb = VFSTOEXT2(mp)->um_e2fs;
66 struct ext2_group_desc * gdp;
67 struct buffer_head * bh;
70 gdp = get_group_desc (mp, block_group, NULL);
71 error = bread(VFSTOEXT2(mp)->um_devvp,
72 fsbtodoff(sb, gdp->bg_block_bitmap),
73 sb->s_blocksize, &bh);
75 panic ( "read_block_bitmap: "
76 "Cannot read block bitmap - "
77 "block_group = %d, block_bitmap = %lu",
78 block_group, (unsigned long) gdp->bg_block_bitmap);
80 sb->s_block_bitmap_number[bitmap_nr] = block_group;
81 sb->s_block_bitmap[bitmap_nr] = bh;
86 * load_block_bitmap loads the block bitmap for a blocks group
88 * It maintains a cache for the last bitmaps loaded. This cache is managed
89 * with a LRU algorithm.
92 * 1/ There is one cache per mounted file system.
93 * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
94 * this function reads the bitmap without maintaining a LRU cache.
97 load__block_bitmap(struct mount *mp, unsigned int block_group)
100 struct ext2_sb_info *sb = VFSTOEXT2(mp)->um_e2fs;
101 unsigned long block_bitmap_number;
102 struct buffer_head * block_bitmap;
104 if (block_group >= sb->s_groups_count)
105 panic ( "load_block_bitmap: "
106 "block_group >= groups_count - "
107 "block_group = %d, groups_count = %lu",
108 block_group, sb->s_groups_count);
110 if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED) {
111 if (sb->s_block_bitmap[block_group]) {
112 if (sb->s_block_bitmap_number[block_group] !=
114 panic ( "load_block_bitmap: "
115 "block_group != block_bitmap_number");
119 read_block_bitmap (mp, block_group, block_group);
124 for (i = 0; i < sb->s_loaded_block_bitmaps &&
125 sb->s_block_bitmap_number[i] != block_group; i++)
127 if (i < sb->s_loaded_block_bitmaps &&
128 sb->s_block_bitmap_number[i] == block_group) {
129 block_bitmap_number = sb->s_block_bitmap_number[i];
130 block_bitmap = sb->s_block_bitmap[i];
131 for (j = i; j > 0; j--) {
132 sb->s_block_bitmap_number[j] =
133 sb->s_block_bitmap_number[j - 1];
134 sb->s_block_bitmap[j] =
135 sb->s_block_bitmap[j - 1];
137 sb->s_block_bitmap_number[0] = block_bitmap_number;
138 sb->s_block_bitmap[0] = block_bitmap;
140 if (sb->s_loaded_block_bitmaps < EXT2_MAX_GROUP_LOADED)
141 sb->s_loaded_block_bitmaps++;
143 ULCK_BUF(sb->s_block_bitmap[EXT2_MAX_GROUP_LOADED - 1])
145 for (j = sb->s_loaded_block_bitmaps - 1; j > 0; j--) {
146 sb->s_block_bitmap_number[j] =
147 sb->s_block_bitmap_number[j - 1];
148 sb->s_block_bitmap[j] =
149 sb->s_block_bitmap[j - 1];
151 read_block_bitmap (mp, block_group, 0);
157 load_block_bitmap(struct mount * mp, unsigned int block_group)
159 struct ext2_sb_info *sb = VFSTOEXT2(mp)->um_e2fs;
160 if (sb->s_loaded_block_bitmaps > 0 &&
161 sb->s_block_bitmap_number[0] == block_group)
164 if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED &&
165 sb->s_block_bitmap_number[block_group] == block_group &&
166 sb->s_block_bitmap[block_group])
169 return load__block_bitmap (mp, block_group);
173 ext2_free_blocks(struct mount * mp, unsigned long block,
176 struct ext2_sb_info *sb = VFSTOEXT2(mp)->um_e2fs;
177 struct buffer_head * bh;
178 struct buffer_head * bh2;
179 unsigned long block_group;
183 struct ext2_group_desc * gdp;
184 struct ext2_super_block * es = sb->s_es;
187 kprintf ("ext2_free_blocks: nonexistent device");
190 lock_super (VFSTOEXT2(mp)->um_devvp);
191 if (block < es->s_first_data_block ||
192 (block + count) > es->s_blocks_count) {
193 kprintf ( "ext2_free_blocks: "
194 "Freeing blocks not in datazone - "
195 "block = %lu, count = %lu", block, count);
196 unlock_super (VFSTOEXT2(mp)->um_devvp);
200 ext2_debug ("freeing blocks %lu to %lu\n", block, block+count-1);
202 block_group = (block - es->s_first_data_block) /
203 EXT2_BLOCKS_PER_GROUP(sb);
204 bit = (block - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb);
205 if (bit + count > EXT2_BLOCKS_PER_GROUP(sb))
206 panic ( "ext2_free_blocks: "
207 "Freeing blocks across group boundary - "
208 "Block = %lu, count = %lu",
210 bitmap_nr = load_block_bitmap (mp, block_group);
211 bh = sb->s_block_bitmap[bitmap_nr];
212 gdp = get_group_desc (mp, block_group, &bh2);
214 if (/* test_opt (sb, CHECK_STRICT) && assume always strict ! */
215 (in_range (gdp->bg_block_bitmap, block, count) ||
216 in_range (gdp->bg_inode_bitmap, block, count) ||
217 in_range (block, gdp->bg_inode_table,
218 sb->s_itb_per_group) ||
219 in_range (block + count - 1, gdp->bg_inode_table,
220 sb->s_itb_per_group)))
221 panic ( "ext2_free_blocks: "
222 "Freeing blocks in system zones - "
223 "Block = %lu, count = %lu",
226 for (i = 0; i < count; i++) {
227 if (!clear_bit (bit + i, bh->b_data))
228 kprintf ("ext2_free_blocks: "
229 "bit already cleared for block %lu",
232 gdp->bg_free_blocks_count++;
233 es->s_free_blocks_count++;
237 mark_buffer_dirty(bh2);
238 mark_buffer_dirty(bh);
240 if (sb->s_flags & MS_SYNCHRONOUS) {
241 ll_rw_block (WRITE, 1, &bh);
246 unlock_super (VFSTOEXT2(mp)->um_devvp);
251 * ext2_new_block uses a goal block to assist allocation. If the goal is
252 * free, or there is a free block within 32 blocks of the goal, that block
253 * is allocated. Otherwise a forward search is made for a free block; within
254 * each block group the search first looks for an entire free byte in the block
255 * bitmap, and then for any free bit if that fails.
258 ext2_new_block(struct mount * mp, unsigned long goal,
259 u_int32_t * prealloc_count,
260 u_int32_t * prealloc_block)
262 struct ext2_sb_info *sb = VFSTOEXT2(mp)->um_e2fs;
263 struct buffer_head * bh;
264 struct buffer_head * bh2;
268 struct ext2_group_desc * gdp;
269 struct ext2_super_block * es = sb->s_es;
272 static int goal_hits = 0, goal_attempts = 0;
275 kprintf ("ext2_new_block: nonexistent device");
278 lock_super (VFSTOEXT2(mp)->um_devvp);
280 ext2_debug ("goal=%lu.\n", goal);
284 * First, test whether the goal block is free.
286 if (goal < es->s_first_data_block || goal >= es->s_blocks_count)
287 goal = es->s_first_data_block;
288 i = (goal - es->s_first_data_block) / EXT2_BLOCKS_PER_GROUP(sb);
289 gdp = get_group_desc (mp, i, &bh2);
290 if (gdp->bg_free_blocks_count > 0) {
291 j = ((goal - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb));
296 bitmap_nr = load_block_bitmap (mp, i);
297 bh = sb->s_block_bitmap[bitmap_nr];
299 ext2_debug ("goal is at %d:%d.\n", i, j);
301 if (!test_bit(j, bh->b_data)) {
304 ext2_debug ("goal bit allocated.\n");
310 * The goal was occupied; search forward for a free
311 * block within the next XX blocks.
313 * end_goal is more or less random, but it has to be
314 * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
315 * next 64-bit boundary is simple..
317 int end_goal = (j + 63) & ~63;
318 j = find_next_zero_bit(bh->b_data, end_goal, j);
323 ext2_debug ("Bit not found near goal\n");
326 * There has been no free block found in the near vicinity
327 * of the goal: do a search forward through the block groups,
328 * searching in each group first for an entire free byte in
329 * the bitmap and then for any free bit.
331 * Search first in the remainder of the current group; then,
332 * cyclicly search through the rest of the groups.
334 p = ((char *) bh->b_data) + (j >> 3);
335 r = memscan(p, 0, (EXT2_BLOCKS_PER_GROUP(sb) - j + 7) >> 3);
336 k = (r - ((char *) bh->b_data)) << 3;
337 if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
341 k = find_next_zero_bit ((unsigned long *) bh->b_data,
342 EXT2_BLOCKS_PER_GROUP(sb),
344 if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
350 ext2_debug ("Bit not found in block group %d.\n", i);
353 * Now search the rest of the groups. We assume that
354 * i and gdp correctly point to the last group visited.
356 for (k = 0; k < sb->s_groups_count; k++) {
358 if (i >= sb->s_groups_count)
360 gdp = get_group_desc (mp, i, &bh2);
361 if (gdp->bg_free_blocks_count > 0)
364 if (k >= sb->s_groups_count) {
365 unlock_super (VFSTOEXT2(mp)->um_devvp);
368 bitmap_nr = load_block_bitmap (mp, i);
369 bh = sb->s_block_bitmap[bitmap_nr];
370 r = memscan(bh->b_data, 0, EXT2_BLOCKS_PER_GROUP(sb) >> 3);
371 j = (r - bh->b_data) << 3;
373 if (j < EXT2_BLOCKS_PER_GROUP(sb))
376 j = find_first_zero_bit ((unsigned long *) bh->b_data,
377 EXT2_BLOCKS_PER_GROUP(sb));
378 if (j >= EXT2_BLOCKS_PER_GROUP(sb)) {
379 kprintf ( "ext2_new_block: "
380 "Free blocks count corrupted for block group %d", i);
381 unlock_super (VFSTOEXT2(mp)->um_devvp);
387 * We have succeeded in finding a free byte in the block
388 * bitmap. Now search backwards up to 7 bits to find the
389 * start of this group of free blocks.
391 for (k = 0; k < 7 && j > 0 && !test_bit (j - 1, bh->b_data); k++, j--);
395 ext2_debug ("using block group %d(%d)\n", i, gdp->bg_free_blocks_count);
397 tmp = j + i * EXT2_BLOCKS_PER_GROUP(sb) + es->s_first_data_block;
399 if (/* test_opt (sb, CHECK_STRICT) && we are always strict. */
400 (tmp == gdp->bg_block_bitmap ||
401 tmp == gdp->bg_inode_bitmap ||
402 in_range (tmp, gdp->bg_inode_table, sb->s_itb_per_group)))
403 panic ( "ext2_new_block: "
404 "Allocating block in system zone - "
405 "%dth block = %u in group %u", j, tmp, i);
407 if (set_bit (j, bh->b_data)) {
408 kprintf ( "ext2_new_block: "
409 "bit already set for block %d", j);
413 ext2_debug ("found bit %d\n", j);
416 * Do block preallocation now if required.
418 #ifdef EXT2_PREALLOCATE
419 if (prealloc_block) {
421 *prealloc_block = tmp + 1;
423 k < 8 && (j + k) < EXT2_BLOCKS_PER_GROUP(sb); k++) {
424 if (set_bit (j + k, bh->b_data))
428 gdp->bg_free_blocks_count -= *prealloc_count;
429 es->s_free_blocks_count -= *prealloc_count;
430 ext2_debug ("Preallocated a further %lu bits.\n",
437 mark_buffer_dirty(bh);
439 if (sb->s_flags & MS_SYNCHRONOUS) {
440 ll_rw_block (WRITE, 1, &bh);
444 if (j >= es->s_blocks_count) {
445 kprintf ( "ext2_new_block: "
446 "block >= blocks count - "
447 "block_group = %d, block=%d", i, j);
448 unlock_super (VFSTOEXT2(mp)->um_devvp);
452 ext2_debug ("allocating block %d. "
453 "Goal hits %d of %d.\n", j, goal_hits, goal_attempts);
455 gdp->bg_free_blocks_count--;
456 mark_buffer_dirty(bh2);
457 es->s_free_blocks_count--;
459 unlock_super (VFSTOEXT2(mp)->um_devvp);
465 ext2_count_free_blocks(struct mount * mp)
467 struct ext2_sb_info *sb = VFSTOEXT2(mp)->um_e2fs;
469 struct ext2_super_block * es;
470 unsigned long desc_count, bitmap_count, x;
472 struct ext2_group_desc * gdp;
475 lock_super (VFSTOEXT2(mp)->um_devvp);
480 for (i = 0; i < sb->s_groups_count; i++) {
481 gdp = get_group_desc (mp, i, NULL);
482 desc_count += gdp->bg_free_blocks_count;
483 bitmap_nr = load_block_bitmap (mp, i);
484 x = ext2_count_free (sb->s_block_bitmap[bitmap_nr],
486 ext2_debug ("group %d: stored = %d, counted = %lu\n",
487 i, gdp->bg_free_blocks_count, x);
490 ext2_debug( "stored = %lu, computed = %lu, %lu\n",
491 es->s_free_blocks_count, desc_count, bitmap_count);
492 unlock_super (VFSTOEXT2(mp)->um_devvp);
495 return sb->s_es->s_free_blocks_count;
501 block_in_use (unsigned long block, struct ext2_sb_info *sb,
504 return test_bit ((block - sb->s_es->s_first_data_block) %
505 EXT2_BLOCKS_PER_GROUP(sb), map);
509 test_root(int a, int b)
523 ext2_group_sparse(int group)
525 return (test_root(group, 3) || test_root(group, 5) ||
526 test_root(group, 7));
531 ext2_check_blocks_bitmap(struct mount * mp)
533 struct ext2_sb_info *sb = VFSTOEXT2(mp)->um_e2fs;
534 struct buffer_head * bh;
535 struct ext2_super_block * es;
536 unsigned long desc_count, bitmap_count, x;
537 unsigned long desc_blocks;
539 struct ext2_group_desc * gdp;
542 lock_super (VFSTOEXT2(mp)->um_devvp);
547 desc_blocks = (sb->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
548 EXT2_DESC_PER_BLOCK(sb);
549 for (i = 0; i < sb->s_groups_count; i++) {
550 gdp = get_group_desc (mp, i, NULL);
551 desc_count += gdp->bg_free_blocks_count;
552 bitmap_nr = load_block_bitmap (mp, i);
553 bh = sb->s_block_bitmap[bitmap_nr];
555 if (!(es->s_feature_ro_compat &
556 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
557 ext2_group_sparse(i)) {
558 if (!test_bit (0, bh->b_data))
559 kprintf ("ext2_check_blocks_bitmap: "
560 "Superblock in group %d "
561 "is marked free", i);
563 for (j = 0; j < desc_blocks; j++)
564 if (!test_bit (j + 1, bh->b_data))
565 kprintf ("ext2_check_blocks_bitmap: "
566 "Descriptor block #%d in group "
567 "%d is marked free", j, i);
570 if (!block_in_use (gdp->bg_block_bitmap, sb, bh->b_data))
571 kprintf ("ext2_check_blocks_bitmap: "
572 "Block bitmap for group %d is marked free",
575 if (!block_in_use (gdp->bg_inode_bitmap, sb, bh->b_data))
576 kprintf ("ext2_check_blocks_bitmap: "
577 "Inode bitmap for group %d is marked free",
580 for (j = 0; j < sb->s_itb_per_group; j++)
581 if (!block_in_use (gdp->bg_inode_table + j, sb, bh->b_data))
582 kprintf ("ext2_check_blocks_bitmap: "
583 "Block #%d of the inode table in "
584 "group %d is marked free", j, i);
586 x = ext2_count_free (bh, sb->s_blocksize);
587 if (gdp->bg_free_blocks_count != x)
588 kprintf ("ext2_check_blocks_bitmap: "
589 "Wrong free blocks count for group %d, "
590 "stored = %d, counted = %lu", i,
591 gdp->bg_free_blocks_count, x);
594 if (es->s_free_blocks_count != bitmap_count)
595 kprintf ("ext2_check_blocks_bitmap: "
596 "Wrong free blocks count in super block, "
597 "stored = %lu, counted = %lu",
598 (unsigned long) es->s_free_blocks_count, bitmap_count);
599 unlock_super (VFSTOEXT2(mp)->um_devvp);
604 * this function is taken from
605 * linux/fs/ext2/bitmap.c
608 static int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0};
610 unsigned long ext2_count_free (struct buffer_head * map, unsigned int numchars)
613 unsigned long sum = 0;
617 for (i = 0; i < numchars; i++)
618 sum += nibblemap[map->b_data[i] & 0xf] +
619 nibblemap[(map->b_data[i] >> 4) & 0xf];