summaryrefslogtreecommitdiff
path: root/sys/gnu/ext2fs/ext2_linux_balloc.c
blob: 1d89f025ff3948304530bde752053340aa39f5e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*	$OpenBSD: ext2_linux_balloc.c,v 1.3 1996/10/18 15:23:36 mickey Exp $	*/

/*
 *  modified for Lites 1.1
 *
 *  Aug 1995, Godmar Back (gback@cs.utah.edu)
 *  University of Utah, Department of Computer Science
 */
/*
 *  linux/fs/ext2/balloc.c
 *
 * Copyright (C) 1992, 1993, 1994, 1995
 * Remy Card (card@masi.ibp.fr)
 * Laboratoire MASI - Institut Blaise Pascal
 * Universite Pierre et Marie Curie (Paris VI)
 *
 *  Enhanced block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
 */

/*
 * The free blocks are managed by bitmaps.  A file system contains several
 * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
 * block for inodes, N blocks for the inode table and data blocks.
 *
 * The file system contains group descriptors which are located after the
 * super block.  Each descriptor contains the number of the bitmap block and
 * the free blocks count in the block.  The descriptors are loaded in memory
 * when a file system is mounted (see ext2_read_super).
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/vnode.h>

#include <ufs/ufs/quota.h>
#include <ufs/ufs/ufsmount.h>
#include <gnu/ext2fs/ext2_extern.h>
#include <gnu/ext2fs/ext2_fs.h>
#include <gnu/ext2fs/ext2_fs_sb.h>
#include <gnu/ext2fs/fs.h>
#include <sys/stat.h>

#if defined(__GNUC__)
#define INLINE	__inline__
#endif

#include <gnu/ext2fs/ext2_bitops.c>

#define in_range(b, first, len)		((b) >= (first) && (b) <= (first) + (len) - 1)

/* got rid of get_group_desc since it can already be found in 
 * ext2_linux_ialloc.c
 */

static void read_block_bitmap (struct mount * mp,
			       unsigned int block_group,
			       unsigned long bitmap_nr)
{
	struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
	struct ext2_group_desc * gdp;
	struct buffer_head * bh;
	int    error;
	
	gdp = get_group_desc (mp, block_group, NULL);
	error = bread (VFSTOUFS(mp)->um_devvp, 
		fsbtodb(sb, gdp->bg_block_bitmap),sb->s_blocksize, NOCRED, &bh);
	if (error != 0)
		panic ( "read_block_bitmap: "
			    "Cannot read block bitmap - "
			    "block_group = %d, block_bitmap = %lu",
			    block_group, (unsigned long) gdp->bg_block_bitmap);
	sb->s_block_bitmap_number[bitmap_nr] = block_group;
	sb->s_block_bitmap[bitmap_nr] = bh;
}

/*
 * load_block_bitmap loads the block bitmap for a blocks group
 *
 * It maintains a cache for the last bitmaps loaded.  This cache is managed
 * with a LRU algorithm.
 *
 * Notes:
 * 1/ There is one cache per mounted file system.
 * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
 *    this function reads the bitmap without maintaining a LRU cache.
 */
