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 <ufs/ufs/quota.h>
39 #include <ufs/ufs/ufsmount.h>
40 #include <gnu/ext2fs/ext2_extern.h>
41 #include <gnu/ext2fs/ext2_fs.h>
42 #include <gnu/ext2fs/ext2_fs_sb.h>
43 #include <gnu/ext2fs/fs.h>
46 #include <gnu/ext2fs/i386-bitops.h>
47 #elif defined (__alpha__)
48 #include <gnu/ext2fs/alpha-bitops.h>
50 #error Provide an bitops.h file, please !
53 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
55 /* got rid of get_group_desc since it can already be found in
59 static void read_block_bitmap (struct mount * mp,
60 unsigned int block_group,
61 unsigned long bitmap_nr)
63 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
64 struct ext2_group_desc * gdp;
65 struct buffer_head * bh;
68 gdp = get_group_desc (mp, block_group, NULL);
69 if ((error = bread (VFSTOUFS(mp)->um_devvp,
70 fsbtodb(sb, gdp->bg_block_bitmap),sb->s_blocksize, NOCRED, &bh)) != 0)
71 panic ( "read_block_bitmap: "
72 "Cannot read block bitmap - "
73 "block_group = %d, block_bitmap = %lu",
74 block_group, (unsigned long) gdp->bg_block_bitmap);
75 sb->s_block_bitmap_number[bitmap_nr] = block_group;
76 sb->s_block_bitmap[bitmap_nr] = bh;
81 * load_block_bitmap loads the block bitmap for a blocks group
83 * It maintains a cache for the last bitmaps loaded. This cache is managed
84 * with a LRU algorithm.
87 * 1/ There is one cache per mounted file system.
88 * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
89 * this function reads the bitmap without maintaining a LRU cache.
91 static int load__block_bitmap (struct mount * mp,
92 unsigned int block_group)
95 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
96 unsigned long block_bitmap_number;
97 struct buffer_head * block_bitmap;
99 if (block_group >= sb->s_groups_count)
100 panic ( "load_block_bitmap: "
101 "block_group >= groups_count - "
102 "block_group = %d, groups_count = %lu",
103 block_group, sb->s_groups_count);
105 if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED) {
106 if (sb->s_block_bitmap[block_group]) {
107 if (sb->s_block_bitmap_number[block_group] !=
109 panic ( "load_block_bitmap: "
110 "block_group != block_bitmap_number");
114 read_block_bitmap (mp, block_group, block_group);
119 for (i = 0; i < sb->s_loaded_block_bitmaps &&
120 sb->s_block_bitmap_number[i] != block_group; i++)
122 if (i < sb->s_loaded_block_bitmaps &&
123 sb->s_block_bitmap_number[i] == block_group) {
124 block_bitmap_number = sb->s_block_bitmap_number[i];
125 block_bitmap = sb->s_block_bitmap[i];
126 for (j = i; j > 0; j--) {
127 sb->s_block_bitmap_number[j] =
128 sb->s_block_bitmap_number[j - 1];
129 sb->s_block_bitmap[j] =
130 sb->s_block_bitmap[j - 1];
132 sb->s_block_bitmap_number[0] = block_bitmap_number;
133 sb->s_block_bitmap[0] = block_bitmap;
135 if (sb->s_loaded_block_bitmaps < EXT2_MAX_GROUP_LOADED)
136 sb->s_loaded_block_bitmaps++;
138 ULCK_BUF(sb->s_block_bitmap[EXT2_MAX_GROUP_LOADED - 1])
140 for (j = sb->s_loaded_block_bitmaps - 1; j > 0; j--) {
141 sb->s_block_bitmap_number[j] =
142 sb->s_block_bitmap_number[j - 1];
143 sb->s_block_bitmap[j] =
144 sb->s_block_bitmap[j - 1];
146 read_block_bitmap (mp, block_group, 0);
151 static __inline int load_block_bitmap (struct mount * mp,
152 unsigned int block_group)
154 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
155 if (sb->s_loaded_block_bitmaps > 0 &&
156 sb->s_block_bitmap_number[0] == block_group)
159 if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED &&
160 sb->s_block_bitmap_number[block_group] == block_group &&
161 sb->s_block_bitmap[block_group])
164 return load__block_bitmap (mp, block_group);
167 void ext2_free_blocks (struct mount * mp, unsigned long block,
170 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
171 struct buffer_head * bh;
172 struct buffer_head * bh2;
173 unsigned long block_group;
177 struct ext2_group_desc * gdp;
178 struct ext2_super_block * es = sb->s_es;
181 printf ("ext2_free_blocks: nonexistent device");
184 lock_super (VFSTOUFS(mp)->um_devvp);
185 if (block < es->s_first_data_block ||
186 (block + count) > es->s_blocks_count) {
187 printf ( "ext2_free_blocks: "
188 "Freeing blocks not in datazone - "
189 "block = %lu, count = %lu", block, count);
190 unlock_super (VFSTOUFS(mp)->um_devvp);
194 ext2_debug ("freeing blocks %lu to %lu\n", block, block+count-1);
196 block_group = (block - es->s_first_data_block) /
197 EXT2_BLOCKS_PER_GROUP(sb);
198 bit = (block - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb);
199 if (bit + count > EXT2_BLOCKS_PER_GROUP(sb))
200 panic ( "ext2_free_blocks: "
201 "Freeing blocks across group boundary - "
202 "Block = %lu, count = %lu",
204 bitmap_nr = load_block_bitmap (mp, block_group);
205 bh = sb->s_block_bitmap[bitmap_nr];
206 gdp = get_group_desc (mp, block_group, &bh2);
208 if (/* test_opt (sb, CHECK_STRICT) && assume always strict ! */
209 (in_range (gdp->bg_block_bitmap, block, count) ||
210 in_range (gdp->bg_inode_bitmap, block, count) ||
211 in_range (block, gdp->bg_inode_table,
212 sb->s_itb_per_group) ||
213 in_range (block + count - 1, gdp->bg_inode_table,
214 sb->s_itb_per_group)))
215 panic ( "ext2_free_blocks: "
216 "Freeing blocks in system zones - "
217 "Block = %lu, count = %lu",
220 for (i = 0; i < count; i++) {
221 if (!clear_bit (bit + i, bh->b_data))
222 printf ("ext2_free_blocks: "
223 "bit already cleared for block %lu",
226 gdp->bg_free_blocks_count++;
227 es->s_free_blocks_count++;
231 mark_buffer_dirty(bh2);
232 mark_buffer_dirty(bh);
234 if (sb->s_flags & MS_SYNCHRONOUS) {
235 ll_rw_block (WRITE, 1, &bh);
240 unlock_super (VFSTOUFS(mp)->um_devvp);
245 * ext2_new_block uses a goal block to assist allocation. If the goal is
246 * free, or there is a free block within 32 blocks of the goal, that block
247 * is allocated. Otherwise a forward search is made for a free block; within
248 * each block group the search first looks for an entire free byte in the block
249 * bitmap, and then for any free bit if that fails.
251 int ext2_new_block (struct mount * mp, unsigned long goal,
252 u_int32_t * prealloc_count,
253 u_int32_t * prealloc_block)
255 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
256 struct buffer_head * bh;
257 struct buffer_head * bh2;
261 struct ext2_group_desc * gdp;
262 struct ext2_super_block * es = sb->s_es;
265 static int goal_hits = 0, goal_attempts = 0;
268 printf ("ext2_new_block: nonexistent device");
271 lock_super (VFSTOUFS(mp)->um_devvp);
273 ext2_debug ("goal=%lu.\n", goal);
277 * First, test whether the goal block is free.
279 if (goal < es->s_first_data_block || goal >= es->s_blocks_count)
280 goal = es->s_first_data_block;
281 i = (goal - es->s_first_data_block) / EXT2_BLOCKS_PER_GROUP(sb);
282 gdp = get_group_desc (mp, i, &bh2);
283 if (gdp->bg_free_blocks_count > 0) {
284 j = ((goal - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb));
289 bitmap_nr = load_block_bitmap (mp, i);
290 bh = sb->s_block_bitmap[bitmap_nr];
292 ext2_debug ("goal is at %d:%d.\n", i, j);
294 if (!test_bit(j, bh->b_data)) {
297 ext2_debug ("goal bit allocated.\n");
303 * The goal was occupied; search forward for a free
304 * block within the next XX blocks.
306 * end_goal is more or less random, but it has to be
307 * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
308 * next 64-bit boundary is simple..
310 int end_goal = (j + 63) & ~63;
311 j = find_next_zero_bit(bh->b_data, end_goal, j);
316 ext2_debug ("Bit not found near goal\n");
319 * There has been no free block found in the near vicinity
320 * of the goal: do a search forward through the block groups,
321 * searching in each group first for an entire free byte in
322 * the bitmap and then for any free bit.
324 * Search first in the remainder of the current group; then,
325 * cyclicly search through the rest of the groups.
327 p = ((char *) bh->b_data) + (j >> 3);
328 r = memscan(p, 0, (EXT2_BLOCKS_PER_GROUP(sb) - j + 7) >> 3);
329 k = (r - ((char *) bh->b_data)) << 3;
330 if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
334 k = find_next_zero_bit ((unsigned long *) bh->b_data,
335 EXT2_BLOCKS_PER_GROUP(sb),
337 if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
343 ext2_debug ("Bit not found in block group %d.\n", i);
346 * Now search the rest of the groups. We assume that
347 * i and gdp correctly point to the last group visited.
349 for (k = 0; k < sb->s_groups_count; k++) {
351 if (i >= sb->s_groups_count)
353 gdp = get_group_desc (mp, i, &bh2);
354 if (gdp->bg_free_blocks_count > 0)
357 if (k >= sb->s_groups_count) {
358 unlock_super (VFSTOUFS(mp)->um_devvp);
361 bitmap_nr = load_block_bitmap (mp, i);
362 bh = sb->s_block_bitmap[bitmap_nr];
363 r = memscan(bh->b_data, 0, EXT2_BLOCKS_PER_GROUP(sb) >> 3);
364 j = (r - bh->b_data) << 3;
366 if (j < EXT2_BLOCKS_PER_GROUP(sb))
369 j = find_first_zero_bit ((unsigned long *) bh->b_data,
370 EXT2_BLOCKS_PER_GROUP(sb));
371 if (j >= EXT2_BLOCKS_PER_GROUP(sb)) {
372 printf ( "ext2_new_block: "
373 "Free blocks count corrupted for block group %d", i);
374 unlock_super (VFSTOUFS(mp)->um_devvp);
380 * We have succeeded in finding a free byte in the block
381 * bitmap. Now search backwards up to 7 bits to find the
382 * start of this group of free blocks.
384 for (k = 0; k < 7 && j > 0 && !test_bit (j - 1, bh->b_data); k++, j--);
388 ext2_debug ("using block group %d(%d)\n", i, gdp->bg_free_blocks_count);
390 tmp = j + i * EXT2_BLOCKS_PER_GROUP(sb) + es->s_first_data_block;
392 if (/* test_opt (sb, CHECK_STRICT) && we are always strict. */
393 (tmp == gdp->bg_block_bitmap ||
394 tmp == gdp->bg_inode_bitmap ||
395 in_range (tmp, gdp->bg_inode_table, sb->s_itb_per_group)))
396 panic ( "ext2_new_block: "
397 "Allocating block in system zone - "
398 "%dth block = %u in group %u", j, tmp, i);
400 if (set_bit (j, bh->b_data)) {
401 printf ( "ext2_new_block: "
402 "bit already set for block %d", j);
406 ext2_debug ("found bit %d\n", j);
409 * Do block preallocation now if required.
411 #ifdef EXT2_PREALLOCATE
412 if (prealloc_block) {
414 *prealloc_block = tmp + 1;
416 k < 8 && (j + k) < EXT2_BLOCKS_PER_GROUP(sb); k++) {
417 if (set_bit (j + k, bh->b_data))
421 gdp->bg_free_blocks_count -= *prealloc_count;
422 es->s_free_blocks_count -= *prealloc_count;
423 ext2_debug ("Preallocated a further %lu bits.\n",
430 mark_buffer_dirty(bh);
432 if (sb->s_flags & MS_SYNCHRONOUS) {
433 ll_rw_block (WRITE, 1, &bh);
437 if (j >= es->s_blocks_count) {
438 printf ( "ext2_new_block: "
439 "block >= blocks count - "
440 "block_group = %d, block=%d", i, j);
441 unlock_super (VFSTOUFS(mp)->um_devvp);
445 ext2_debug ("allocating block %d. "
446 "Goal hits %d of %d.\n", j, goal_hits, goal_attempts);
448 gdp->bg_free_blocks_count--;
449 mark_buffer_dirty(bh2);
450 es->s_free_blocks_count--;
452 unlock_super (VFSTOUFS(mp)->um_devvp);
457 static unsigned long ext2_count_free_blocks (struct mount * mp)
459 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
461 struct ext2_super_block * es;
462 unsigned long desc_count, bitmap_count, x;
464 struct ext2_group_desc * gdp;
467 lock_super (VFSTOUFS(mp)->um_devvp);
472 for (i = 0; i < sb->s_groups_count; i++) {
473 gdp = get_group_desc (mp, i, NULL);
474 desc_count += gdp->bg_free_blocks_count;
475 bitmap_nr = load_block_bitmap (mp, i);
476 x = ext2_count_free (sb->s_block_bitmap[bitmap_nr],
478 ext2_debug ("group %d: stored = %d, counted = %lu\n",
479 i, gdp->bg_free_blocks_count, x);
482 ext2_debug( "stored = %lu, computed = %lu, %lu\n",
483 es->s_free_blocks_count, desc_count, bitmap_count);
484 unlock_super (VFSTOUFS(mp)->um_devvp);
487 return sb->s_es->s_free_blocks_count;
492 static __inline int block_in_use (unsigned long block,
493 struct ext2_sb_info * sb,
496 return test_bit ((block - sb->s_es->s_first_data_block) %
497 EXT2_BLOCKS_PER_GROUP(sb), map);
500 static int test_root(int a, int b)
513 int ext2_group_sparse(int group)
515 return (test_root(group, 3) || test_root(group, 5) ||
516 test_root(group, 7));
520 static void ext2_check_blocks_bitmap (struct mount * mp)
522 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
523 struct buffer_head * bh;
524 struct ext2_super_block * es;
525 unsigned long desc_count, bitmap_count, x;
526 unsigned long desc_blocks;
528 struct ext2_group_desc * gdp;
531 lock_super (VFSTOUFS(mp)->um_devvp);
536 desc_blocks = (sb->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
537 EXT2_DESC_PER_BLOCK(sb);
538 for (i = 0; i < sb->s_groups_count; i++) {
539 gdp = get_group_desc (mp, i, NULL);
540 desc_count += gdp->bg_free_blocks_count;
541 bitmap_nr = load_block_bitmap (mp, i);
542 bh = sb->s_block_bitmap[bitmap_nr];
544 if (!(es->s_feature_ro_compat &
545 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
546 ext2_group_sparse(i)) {
547 if (!test_bit (0, bh->b_data))
548 printf ("ext2_check_blocks_bitmap: "
549 "Superblock in group %d "
550 "is marked free", i);
552 for (j = 0; j < desc_blocks; j++)
553 if (!test_bit (j + 1, bh->b_data))
554 printf ("ext2_check_blocks_bitmap: "
555 "Descriptor block #%d in group "
556 "%d is marked free", j, i);
559 if (!block_in_use (gdp->bg_block_bitmap, sb, bh->b_data))
560 printf ("ext2_check_blocks_bitmap: "
561 "Block bitmap for group %d is marked free",
564 if (!block_in_use (gdp->bg_inode_bitmap, sb, bh->b_data))
565 printf ("ext2_check_blocks_bitmap: "
566 "Inode bitmap for group %d is marked free",
569 for (j = 0; j < sb->s_itb_per_group; j++)
570 if (!block_in_use (gdp->bg_inode_table + j, sb, bh->b_data))
571 printf ("ext2_check_blocks_bitmap: "
572 "Block #%d of the inode table in "
573 "group %d is marked free", j, i);
575 x = ext2_count_free (bh, sb->s_blocksize);
576 if (gdp->bg_free_blocks_count != x)
577 printf ("ext2_check_blocks_bitmap: "
578 "Wrong free blocks count for group %d, "
579 "stored = %d, counted = %lu", i,
580 gdp->bg_free_blocks_count, x);
583 if (es->s_free_blocks_count != bitmap_count)
584 printf ("ext2_check_blocks_bitmap: "
585 "Wrong free blocks count in super block, "
586 "stored = %lu, counted = %lu",
587 (unsigned long) es->s_free_blocks_count, bitmap_count);
588 unlock_super (VFSTOUFS(mp)->um_devvp);
593 * this function is taken from
594 * linux/fs/ext2/bitmap.c
597 static int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0};
599 unsigned long ext2_count_free (struct buffer_head * map, unsigned int numchars)
602 unsigned long sum = 0;
606 for (i = 0; i < numchars; i++)
607 sum += nibblemap[map->b_data[i] & 0xf] +
608 nibblemap[(map->b_data[i] >> 4) & 0xf];