* Remove (void) casts for discarded return values.
[dragonfly.git] / sys / vfs / gnu / ext2fs / ext2_linux_balloc.c
CommitLineData
984263bc
MD
1/*
2 * modified for Lites 1.1
3 *
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
6 *
7 * $FreeBSD: src/sys/gnu/ext2fs/ext2_linux_balloc.c,v 1.11.2.3 2001/08/14 18:03:19 gallatin Exp $
61670a01 8 * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_linux_balloc.c,v 1.5 2005/06/06 15:35:06 dillon Exp $
984263bc
MD
9 */
10/*
11 * linux/fs/ext2/balloc.c
12 *
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)
17 *
18 * Enhanced block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
19 */
20
21/*
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.
25 *
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).
30 */
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/buf.h>
35#include <sys/proc.h>
36#include <sys/mount.h>
37#include <sys/vnode.h>
7b95be2a 38#include <sys/buf2.h>
61670a01 39#include <sys/thread2.h>
984263bc 40
1f2de5d4
MD
41#include <vfs/ufs/quota.h>
42#include <vfs/ufs/ufsmount.h>
43#include "ext2_extern.h"
44#include "ext2_fs.h"
45#include "ext2_fs_sb.h"
46#include "fs.h"
984263bc
MD
47
48#ifdef __i386__
1f2de5d4 49#include "i386-bitops.h"
984263bc 50#elif defined (__alpha__)
1f2de5d4 51#include "alpha-bitops.h"
984263bc
MD
52#else
53#error Provide an bitops.h file, please !
54#endif
55
56#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
57
58/* got rid of get_group_desc since it can already be found in
59 * ext2_linux_ialloc.c
60 */
61
62static void read_block_bitmap (struct mount * mp,
63 unsigned int block_group,
64 unsigned long bitmap_nr)
65{
66 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
67 struct ext2_group_desc * gdp;
68 struct buffer_head * bh;
69 int error;
70
71 gdp = get_group_desc (mp, block_group, NULL);
72 if ((error = bread (VFSTOUFS(mp)->um_devvp,
7b95be2a 73 fsbtodb(sb, gdp->bg_block_bitmap),sb->s_blocksize, &bh)) != 0)
984263bc
MD
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;
80 LCK_BUF(bh)
81}
82
83/*
84 * load_block_bitmap loads the block bitmap for a blocks group
85 *
86 * It maintains a cache for the last bitmaps loaded. This cache is managed
87 * with a LRU algorithm.
88 *
89 * Notes:
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.
93 */
94static int load__block_bitmap (struct mount * mp,
95 unsigned int block_group)
96{
97 int i, j;
98 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
99 unsigned long block_bitmap_number;
100 struct buffer_head * block_bitmap;
101
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);
107
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] !=
111 block_group)
112 panic ( "load_block_bitmap: "
113 "block_group != block_bitmap_number");
114 else
115 return block_group;
116 } else {
117 read_block_bitmap (mp, block_group, block_group);
118 return block_group;
119 }
120 }
121
122 for (i = 0; i < sb->s_loaded_block_bitmaps &&
123 sb->s_block_bitmap_number[i] != block_group; i++)
124 ;
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];
134 }
135 sb->s_block_bitmap_number[0] = block_bitmap_number;
136 sb->s_block_bitmap[0] = block_bitmap;
137 } else {
138 if (sb->s_loaded_block_bitmaps < EXT2_MAX_GROUP_LOADED)
139 sb->s_loaded_block_bitmaps++;
140 else
141 ULCK_BUF(sb->s_block_bitmap[EXT2_MAX_GROUP_LOADED - 1])
142
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];
148 }
149 read_block_bitmap (mp, block_group, 0);
150 }
151 return 0;
152}
153
154static __inline int load_block_bitmap (struct mount * mp,
155 unsigned int block_group)
156{
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)
160 return 0;
161
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])
165 return block_group;
166
167 return load__block_bitmap (mp, block_group);
168}
169
170void ext2_free_blocks (struct mount * mp, unsigned long block,
171 unsigned long count)
172{
173 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
174 struct buffer_head * bh;
175 struct buffer_head * bh2;
176 unsigned long block_group;
177 unsigned long bit;
178 unsigned long i;
179 int bitmap_nr;
180 struct ext2_group_desc * gdp;
181 struct ext2_super_block * es = sb->s_es;
182
183 if (!sb) {
184 printf ("ext2_free_blocks: nonexistent device");
185 return;
186 }
187 lock_super (VFSTOUFS(mp)->um_devvp);
188 if (block < es->s_first_data_block ||
189 (block + count) > es->s_blocks_count) {
190 printf ( "ext2_free_blocks: "
191 "Freeing blocks not in datazone - "
192 "block = %lu, count = %lu", block, count);
193 unlock_super (VFSTOUFS(mp)->um_devvp);
194 return;
195 }
196
197 ext2_debug ("freeing blocks %lu to %lu\n", block, block+count-1);
198
199 block_group = (block - es->s_first_data_block) /
200 EXT2_BLOCKS_PER_GROUP(sb);
201 bit = (block - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb);
202 if (bit + count > EXT2_BLOCKS_PER_GROUP(sb))
203 panic ( "ext2_free_blocks: "
204 "Freeing blocks across group boundary - "
205 "Block = %lu, count = %lu",
206 block, count);
207 bitmap_nr = load_block_bitmap (mp, block_group);
208 bh = sb->s_block_bitmap[bitmap_nr];
209 gdp = get_group_desc (mp, block_group, &bh2);
210
211 if (/* test_opt (sb, CHECK_STRICT) && assume always strict ! */
212 (in_range (gdp->bg_block_bitmap, block, count) ||
213 in_range (gdp->bg_inode_bitmap, block, count) ||
214 in_range (block, gdp->bg_inode_table,
215 sb->s_itb_per_group) ||
216 in_range (block + count - 1, gdp->bg_inode_table,
217 sb->s_itb_per_group)))
218 panic ( "ext2_free_blocks: "
219 "Freeing blocks in system zones - "
220 "Block = %lu, count = %lu",
221 block, count);
222
223 for (i = 0; i < count; i++) {
224 if (!clear_bit (bit + i, bh->b_data))
225 printf ("ext2_free_blocks: "
226 "bit already cleared for block %lu",
227 block);
228 else {
229 gdp->bg_free_blocks_count++;
230 es->s_free_blocks_count++;
231 }
232 }
233
234 mark_buffer_dirty(bh2);
235 mark_buffer_dirty(bh);
236/****
237 if (sb->s_flags & MS_SYNCHRONOUS) {
238 ll_rw_block (WRITE, 1, &bh);
239 wait_on_buffer (bh);
240 }
241****/
242 sb->s_dirt = 1;
243 unlock_super (VFSTOUFS(mp)->um_devvp);
244 return;
245}
246
247/*
248 * ext2_new_block uses a goal block to assist allocation. If the goal is
249 * free, or there is a free block within 32 blocks of the goal, that block
250 * is allocated. Otherwise a forward search is made for a free block; within
251 * each block group the search first looks for an entire free byte in the block
252 * bitmap, and then for any free bit if that fails.
253 */
254int ext2_new_block (struct mount * mp, unsigned long goal,
255 u_int32_t * prealloc_count,
256 u_int32_t * prealloc_block)
257{
258 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
259 struct buffer_head * bh;
260 struct buffer_head * bh2;
261 char * p, * r;
262 int i, j, k, tmp;
263 int bitmap_nr;
264 struct ext2_group_desc * gdp;
265 struct ext2_super_block * es = sb->s_es;
266
267#ifdef EXT2FS_DEBUG
268 static int goal_hits = 0, goal_attempts = 0;
269#endif
270 if (!sb) {
271 printf ("ext2_new_block: nonexistent device");
272 return 0;
273 }
274 lock_super (VFSTOUFS(mp)->um_devvp);
275
276 ext2_debug ("goal=%lu.\n", goal);
277
278repeat:
279 /*
280 * First, test whether the goal block is free.
281 */
282 if (goal < es->s_first_data_block || goal >= es->s_blocks_count)
283 goal = es->s_first_data_block;
284 i = (goal - es->s_first_data_block) / EXT2_BLOCKS_PER_GROUP(sb);
285 gdp = get_group_desc (mp, i, &bh2);
286 if (gdp->bg_free_blocks_count > 0) {
287 j = ((goal - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb));
288#ifdef EXT2FS_DEBUG
289 if (j)
290 goal_attempts++;
291#endif
292 bitmap_nr = load_block_bitmap (mp, i);
293 bh = sb->s_block_bitmap[bitmap_nr];
294
295 ext2_debug ("goal is at %d:%d.\n", i, j);
296
297 if (!test_bit(j, bh->b_data)) {
298#ifdef EXT2FS_DEBUG
299 goal_hits++;
300 ext2_debug ("goal bit allocated.\n");
301#endif
302 goto got_block;
303 }
304 if (j) {
305 /*
306 * The goal was occupied; search forward for a free
307 * block within the next XX blocks.
308 *
309 * end_goal is more or less random, but it has to be
310 * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
311 * next 64-bit boundary is simple..
312 */
313 int end_goal = (j + 63) & ~63;
314 j = find_next_zero_bit(bh->b_data, end_goal, j);
315 if (j < end_goal)
316 goto got_block;
317 }
318
319 ext2_debug ("Bit not found near goal\n");
320
321 /*
322 * There has been no free block found in the near vicinity
323 * of the goal: do a search forward through the block groups,
324 * searching in each group first for an entire free byte in
325 * the bitmap and then for any free bit.
326 *
327 * Search first in the remainder of the current group; then,
328 * cyclicly search through the rest of the groups.
329 */
330 p = ((char *) bh->b_data) + (j >> 3);
331 r = memscan(p, 0, (EXT2_BLOCKS_PER_GROUP(sb) - j + 7) >> 3);
332 k = (r - ((char *) bh->b_data)) << 3;
333 if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
334 j = k;
335 goto search_back;
336 }
337 k = find_next_zero_bit ((unsigned long *) bh->b_data,
338 EXT2_BLOCKS_PER_GROUP(sb),
339 j);
340 if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
341 j = k;
342 goto got_block;
343 }
344 }
345
346 ext2_debug ("Bit not found in block group %d.\n", i);
347
348 /*
349 * Now search the rest of the groups. We assume that
350 * i and gdp correctly point to the last group visited.
351 */
352 for (k = 0; k < sb->s_groups_count; k++) {
353 i++;
354 if (i >= sb->s_groups_count)
355 i = 0;
356 gdp = get_group_desc (mp, i, &bh2);
357 if (gdp->bg_free_blocks_count > 0)
358 break;
359 }
360 if (k >= sb->s_groups_count) {
361 unlock_super (VFSTOUFS(mp)->um_devvp);
362 return 0;
363 }
364 bitmap_nr = load_block_bitmap (mp, i);
365 bh = sb->s_block_bitmap[bitmap_nr];
366 r = memscan(bh->b_data, 0, EXT2_BLOCKS_PER_GROUP(sb) >> 3);
367 j = (r - bh->b_data) << 3;
368
369 if (j < EXT2_BLOCKS_PER_GROUP(sb))
370 goto search_back;
371 else
372 j = find_first_zero_bit ((unsigned long *) bh->b_data,
373 EXT2_BLOCKS_PER_GROUP(sb));
374 if (j >= EXT2_BLOCKS_PER_GROUP(sb)) {
375 printf ( "ext2_new_block: "
376 "Free blocks count corrupted for block group %d", i);
377 unlock_super (VFSTOUFS(mp)->um_devvp);
378 return 0;
379 }
380
381search_back:
382 /*
383 * We have succeeded in finding a free byte in the block
384 * bitmap. Now search backwards up to 7 bits to find the
385 * start of this group of free blocks.
386 */
387 for (k = 0; k < 7 && j > 0 && !test_bit (j - 1, bh->b_data); k++, j--);
388
389got_block:
390
391 ext2_debug ("using block group %d(%d)\n", i, gdp->bg_free_blocks_count);
392
393 tmp = j + i * EXT2_BLOCKS_PER_GROUP(sb) + es->s_first_data_block;
394
395 if (/* test_opt (sb, CHECK_STRICT) && we are always strict. */
396 (tmp == gdp->bg_block_bitmap ||
397 tmp == gdp->bg_inode_bitmap ||
398 in_range (tmp, gdp->bg_inode_table, sb->s_itb_per_group)))
399 panic ( "ext2_new_block: "
400 "Allocating block in system zone - "
401 "%dth block = %u in group %u", j, tmp, i);
402
403 if (set_bit (j, bh->b_data)) {
404 printf ( "ext2_new_block: "
405 "bit already set for block %d", j);
406 goto repeat;
407 }
408
409 ext2_debug ("found bit %d\n", j);
410
411 /*
412 * Do block preallocation now if required.
413 */
414#ifdef EXT2_PREALLOCATE
415 if (prealloc_block) {
416 *prealloc_count = 0;
417 *prealloc_block = tmp + 1;
418 for (k = 1;
419 k < 8 && (j + k) < EXT2_BLOCKS_PER_GROUP(sb); k++) {
420 if (set_bit (j + k, bh->b_data))
421 break;
422 (*prealloc_count)++;
423 }
424 gdp->bg_free_blocks_count -= *prealloc_count;
425 es->s_free_blocks_count -= *prealloc_count;
426 ext2_debug ("Preallocated a further %lu bits.\n",
427 *prealloc_count);
428 }
429#endif
430
431 j = tmp;
432
433 mark_buffer_dirty(bh);
434/****
435 if (sb->s_flags & MS_SYNCHRONOUS) {
436 ll_rw_block (WRITE, 1, &bh);
437 wait_on_buffer (bh);
438 }
439****/
440 if (j >= es->s_blocks_count) {
441 printf ( "ext2_new_block: "
442 "block >= blocks count - "
443 "block_group = %d, block=%d", i, j);
444 unlock_super (VFSTOUFS(mp)->um_devvp);
445 return 0;
446 }
447
448 ext2_debug ("allocating block %d. "
449 "Goal hits %d of %d.\n", j, goal_hits, goal_attempts);
450
451 gdp->bg_free_blocks_count--;
452 mark_buffer_dirty(bh2);
453 es->s_free_blocks_count--;
454 sb->s_dirt = 1;
455 unlock_super (VFSTOUFS(mp)->um_devvp);
456 return j;
457}
458
459#ifdef unused
460static unsigned long ext2_count_free_blocks (struct mount * mp)
461{
462 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
463#ifdef EXT2FS_DEBUG
464 struct ext2_super_block * es;
465 unsigned long desc_count, bitmap_count, x;
466 int bitmap_nr;
467 struct ext2_group_desc * gdp;
468 int i;
469
470 lock_super (VFSTOUFS(mp)->um_devvp);
471 es = sb->s_es;
472 desc_count = 0;
473 bitmap_count = 0;
474 gdp = NULL;
475 for (i = 0; i < sb->s_groups_count; i++) {
476 gdp = get_group_desc (mp, i, NULL);
477 desc_count += gdp->bg_free_blocks_count;
478 bitmap_nr = load_block_bitmap (mp, i);
479 x = ext2_count_free (sb->s_block_bitmap[bitmap_nr],
480 sb->s_blocksize);
481 ext2_debug ("group %d: stored = %d, counted = %lu\n",
482 i, gdp->bg_free_blocks_count, x);
483 bitmap_count += x;
484 }
485 ext2_debug( "stored = %lu, computed = %lu, %lu\n",
486 es->s_free_blocks_count, desc_count, bitmap_count);
487 unlock_super (VFSTOUFS(mp)->um_devvp);
488 return bitmap_count;
489#else
490 return sb->s_es->s_free_blocks_count;
491#endif
492}
493#endif /* unused */
494
495static __inline int block_in_use (unsigned long block,
496 struct ext2_sb_info * sb,
497 unsigned char * map)
498{
499 return test_bit ((block - sb->s_es->s_first_data_block) %
500 EXT2_BLOCKS_PER_GROUP(sb), map);
501}
502
503static int test_root(int a, int b)
504{
505 if (a == 0)
506 return 1;
507 while (1) {
508 if (a == 1)
509 return 1;
510 if (a % b)
511 return 0;
512 a = a / b;
513 }
514}
515
516int ext2_group_sparse(int group)
517{
518 return (test_root(group, 3) || test_root(group, 5) ||
519 test_root(group, 7));
520}
521
522#ifdef unused
523static void ext2_check_blocks_bitmap (struct mount * mp)
524{
525 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
526 struct buffer_head * bh;
527 struct ext2_super_block * es;
528 unsigned long desc_count, bitmap_count, x;
529 unsigned long desc_blocks;
530 int bitmap_nr;
531 struct ext2_group_desc * gdp;
532 int i, j;
533
534 lock_super (VFSTOUFS(mp)->um_devvp);
535 es = sb->s_es;
536 desc_count = 0;
537 bitmap_count = 0;
538 gdp = NULL;
539 desc_blocks = (sb->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
540 EXT2_DESC_PER_BLOCK(sb);
541 for (i = 0; i < sb->s_groups_count; i++) {
542 gdp = get_group_desc (mp, i, NULL);
543 desc_count += gdp->bg_free_blocks_count;
544 bitmap_nr = load_block_bitmap (mp, i);
545 bh = sb->s_block_bitmap[bitmap_nr];
546
547 if (!(es->s_feature_ro_compat &
548 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
549 ext2_group_sparse(i)) {
550 if (!test_bit (0, bh->b_data))
551 printf ("ext2_check_blocks_bitmap: "
552 "Superblock in group %d "
553 "is marked free", i);
554
555 for (j = 0; j < desc_blocks; j++)
556 if (!test_bit (j + 1, bh->b_data))
557 printf ("ext2_check_blocks_bitmap: "
558 "Descriptor block #%d in group "
559 "%d is marked free", j, i);
560 }
561
562 if (!block_in_use (gdp->bg_block_bitmap, sb, bh->b_data))
563 printf ("ext2_check_blocks_bitmap: "
564 "Block bitmap for group %d is marked free",
565 i);
566
567 if (!block_in_use (gdp->bg_inode_bitmap, sb, bh->b_data))
568 printf ("ext2_check_blocks_bitmap: "
569 "Inode bitmap for group %d is marked free",
570 i);
571
572 for (j = 0; j < sb->s_itb_per_group; j++)
573 if (!block_in_use (gdp->bg_inode_table + j, sb, bh->b_data))
574 printf ("ext2_check_blocks_bitmap: "
575 "Block #%d of the inode table in "
576 "group %d is marked free", j, i);
577
578 x = ext2_count_free (bh, sb->s_blocksize);
579 if (gdp->bg_free_blocks_count != x)
580 printf ("ext2_check_blocks_bitmap: "
581 "Wrong free blocks count for group %d, "
582 "stored = %d, counted = %lu", i,
583 gdp->bg_free_blocks_count, x);
584 bitmap_count += x;
585 }
586 if (es->s_free_blocks_count != bitmap_count)
587 printf ("ext2_check_blocks_bitmap: "
588 "Wrong free blocks count in super block, "
589 "stored = %lu, counted = %lu",
590 (unsigned long) es->s_free_blocks_count, bitmap_count);
591 unlock_super (VFSTOUFS(mp)->um_devvp);
592}
593#endif /* unused */
594
595/*
596 * this function is taken from
597 * linux/fs/ext2/bitmap.c
598 */
599
600static int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0};
601
602unsigned long ext2_count_free (struct buffer_head * map, unsigned int numchars)
603{
604 unsigned int i;
605 unsigned long sum = 0;
606
607 if (!map)
608 return (0);
609 for (i = 0; i < numchars; i++)
610 sum += nibblemap[map->b_data[i] & 0xf] +
611 nibblemap[(map->b_data[i] >> 4) & 0xf];
612 return (sum);
613}