static int load__block_bitmap (struct mount * mp,
			       unsigned int block_group)
{
	int i, j;
	struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
	unsigned long block_bitmap_number;
	struct buffer_head * block_bitmap;

	if (block_group >= sb->s_groups_count)
		panic ( "load_block_bitmap: "
			    "block_group >= groups_count - "
			    "block_group = %d, groups_count = %lu",
			    block_group, sb->s_groups_count);

	if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED) {
		if (sb->s_block_bitmap[block_group]) {
			if (sb->s_block_bitmap_number[block_group] !=
			    block_group)
				panic ( "load_block_bitmap: "
					    "block_group != block_bitmap_number");
			else
				return block_group;
		} else {
			read_block_bitmap (mp, block_group, block_group);
			return block_group;
		}
	}

	for (i = 0; i < sb->s_loaded_block_bitmaps &&
		    sb->s_block_bitmap_number[i] != block_group; i++)
		;
	if (i < sb->s_loaded_block_bitmaps &&
  	    sb->s_block_bitmap_number[i] == block_group) {
		block_bitmap_number = sb->s_block_bitmap_number[i];
		block_bitmap = sb->s_block_bitmap[i];
		for (j = i; j > 0; j--) {
			sb->s_block_bitmap_number[j] =
				sb->s_block_bitmap_number[j - 1];
			sb->s_block_bitmap[j] =
				sb->s_block_bitmap[j - 1];
		}
		sb->s_block_bitmap_number[0] = block_bitmap_number;
		sb->s_block_bitmap[0] = block_bitmap;
	} else {
		if (sb->s_loaded_block_bitmaps < EXT2_MAX_GROUP_LOADED)
			sb->s_loaded_block_bitmaps++;
		else
			brelse (sb->s_block_bitmap[EXT2_MAX_GROUP_LOADED - 1]);
		for (j = sb->s_loaded_block_bitmaps - 1; j > 0;  j--) {
			sb->s_block_bitmap_number[j] =
				sb->s_block_bitmap_number[j - 1];
			sb->s_block_bitmap[j] =
				sb->s_block_bitmap[j - 1];
		}
		read_block_bitmap (mp, block_group, 0);
	}
	return 0;
}

static INLINE int load_block_bitmap (struct mount * mp,
				     unsigned int block_group)
{
	struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
	if (sb->s_loaded_block_bitmaps > 0 &&
	    sb->s_block_bitmap_number[0] == block_group)
		return 0;
	
	if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED && 
	    sb->s_block_bitmap_number[block_group] == block_group &&
	    sb->s_block_bitmap[block_group]) 
		return block_group;

	return load__block_bitmap (mp, block_group);
}

void ext2_free_blocks (struct mount * mp, unsigned long block,
		       unsigned long count)
{
	struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
	struct buffer_head * bh;
	struct buffer_head * bh2;
	unsigned long block_group;
	unsigned long bit;
	unsigned long i;
	int bitmap_nr;
	struct ext2_group_desc * gdp;
	struct ext2_super_block * es = sb->s_es;

	if (!sb) {
		printf ("ext2_free_blocks: nonexistent device");
		return;
	}
	lock_super (VFSTOUFS(mp)->um_devvp);
	if (block < es->s_first_data_block || 
	    (block + count) > es->s_blocks_count) {
		printf ( "ext2_free_blocks: "
			    "Freeing blocks not in datazone - "
			    "block = %lu, count = %lu", block, count);
		unlock_super (VFSTOUFS(mp)->um_devvp);
		return;
	}

	ext2_debug ("freeing blocks %lu to %lu\n", block, block+count-1);

	block_group = (block - es->s_first_data_block) /
		      EXT2_BLOCKS_PER_GROUP(sb);
	bit = (block - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb);
	if (bit + count > EXT2_BLOCKS_PER_GROUP(sb))
		panic ( "ext2_free_blocks: "
			    "Freeing blocks across group boundary - "
			    "Block = %lu, count = %lu",
			    block, count);
	bitmap_nr = load_block_bitmap (mp, block_group);
	bh = sb->s_block_bitmap[bitmap_nr];
	gdp = get_group_desc (mp, block_group, &bh2);

	if (/* test_opt (sb, CHECK_STRICT) && 	assume always strict ! */
	    (in_range (gdp->bg_block_bitmap, block, count) ||
	     in_range (gdp->bg_inode_bitmap, block, count) ||
	     in_range (block, gdp->bg_inode_table,
		       sb->s_itb_per_group) ||
	     in_range (block + count - 1, gdp->bg_inode_table,
		       sb->s_itb_per_group)))
		panic ( "ext2_free_blocks: "
			    "Freeing blocks in system zones - "
			    "Block = %lu, count = %lu",
			    block, count);

	for (i = 0; i < count; i++) {
		if (!ext2fs_clear_bit (bit + i, bh->b_data))
			printf ("ext2_free_blocks: "
				      "bit already cleared for block %lu", 
				      block);
		else {
			gdp->bg_free_blocks_count++;
			es->s_free_blocks_count++;
		}
	}

	mark_buffer_dirty(bh2);
	mark_buffer_dirty(bh);
/****
	if (sb->s_flags & MS_SYNCHRONOUS) {
		ll_rw_block (WRITE, 1, &bh);
		wait_on_buffer (bh);
	}
****/
	sb->s_dirt = 1;
	unlock_super (VFSTOUFS(mp)->um_devvp);
	return;
}

