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 $
8 * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_linux_balloc.c,v 1.6 2006/01/13 21:09:27 swildner Exp $
11 * linux/fs/ext2/balloc.c
13 * Copyright (C) 1992, 1993, 1994, 1995
14 * Remy Card (card@masi.ibp.fr)
15 * Laboratoire MASI - Institut Blaise Pascal
16 * Universite Pierre et Marie Curie (Paris VI)
18 * Enhanced block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
22 * The free blocks are managed by bitmaps. A file system contains several
23 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
24 * block for inodes, N blocks for the inode table and data blocks.
26 * The file system contains group descriptors which are located after the
27 * super block. Each descriptor contains the number of the bitmap block and
28 * the free blocks count in the block. The descriptors are loaded in memory
29 * when a file system is mounted (see ext2_read_super).
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/mount.h>
37 #include <sys/vnode.h>
39 #include <sys/thread2.h>
41 #include <vfs/ufs/quota.h>
42 #include <vfs/ufs/ufsmount.h>
43 #include "ext2_extern.h"
45 #include "ext2_fs_sb.h"
49 #include "i386-bitops.h"
50 #elif defined (__alpha__)
51 #include "alpha-bitops.h"
53 #error Provide an bitops.h file, please !
56 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
58 /* got rid of get_group_desc since it can already be found in
63 read_block_bitmap(struct mount *mp, unsigned int block_group,
64 unsigned long bitmap_nr)
66 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
67 struct ext2_group_desc * gdp;
68 struct buffer_head * bh;
71 gdp = get_group_desc (mp, block_group, NULL);
72 if ((error = bread (VFSTOUFS(mp)->um_devvp,
73 fsbtodb(sb, gdp->bg_block_bitmap),sb->s_blocksize, &bh)) != 0)
74 panic ( "read_block_bitmap: "
75 "Cannot read block bitmap - "
76 "block_group = %d, block_bitmap = %lu",
77 block_group, (unsigned long) gdp->bg_block_bitmap);
78 sb->s_block_bitmap_number[bitmap_nr] = block_group;
79 sb->s_block_bitmap[bitmap_nr] = bh;
84 * load_block_bitmap loads the block bitmap for a blocks group
86 * It maintains a cache for the last bitmaps loaded. This cache is managed
87 * with a LRU algorithm.
90 * 1/ There is one cache per mounted file system.
91 * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
92 * this function reads the bitmap without maintaining a LRU cache.
95 load__block_bitmap(struct mount *mp, unsigned int block_group)
98 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
99 unsigned long block_bitmap_number;
100 struct buffer_head * block_bitmap;
102 if (block_group >= sb->s_groups_count)
103 panic ( "load_block_bitmap: "
104 "block_group >= groups_count - "
105 "block_group = %d, groups_count = %lu",
106 block_group, sb->s_groups_count);
108 if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED) {
109 if (sb->s_block_bitmap[block_group]) {
110 if (sb->s_block_bitmap_number[block_group] !=
112 panic ( "load_block_bitmap: "
113 "block_group != block_bitmap_number");
117 read_block_bitmap (mp, block_group, block_group);
122 for (i = 0; i < sb->s_loaded_block_bitmaps &&
123 sb->s_block_bitmap_number[i] != block_group; i++)
125 if (i < sb->s_loaded_block_bitmaps &&
126 sb->s_block_bitmap_number[i] == block_group) {
127 block_bitmap_number = sb->s_block_bitmap_number[i];
128 block_bitmap = sb->s_block_bitmap[i];
129 for (j = i; j > 0; j--) {
130 sb->s_block_bitmap_number[j] =
131 sb->s_block_bitmap_number[j - 1];
132 sb->s_block_bitmap[j] =
133 sb->s_block_bitmap[j - 1];
135 sb->s_block_bitmap_number[0] = block_bitmap_number;
136 sb->s_block_bitmap[0] = block_bitmap;
138 if (sb->s_loaded_block_bitmaps < EXT2_MAX_GROUP_LOADED)
139 sb->s_loaded_block_bitmaps++;
141 ULCK_BUF(sb->s_block_bitmap[EXT2_MAX_GROUP_LOADED - 1])
143 for (j = sb->s_loaded_block_bitmaps - 1; j > 0; j--) {
144 sb->s_block_bitmap_number[j] =
145 sb->s_block_bitmap_number[j - 1];
146 sb->s_block_bitmap[j] =
147 sb->s_block_bitmap[j - 1];
149 read_block_bitmap (mp, block_group, 0);
155 load_block_bitmap(struct mount * mp, unsigned int block_group)
157 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
158 if (sb->s_loaded_block_bitmaps > 0 &&
159 sb->s_block_bitmap_number[0] == block_group)
162 if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED &&
163 sb->s_block_bitmap_number[block_group] == block_group &&
164 sb->s_block_bitmap[block_group])
167 return load__block_bitmap (mp, block_group);
171 ext2_free_blocks(struct mount * mp, unsigned long block,
174 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
175 struct buffer_head * bh;
176 struct buffer_head * bh2;
177 unsigned long block_group;
181 struct ext2_group_desc * gdp;
182 struct ext2_super_block * es = sb->s_es;
185 printf ("ext2_free_blocks: nonexistent device");
188 lock_super (VFSTOUFS(mp)->um_devvp);
189 if (block < es->s_first_data_block ||
190 (block + count) > es->s_blocks_count) {
191 printf ( "ext2_free_blocks: "
192 "Freeing blocks not in datazone - "
193 "block = %lu, count = %lu", block, count);
194 unlock_super (VFSTOUFS(mp)->um_devvp);
198 ext2_debug ("freeing blocks %lu to %lu\n", block, block+count-1);
200 block_group = (block - es->s_first_data_block) /
201 EXT2_BLOCKS_PER_GROUP(sb);
202 bit = (block - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb);
203 if (bit + count > EXT2_BLOCKS_PER_GROUP(sb))
204 panic ( "ext2_free_blocks: "
205 "Freeing blocks across group boundary - "
206 "Block = %lu, count = %lu",
208 bitmap_nr = load_block_bitmap (mp, block_group);
209 bh = sb->s_block_bitmap[bitmap_nr];
210 gdp = get_group_desc (mp, block_group, &bh2);
212 if (/* test_opt (sb, CHECK_STRICT) && assume always strict ! */
213 (in_range (gdp->bg_block_bitmap, block, count) ||
214 in_range (gdp->bg_inode_bitmap, block, count) ||
215 in_range (block, gdp->bg_inode_table,
216 sb->s_itb_per_group) ||
217 in_range (block + count - 1, gdp->bg_inode_table,
218 sb->s_itb_per_group)))
219 panic ( "ext2_free_blocks: "
220 "Freeing blocks in system zones - "
221 "Block = %lu, count = %lu",
224 for (i = 0; i < count; i++) {
225 if (!clear_bit (bit + i, bh->b_data))
226 printf ("ext2_free_blocks: "
227 "bit already cleared for block %lu",
230 gdp->bg_free_blocks_count++;
231 es->s_free_blocks_count++;
235 mark_buffer_dirty(bh2);
236 mark_buffer_dirty(bh);
238 if (sb->s_flags & MS_SYNCHRONOUS) {
239 ll_rw_block (WRITE, 1, &bh);
244 unlock_super (VFSTOUFS(mp)->um_devvp);
249 * ext2_new_block uses a goal block to assist allocation. If the goal is
250 * free, or there is a free block within 32 blocks of the goal, that block
251 * is allocated. Otherwise a forward search is made for a free block; within
252 * each block group the search first looks for an entire free byte in the block
253 * bitmap, and then for any free bit if that fails.
256 ext2_new_block(struct mount * mp, unsigned long goal,
257 u_int32_t * prealloc_count,
258 u_int32_t * prealloc_block)
260 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
261 struct buffer_head * bh;
262 struct buffer_head * bh2;
266 struct ext2_group_desc * gdp;
267 struct ext2_super_block * es = sb->s_es;
270 static int goal_hits = 0, goal_attempts = 0;
273 printf ("ext2_new_block: nonexistent device");
276 lock_super (VFSTOUFS(mp)->um_devvp);
278 ext2_debug ("goal=%lu.\n", goal);
282 * First, test whether the goal block is free.
284 if (goal < es->s_first_data_block || goal >= es->s_blocks_count)
285 goal = es->s_first_data_block;
286 i = (goal - es->s_first_data_block) / EXT2_BLOCKS_PER_GROUP(sb);
287 gdp = get_group_desc (mp, i, &bh2);
288 if (gdp->bg_free_blocks_count > 0) {
289 j = ((goal - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb));
294 bitmap_nr = load_block_bitmap (mp, i);
295 bh = sb->s_block_bitmap[bitmap_nr];
297 ext2_debug ("goal is at %d:%d.\n", i, j);
299 if (!test_bit(j, bh->b_data)) {
302 ext2_debug ("goal bit allocated.\n");
308 * The goal was occupied; search forward for a free
309 * block within the next XX blocks.
311 * end_goal is more or less random, but it has to be
312 * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
313 * next 64-bit boundary is simple..
315 int end_goal = (j + 63) & ~63;
316 j = find_next_zero_bit(bh->b_data, end_goal, j);
321 ext2_debug ("Bit not found near goal\n");
324 * There has been no free block found in the near vicinity
325 * of the goal: do a search forward through the block groups,
326 * searching in each group first for an entire free byte in
327 * the bitmap and then for any free bit.
329 * Search first in the remainder of the current group; then,
330 * cyclicly search through the rest of the groups.
332 p = ((char *) bh->b_data) + (j >> 3);
333 r = memscan(p, 0, (EXT2_BLOCKS_PER_GROUP(sb) - j + 7) >> 3);
334 k = (r - ((char *) bh->b_data)) << 3;
335 if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
339 k = find_next_zero_bit ((unsigned long *) bh->b_data,
340 EXT2_BLOCKS_PER_GROUP(sb),
342 if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
348 ext2_debug ("Bit not found in block group %d.\n", i);
351 * Now search the rest of the groups. We assume that
352 * i and gdp correctly point to the last group visited.
354 for (k = 0; k < sb->s_groups_count; k++) {
356 if (i >= sb->s_groups_count)
358 gdp = get_group_desc (mp, i, &bh2);
359 if (gdp->bg_free_blocks_count > 0)
362 if (k >= sb->s_groups_count) {
363 unlock_super (VFSTOUFS(mp)->um_devvp);
366 bitmap_nr = load_block_bitmap (mp, i);
367 bh = sb->s_block_bitmap[bitmap_nr];
368 r = memscan(bh->b_data, 0, EXT2_BLOCKS_PER_GROUP(sb) >> 3);
369 j = (r - bh->b_data) << 3;
371 if (j < EXT2_BLOCKS_PER_GROUP(sb))
374 j = find_first_zero_bit ((unsigned long *) bh->b_data,
375 EXT2_BLOCKS_PER_GROUP(sb));
376 if (j >= EXT2_BLOCKS_PER_GROUP(sb)) {
377 printf ( "ext2_new_block: "
378 "Free blocks count corrupted for block group %d", i);
379 unlock_super (VFSTOUFS(mp)->um_devvp);
385 * We have succeeded in finding a free byte in the block
386 * bitmap. Now search backwards up to 7 bits to find the
387 * start of this group of free blocks.
389 for (k = 0; k < 7 && j > 0 && !test_bit (j - 1, bh->b_data); k++, j--);
393 ext2_debug ("using block group %d(%d)\n", i, gdp->bg_free_blocks_count);
395 tmp = j + i * EXT2_BLOCKS_PER_GROUP(sb) + es->s_first_data_block;
397 if (/* test_opt (sb, CHECK_STRICT) && we are always strict. */
398 (tmp == gdp->bg_block_bitmap ||
399 tmp == gdp->bg_inode_bitmap ||
400 in_range (tmp, gdp->bg_inode_table, sb->s_itb_per_group)))
401 panic ( "ext2_new_block: "
402 "Allocating block in system zone - "
403 "%dth block = %u in group %u", j, tmp, i);
405 if (set_bit (j, bh->b_data)) {
406 printf ( "ext2_new_block: "
407 "bit already set for block %d", j);
411 ext2_debug ("found bit %d\n", j);
414 * Do block preallocation now if required.
416 #ifdef EXT2_PREALLOCATE
417 if (prealloc_block) {
419 *prealloc_block = tmp + 1;
421 k < 8 && (j + k) < EXT2_BLOCKS_PER_GROUP(sb); k++) {
422 if (set_bit (j + k, bh->b_data))
426 gdp->bg_free_blocks_count -= *prealloc_count;
427 es->s_free_blocks_count -= *prealloc_count;
428 ext2_debug ("Preallocated a further %lu bits.\n",
435 mark_buffer_dirty(bh);
437 if (sb->s_flags & MS_SYNCHRONOUS) {
438 ll_rw_block (WRITE, 1, &bh);
442 if (j >= es->s_blocks_count) {
443 printf ( "ext2_new_block: "
444 "block >= blocks count - "
445 "block_group = %d, block=%d", i, j);
446 unlock_super (VFSTOUFS(mp)->um_devvp);
450 ext2_debug ("allocating block %d. "
451 "Goal hits %d of %d.\n", j, goal_hits, goal_attempts);
453 gdp->bg_free_blocks_count--;
454 mark_buffer_dirty(bh2);
455 es->s_free_blocks_count--;
457 unlock_super (VFSTOUFS(mp)->um_devvp);
463 ext2_count_free_blocks(struct mount * mp)
465 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
467 struct ext2_super_block * es;
468 unsigned long desc_count, bitmap_count, x;
470 struct ext2_group_desc * gdp;
473 lock_super (VFSTOUFS(mp)->um_devvp);
478 for (i = 0; i < sb->s_groups_count; i++) {
479 gdp = get_group_desc (mp, i, NULL);
480 desc_count += gdp->bg_free_blocks_count;
481 bitmap_nr = load_block_bitmap (mp, i);
482 x = ext2_count_free (sb->s_block_bitmap[bitmap_nr],
484 ext2_debug ("group %d: stored = %d, counted = %lu\n",
485 i, gdp->bg_free_blocks_count, x);
488 ext2_debug( "stored = %lu, computed = %lu, %lu\n",
489 es->s_free_blocks_count, desc_count, bitmap_count);
490 unlock_super (VFSTOUFS(mp)->um_devvp);
493 return sb->s_es->s_free_blocks_count;
499 block_in_use (unsigned long block, struct ext2_sb_info *sb,
502 return test_bit ((block - sb->s_es->s_first_data_block) %
503 EXT2_BLOCKS_PER_GROUP(sb), map);
507 test_root(int a, int b)
521 ext2_group_sparse(int group)
523 return (test_root(group, 3) || test_root(group, 5) ||
524 test_root(group, 7));
529 ext2_check_blocks_bitmap(struct mount * mp)
531 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
532 struct buffer_head * bh;
533 struct ext2_super_block * es;
534 unsigned long desc_count, bitmap_count, x;
535 unsigned long desc_blocks;
537 struct ext2_group_desc * gdp;
540 lock_super (VFSTOUFS(mp)->um_devvp);
545 desc_blocks = (sb->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
546 EXT2_DESC_PER_BLOCK(sb);
547 for (i = 0; i < sb->s_groups_count; i++) {
548 gdp = get_group_desc (mp, i, NULL);
549 desc_count += gdp->bg_free_blocks_count;
550 bitmap_nr = load_block_bitmap (mp, i);
551 bh = sb->s_block_bitmap[bitmap_nr];
553 if (!(es->s_feature_ro_compat &
554 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
555 ext2_group_sparse(i)) {
556 if (!test_bit (0, bh->b_data))
557 printf ("ext2_check_blocks_bitmap: "
558 "Superblock in group %d "
559 "is marked free", i);
561 for (j = 0; j < desc_blocks; j++)
562 if (!test_bit (j + 1, bh->b_data))
563 printf ("ext2_check_blocks_bitmap: "
564 "Descriptor block #%d in group "
565 "%d is marked free", j, i);
568 if (!block_in_use (gdp->bg_block_bitmap, sb, bh->b_data))
569 printf ("ext2_check_blocks_bitmap: "
570 "Block bitmap for group %d is marked free",
573 if (!block_in_use (gdp->bg_inode_bitmap, sb, bh->b_data))
574 printf ("ext2_check_blocks_bitmap: "
575 "Inode bitmap for group %d is marked free",
578 for (j = 0; j < sb->s_itb_per_group; j++)
579 if (!block_in_use (gdp->bg_inode_table + j, sb, bh->b_data))
580 printf ("ext2_check_blocks_bitmap: "
581 "Block #%d of the inode table in "
582 "group %d is marked free", j, i);
584 x = ext2_count_free (bh, sb->s_blocksize);
585 if (gdp->bg_free_blocks_count != x)
586 printf ("ext2_check_blocks_bitmap: "
587 "Wrong free blocks count for group %d, "
588 "stored = %d, counted = %lu", i,
589 gdp->bg_free_blocks_count, x);
592 if (es->s_free_blocks_count != bitmap_count)
593 printf ("ext2_check_blocks_bitmap: "
594 "Wrong free blocks count in super block, "
595 "stored = %lu, counted = %lu",
596 (unsigned long) es->s_free_blocks_count, bitmap_count);
597 unlock_super (VFSTOUFS(mp)->um_devvp);
602 * this function is taken from
603 * linux/fs/ext2/bitmap.c
606 static int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0};
608 unsigned long ext2_count_free (struct buffer_head * map, unsigned int numchars)
611 unsigned long sum = 0;
615 for (i = 0; i < numchars; i++)
616 sum += nibblemap[map->b_data[i] & 0xf] +
617 nibblemap[(map->b_data[i] >> 4) & 0xf];