/*
 * ext2_new_block uses a goal block to assist allocation.  If the goal is
 * free, or there is a free block within 32 blocks of the goal, that block
 * is allocated.  Otherwise a forward search is made for a free block; within 
 * each block group the search first looks for an entire free byte in the block
 * bitmap, and then for any free bit if that fails.
 */
int ext2_new_block (struct mount * mp, u_int32_t goal,
		    int32_t * prealloc_count,
		    int32_t * prealloc_block)
{
	struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
	struct buffer_head * bh;
	struct buffer_head * bh2;
	char * p, * r;
	int i, j, k, tmp;
	int bitmap_nr;
	struct ext2_group_desc * gdp;
	struct ext2_super_block * es = sb->s_es;

#ifdef EXT2FS_DEBUG
	static int goal_hits = 0, goal_attempts = 0;
#endif
	if (!sb) {
		printf ("ext2_new_block: nonexistent device");
		return 0;
	}
	lock_super (VFSTOUFS(mp)->um_devvp);

        ext2_debug ("goal=%lu.\n", goal);

repeat:
	/*
	 * First, test whether the goal block is free.
	 */
	if (goal < es->s_first_data_block || goal >= es->s_blocks_count)
		goal = es->s_first_data_block;
	i = (goal - es->s_first_data_block) / EXT2_BLOCKS_PER_GROUP(sb);
	gdp = get_group_desc (mp, i, &bh2);
	if (gdp->bg_free_blocks_count > 0) {
		j = ((goal - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb));
#ifdef EXT2FS_DEBUG
		if (j)
			goal_attempts++;
#endif
		bitmap_nr = load_block_bitmap (mp, i);
		bh = sb->s_block_bitmap[bitmap_nr];

		ext2_debug ("goal is at %d:%d.\n", i, j); 

		if (!ext2fs_test_bit(j, bh->b_data)) {
#ifdef EXT2FS_DEBUG
			goal_hits++;
			ext2_debug ("goal bit allocated.\n");
#endif
			goto got_block;
		}
		if (j) {
			/*
			 * The goal was occupied; search forward for a free 
			 * block within the next XX blocks.
			 *
			 * end_goal is more or less random, but it has to be
			 * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
			 * next 64-bit boundary is simple..
			 */
			int end_goal = (j + 63) & ~63;
			j = ext2fs_fnzb(bh->b_data, end_goal, j);
			if (j < end_goal)
				goto got_block;
		}
	
		ext2_debug ("Bit not found near goal\n");

		/*
		 * There has been no free block found in the near vicinity
		 * of the goal: do a search forward through the block groups,
		 * searching in each group first for an entire free byte in
		 * the bitmap and then for any free bit.
		 * 
		 * Search first in the remainder of the current group; then,
		 * cyclicly search through the rest of the groups.
		 */
		p = ((char *) bh->b_data) + (j >> 3);
		r = ext2fs_memscan(p, 0,
				   (EXT2_BLOCKS_PER_GROUP(sb) - j + 7) >> 3);
		k = (r - ((char *) bh->b_data)) << 3;
		if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
			j = k;
			goto search_back;
		}
		k = ext2fs_fnzb ((unsigned long *) bh->b_data, 
					EXT2_BLOCKS_PER_GROUP(sb),
					j);
		if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
			j = k;
			goto got_block;
		}
	}

	ext2_debug ("Bit not found in block group %d.\n", i); 

	/*
	 * Now search the rest of the groups.  We assume that 
	 * i and gdp correctly point to the last group visited.
	 */
	for (k = 0; k < sb->s_groups_count; k++) {
		i++;
		if (i >= sb->s_groups_count)
			i = 0;
		gdp = get_group_desc (mp, i, &bh2);
		if (gdp->bg_free_blocks_count > 0)
			break;
	}
	if (k >= sb->s_groups_count) {
		unlock_super (VFSTOUFS(mp)->um_devvp);
		return 0;
	}
	bitmap_nr = load_block_bitmap (mp, i);
	bh = sb->s_block_bitmap[bitmap_nr];
	r = ext2fs_memscan(bh->b_data, 0, EXT2_BLOCKS_PER_GROUP(sb) >> 3);
	j = (r - bh->b_data) << 3;

	if (j < EXT2_BLOCKS_PER_GROUP(sb))
		goto search_back;
	else
		j = ext2fs_ffzb ((unsigned long *) bh->b_data,
					 EXT2_BLOCKS_PER_GROUP(sb));
	if (j >= EXT2_BLOCKS_PER_GROUP(sb)) {
		printf ( "ext2_new_block: "
			 "Free blocks count corrupted for block group %d", i);
		unlock_super (VFSTOUFS(mp)->um_devvp);
		return 0;
	}

search_back:
	/* 
	 * We have succeeded in finding a free byte in the block
	 * bitmap.  Now search backwards up to 7 bits to find the
	 * start of this group of free blocks.
	 */
	for (k = 0; k < 7 && j > 0 && !ext2fs_test_bit (j - 1, bh->b_data);
		k++, j--);
	
got_block:

	ext2_debug ("using block group %d(%d)\n", i, gdp->bg_free_blocks_count);

	tmp = j + i * EXT2_BLOCKS_PER_GROUP(sb) + es->s_first_data_block;

	if (/* test_opt (sb, CHECK_STRICT) && we are always strict. */
	    (tmp == gdp->bg_block_bitmap ||
	     tmp == gdp->bg_inode_bitmap ||
	     in_range (tmp, gdp->bg_inode_table, sb->s_itb_per_group)))
		panic ( "ext2_new_block: "
			    "Allocating block in system zone - "
			    "%dth block = %u in group %u", j, tmp, i);

	if (ext2fs_set_bit (j, bh->b_data)) {
		printf ( "ext2_new_block: "
			 "bit already set for block %d", j);
		goto repeat;
	}

	ext2_debug ("found bit %d\n", j);

	/*
	 * Do block preallocation now if required.
	 */
#ifdef EXT2_PREALLOCATE
	if (prealloc_block) {
		*prealloc_count = 0;
		*prealloc_block = tmp + 1;
		for (k = 1;
		     k < 8 && (j + k) < EXT2_BLOCKS_PER_GROUP(sb); k++) {
			if (ext2fs_set_bit (j + k, bh->b_data))
				break;
			(*prealloc_count)++;
		}	
		gdp->bg_free_blocks_count -= *prealloc_count;
		es->s_free_blocks_count -= *prealloc_count;
		ext2_debug ("Preallocated a further %lu bits.\n",
			    *prealloc_count); 
	}
#endif

	j = tmp;

	mark_buffer_dirty(bh);
/****
	if (sb->s_flags & MS_SYNCHRONOUS) {
		ll_rw_block (WRITE, 1, &bh);
		wait_on_buffer (bh);
	}
****/
	if (j >= es->s_blocks_count) {
		printf ( "ext2_new_block: "
			    "block >= blocks count - "
			    "block_group = %d, block=%d", i, j);
		unlock_super (VFSTOUFS(mp)->um_devvp);
		return 0;
	}

	ext2_debug ("allocating block %d. "
		    "Goal hits %d of %d.\n", j, goal_hits, goal_attempts);

	gdp->bg_free_blocks_count--;
	mark_buffer_dirty(bh2);
	es->s_free_blocks_count--;
	sb->s_dirt = 1;
	unlock_super (VFSTOUFS(mp)->um_devvp);
	return j;
}

#if 0
static unsigned long ext2_count_free_blocks (struct mount * mp)
{
	struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
#ifdef EXT2FS_DEBUG
	struct ext2_super_block * es;
	unsigned long desc_count, bitmap_count, x;
	int bitmap_nr;
	struct ext2_group_desc * gdp;
	int i;
	
	lock_super (VFSTOUFS(mp)->um_devvp);
	es = sb->s_es;
	desc_count = 0;
	bitmap_count = 0;
	gdp = NULL;
	for (i = 0; i < sb->s_groups_count; i++) {
		gdp = get_group_desc (mp, i, NULL);
		desc_count += gdp->bg_free_blocks_count;
		bitmap_nr = load_block_bitmap (mp, i);
		x = ext2_count_free (sb->s_block_bitmap[bitmap_nr],
				     sb->s_blocksize);
		ext2_debug ("group %d: stored = %d, counted = %lu\n",
			i, gdp->bg_free_blocks_count, x);
		bitmap_count += x;
	}
	ext2_debug( "stored = %lu, computed = %lu, %lu\n",
	       es->s_free_blocks_count, desc_count, bitmap_count);
	unlock_super (VFSTOUFS(mp)->um_devvp);
	return bitmap_count;
#else
	return sb->s_es->s_free_blocks_count;
#endif
}
#endif

static INLINE int block_in_use (unsigned long block,
				struct ext2_sb_info * sb,
				unsigned char * map)
{
	return ext2fs_test_bit ((block - sb->s_es->s_first_data_block) %
			 	 EXT2_BLOCKS_PER_GROUP(sb), map);
}

#if 0
static void ext2_check_blocks_bitmap (struct mount * mp)
{
	struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
	struct buffer_head * bh;
	struct ext2_super_block * es;
	unsigned long desc_count, bitmap_count, x;
	unsigned long desc_blocks;
	int bitmap_nr;
	struct ext2_group_desc * gdp;
	int i, j;

	lock_super (VFSTOUFS(mp)->um_devvp);
	es = sb->s_es;
	desc_count = 0;
	bitmap_count = 0;
	gdp = NULL;
	desc_blocks = (sb->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
		      EXT2_DESC_PER_BLOCK(sb);
	for (i = 0; i < sb->s_groups_count; i++) {
		gdp = get_group_desc (mp, i, NULL);
		desc_count += gdp->bg_free_blocks_count;
		bitmap_nr = load_block_bitmap (mp, i);
		bh = sb->s_block_bitmap[bitmap_nr];

		if (!ext2fs_test_bit (0, bh->b_data))
			printf ( "ext2_check_blocks_bitmap: "
				    "Superblock in group %d is marked free", i);

		for (j = 0; j < desc_blocks; j++)
			if (!ext2fs_test_bit (j + 1, bh->b_data))
				printf ("ext2_check_blocks_bitmap: "
					    "Descriptor block #%d in group "
					    "%d is marked free", j, i);

		if (!block_in_use (gdp->bg_block_bitmap, sb, bh->b_data))
			printf ("ext2_check_blocks_bitmap: "
				    "Block bitmap for group %d is marked free",
				    i);

		if (!block_in_use (gdp->bg_inode_bitmap, sb, bh->b_data))
			printf ("ext2_check_blocks_bitmap: "
				    "Inode bitmap for group %d is marked free",
				    i);

		for (j = 0; j < sb->s_itb_per_group; j++)
			if (!block_in_use (gdp->bg_inode_table + j, sb, bh->b_data))
				printf ("ext2_check_blocks_bitmap: "
					    "Block #%d of the inode table in "
					    "group %d is marked free", j, i);

		x = ext2_count_free (bh, sb->s_blocksize);
		if (gdp->bg_free_blocks_count != x)
			printf ("ext2_check_blocks_bitmap: "
				    "Wrong free blocks count for group %d, "
				    "stored = %d, counted = %lu", i,
				    gdp->bg_free_blocks_count, x);
		bitmap_count += x;
	}
	if (es->s_free_blocks_count != bitmap_count)
		printf ("ext2_check_blocks_bitmap: "
			    "Wrong free blocks count in super block, "
			    "stored = %lu, counted = %lu",
			    (unsigned long) es->s_free_blocks_count, bitmap_count);
	unlock_super (VFSTOUFS(mp)->um_devvp);
}
#endif

/*
 *  this function is taken from 
 *  linux/fs/ext2/bitmap.c
 */

static int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0};

unsigned long ext2_count_free (struct buffer_head * map, unsigned int numchars)
{
        unsigned int i;
        unsigned long sum = 0;

        if (!map)
                return (0);
        for (i = 0; i < numchars; i++)
                sum += nibblemap[map->b_data[i] & 0xf] +
                        nibblemap[(map->b_data[i] >> 4) & 0xf];
        return (sum);
}