This source file includes following definitions.
- get_group_desc
- read_block_bitmap
- load__block_bitmap
- load_block_bitmap
- ext2_free_blocks
- ext2_new_block
- ext2_count_free_blocks
- block_in_use
- ext2_check_blocks_bitmap
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 #include <linux/fs.h>
27 #include <linux/ext2_fs.h>
28 #include <linux/stat.h>
29 #include <linux/sched.h>
30 #include <linux/string.h>
31 #include <linux/locks.h>
32
33 #include <asm/bitops.h>
34
35 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
36
37 static struct ext2_group_desc * get_group_desc (struct super_block * sb,
38 unsigned int block_group,
39 struct buffer_head ** bh)
40 {
41 unsigned long group_desc;
42 unsigned long desc;
43 struct ext2_group_desc * gdp;
44
45 if (block_group >= sb->u.ext2_sb.s_groups_count)
46 ext2_panic (sb, "get_group_desc",
47 "block_group >= groups_count\n"
48 "block_group = %d, groups_count = %lu",
49 block_group, sb->u.ext2_sb.s_groups_count);
50
51 group_desc = block_group / EXT2_DESC_PER_BLOCK(sb);
52 desc = block_group % EXT2_DESC_PER_BLOCK(sb);
53 if (!sb->u.ext2_sb.s_group_desc[group_desc])
54 ext2_panic (sb, "get_group_desc",
55 "Group descriptor not loaded\n"
56 "block_group = %d, group_desc = %lu, desc = %lu",
57 block_group, group_desc, desc);
58 gdp = (struct ext2_group_desc *)
59 sb->u.ext2_sb.s_group_desc[group_desc]->b_data;
60 if (bh)
61 *bh = sb->u.ext2_sb.s_group_desc[group_desc];
62 return gdp + desc;
63 }
64
65 static void read_block_bitmap (struct super_block * sb,
66 unsigned int block_group,
67 unsigned long bitmap_nr)
68 {
69 struct ext2_group_desc * gdp;
70 struct buffer_head * bh;
71
72 gdp = get_group_desc (sb, block_group, NULL);
73 bh = bread (sb->s_dev, gdp->bg_block_bitmap, sb->s_blocksize);
74 if (!bh)
75 ext2_panic (sb, "read_block_bitmap",
76 "Cannot read block bitmap\n"
77 "block_group = %d, block_bitmap = %lu",
78 block_group, gdp->bg_block_bitmap);
79 sb->u.ext2_sb.s_block_bitmap_number[bitmap_nr] = block_group;
80 sb->u.ext2_sb.s_block_bitmap[bitmap_nr] = bh;
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94 static int load__block_bitmap (struct super_block * sb,
95 unsigned int block_group)
96 {
97 int i, j;
98 unsigned long block_bitmap_number;
99 struct buffer_head * block_bitmap;
100
101 if (block_group >= sb->u.ext2_sb.s_groups_count)
102 ext2_panic (sb, "load_block_bitmap",
103 "block_group >= groups_count\n"
104 "block_group = %d, groups_count = %lu",
105 block_group, sb->u.ext2_sb.s_groups_count);
106
107 if (sb->u.ext2_sb.s_groups_count <= EXT2_MAX_GROUP_LOADED) {
108 if (sb->u.ext2_sb.s_block_bitmap[block_group]) {
109 if (sb->u.ext2_sb.s_block_bitmap_number[block_group] !=
110 block_group)
111 ext2_panic (sb, "load_block_bitmap",
112 "block_group != block_bitmap_number");
113 else
114 return block_group;
115 } else {
116 read_block_bitmap (sb, block_group, block_group);
117 return block_group;
118 }
119 }
120
121 for (i = 0; i < sb->u.ext2_sb.s_loaded_block_bitmaps &&
122 sb->u.ext2_sb.s_block_bitmap_number[i] != block_group; i++)
123 ;
124 if (i < sb->u.ext2_sb.s_loaded_block_bitmaps &&
125 sb->u.ext2_sb.s_block_bitmap_number[i] == block_group) {
126 block_bitmap_number = sb->u.ext2_sb.s_block_bitmap_number[i];
127 block_bitmap = sb->u.ext2_sb.s_block_bitmap[i];
128 for (j = i; j > 0; j--) {
129 sb->u.ext2_sb.s_block_bitmap_number[j] =
130 sb->u.ext2_sb.s_block_bitmap_number[j - 1];
131 sb->u.ext2_sb.s_block_bitmap[j] =
132 sb->u.ext2_sb.s_block_bitmap[j - 1];
133 }
134 sb->u.ext2_sb.s_block_bitmap_number[0] = block_bitmap_number;
135 sb->u.ext2_sb.s_block_bitmap[0] = block_bitmap;
136 } else {
137 if (sb->u.ext2_sb.s_loaded_block_bitmaps < EXT2_MAX_GROUP_LOADED)
138 sb->u.ext2_sb.s_loaded_block_bitmaps++;
139 else
140 brelse (sb->u.ext2_sb.s_block_bitmap[EXT2_MAX_GROUP_LOADED - 1]);
141 for (j = sb->u.ext2_sb.s_loaded_block_bitmaps - 1; j > 0; j--) {
142 sb->u.ext2_sb.s_block_bitmap_number[j] =
143 sb->u.ext2_sb.s_block_bitmap_number[j - 1];
144 sb->u.ext2_sb.s_block_bitmap[j] =
145 sb->u.ext2_sb.s_block_bitmap[j - 1];
146 }
147 read_block_bitmap (sb, block_group, 0);
148 }
149 return 0;
150 }
151
152 static inline int load_block_bitmap (struct super_block * sb,
153 unsigned int block_group)
154 {
155 if (sb->u.ext2_sb.s_loaded_block_bitmaps > 0 &&
156 sb->u.ext2_sb.s_block_bitmap_number[0] == block_group)
157 return 0;
158
159 if (sb->u.ext2_sb.s_groups_count <= EXT2_MAX_GROUP_LOADED &&
160 sb->u.ext2_sb.s_block_bitmap_number[block_group] == block_group &&
161 sb->u.ext2_sb.s_block_bitmap[block_group])
162 return block_group;
163
164 return load__block_bitmap (sb, block_group);
165 }
166
167 void ext2_free_blocks (struct super_block * sb, unsigned long block,
168 unsigned long count)
169 {
170 struct buffer_head * bh;
171 struct buffer_head * bh2;
172 unsigned long block_group;
173 unsigned long bit;
174 unsigned long i;
175 int bitmap_nr;
176 struct ext2_group_desc * gdp;
177 struct ext2_super_block * es;
178
179 if (!sb) {
180 printk ("ext2_free_blocks: nonexistent device");
181 return;
182 }
183 lock_super (sb);
184 es = sb->u.ext2_sb.s_es;
185 if (block < es->s_first_data_block ||
186 (block + count) > es->s_blocks_count) {
187 ext2_error (sb, "ext2_free_blocks",
188 "Freeing blocks not in datazone\n"
189 "block = %lu, count = %lu", block, count);
190 unlock_super (sb);
191 return;
192 }
193
194 ext2_debug ("freeing block %lu\n", block);
195
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 ext2_panic (sb, "ext2_free_blocks",
201 "Freeing blocks across group boundary\n"
202 "Block = %lu, count = %lu",
203 block, count);
204 bitmap_nr = load_block_bitmap (sb, block_group);
205 bh = sb->u.ext2_sb.s_block_bitmap[bitmap_nr];
206 gdp = get_group_desc (sb, block_group, &bh2);
207
208 if (test_opt (sb, CHECK_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->u.ext2_sb.s_itb_per_group) ||
213 in_range (block + count - 1, gdp->bg_inode_table,
214 sb->u.ext2_sb.s_itb_per_group)))
215 ext2_panic (sb, "ext2_free_blocks",
216 "Freeing blocks in system zones\n"
217 "Block = %lu, count = %lu",
218 block, count);
219
220 for (i = 0; i < count; i++) {
221 if (!clear_bit (bit + i, bh->b_data))
222 ext2_warning (sb, "ext2_free_blocks",
223 "bit already cleared for block %lu",
224 block);
225 else {
226 gdp->bg_free_blocks_count++;
227 es->s_free_blocks_count++;
228 }
229 }
230
231 mark_buffer_dirty(bh2, 1);
232 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
233
234 mark_buffer_dirty(bh, 1);
235 if (sb->s_flags & MS_SYNC) {
236 ll_rw_block (WRITE, 1, &bh);
237 wait_on_buffer (bh);
238 }
239 sb->s_dirt = 1;
240 unlock_super (sb);
241 return;
242 }
243
244
245
246
247
248
249
250
251 int ext2_new_block (struct super_block * sb, unsigned long goal,
252 unsigned long * prealloc_count,
253 unsigned long * prealloc_block)
254 {
255 struct buffer_head * bh;
256 struct buffer_head * bh2;
257 char * p, * r;
258 int i, j, k, tmp;
259 unsigned long lmap;
260 int bitmap_nr;
261 struct ext2_group_desc * gdp;
262 struct ext2_super_block * es;
263
264 #ifdef EXT2FS_DEBUG
265 static int goal_hits = 0, goal_attempts = 0;
266 #endif
267 if (!sb) {
268 printk ("ext2_new_block: nonexistent device");
269 return 0;
270 }
271 lock_super (sb);
272 es = sb->u.ext2_sb.s_es;
273 if (es->s_free_blocks_count <= es->s_r_blocks_count && !fsuser()) {
274 unlock_super (sb);
275 return 0;
276 }
277
278 ext2_debug ("goal=%lu.\n", goal);
279
280 repeat:
281
282
283
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 (sb, i, &bh2);
288 if (gdp->bg_free_blocks_count > 0) {
289 j = ((goal - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb));
290 #ifdef EXT2FS_DEBUG
291 if (j)
292 goal_attempts++;
293 #endif
294 bitmap_nr = load_block_bitmap (sb, i);
295 bh = sb->u.ext2_sb.s_block_bitmap[bitmap_nr];
296
297 ext2_debug ("goal is at %d:%d.\n", i, j);
298
299 if (!test_bit(j, bh->b_data)) {
300 #ifdef EXT2FS_DEBUG
301 goal_hits++;
302 ext2_debug ("goal bit allocated.\n");
303 #endif
304 goto got_block;
305 }
306 if (j) {
307
308
309
310
311 lmap = ((((unsigned long *) bh->b_data)[j >> 5]) >>
312 ((j & 31) + 1));
313 if (j < EXT2_BLOCKS_PER_GROUP(sb) - 32)
314 lmap |= (((unsigned long *) bh->b_data)[(j >> 5) + 1]) <<
315 (31 - (j & 31));
316 else
317 lmap |= 0xffffffff << (31 - (j & 31));
318 if (lmap != 0xffffffffl) {
319 k = ffz(lmap) + 1;
320 if ((j + k) < EXT2_BLOCKS_PER_GROUP(sb)) {
321 j += k;
322 goto got_block;
323 }
324 }
325 }
326
327 ext2_debug ("Bit not found near goal\n");
328
329
330
331
332
333
334
335
336
337
338 p = ((char *) bh->b_data) + (j >> 3);
339 r = memscan(p, 0, (EXT2_BLOCKS_PER_GROUP(sb) - j + 7) >> 3);
340 k = (r - ((char *) bh->b_data)) << 3;
341 if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
342 j = k;
343 goto search_back;
344 }
345 k = find_next_zero_bit ((unsigned long *) bh->b_data,
346 EXT2_BLOCKS_PER_GROUP(sb),
347 j);
348 if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
349 j = k;
350 goto got_block;
351 }
352 }
353
354 ext2_debug ("Bit not found in block group %d.\n", i);
355
356
357
358
359
360 for (k = 0; k < sb->u.ext2_sb.s_groups_count; k++) {
361 i++;
362 if (i >= sb->u.ext2_sb.s_groups_count)
363 i = 0;
364 gdp = get_group_desc (sb, i, &bh2);
365 if (gdp->bg_free_blocks_count > 0)
366 break;
367 }
368 if (k >= sb->u.ext2_sb.s_groups_count) {
369 unlock_super (sb);
370 return 0;
371 }
372 bitmap_nr = load_block_bitmap (sb, i);
373 bh = sb->u.ext2_sb.s_block_bitmap[bitmap_nr];
374 r = memscan(bh->b_data, 0, EXT2_BLOCKS_PER_GROUP(sb) >> 3);
375 j = (r - bh->b_data) << 3;
376 if (j < EXT2_BLOCKS_PER_GROUP(sb))
377 goto search_back;
378 else
379 j = find_first_zero_bit ((unsigned long *) bh->b_data,
380 EXT2_BLOCKS_PER_GROUP(sb));
381 if (j >= EXT2_BLOCKS_PER_GROUP(sb)) {
382 ext2_error (sb, "ext2_new_block",
383 "Free blocks count corrupted for block group %d", i);
384 unlock_super (sb);
385 return 0;
386 }
387
388 search_back:
389
390
391
392
393
394 for (k = 0; k < 7 && j > 0 && !test_bit (j - 1, bh->b_data); k++, j--);
395
396 got_block:
397
398 ext2_debug ("using block group %d(%d)\n", i, gdp->bg_free_blocks_count);
399
400 tmp = j + i * EXT2_BLOCKS_PER_GROUP(sb) + es->s_first_data_block;
401
402 if (test_opt (sb, CHECK_STRICT) &&
403 (tmp == gdp->bg_block_bitmap ||
404 tmp == gdp->bg_inode_bitmap ||
405 in_range (tmp, gdp->bg_inode_table, sb->u.ext2_sb.s_itb_per_group)))
406 ext2_panic (sb, "ext2_new_block",
407 "Allocating block in system zone\n"
408 "block = %u", tmp);
409
410 if (set_bit (j, bh->b_data)) {
411 ext2_warning (sb, "ext2_new_block",
412 "bit already set for block %d", j);
413 goto repeat;
414 }
415
416 ext2_debug ("found bit %d\n", j);
417
418
419
420
421 #ifdef EXT2_PREALLOCATE
422 if (prealloc_block) {
423 *prealloc_count = 0;
424 *prealloc_block = tmp + 1;
425 for (k = 1;
426 k < 8 && (j + k) < EXT2_BLOCKS_PER_GROUP(sb); k++) {
427 if (set_bit (j + k, bh->b_data))
428 break;
429 (*prealloc_count)++;
430 }
431 gdp->bg_free_blocks_count -= *prealloc_count;
432 es->s_free_blocks_count -= *prealloc_count;
433 ext2_debug ("Preallocated a further %lu bits.\n",
434 *prealloc_count);
435 }
436 #endif
437
438 j = tmp;
439
440 mark_buffer_dirty(bh, 1);
441 if (sb->s_flags & MS_SYNC) {
442 ll_rw_block (WRITE, 1, &bh);
443 wait_on_buffer (bh);
444 }
445
446 if (j >= es->s_blocks_count) {
447 ext2_error (sb, "ext2_new_block",
448 "block >= blocks count\n"
449 "block_group = %d, block=%d", i, j);
450 unlock_super (sb);
451 return 0;
452 }
453 if (!(bh = getblk (sb->s_dev, j, sb->s_blocksize))) {
454 ext2_error (sb, "ext2_new_block", "cannot get block %d", j);
455 unlock_super (sb);
456 return 0;
457 }
458 memset(bh->b_data, 0, sb->s_blocksize);
459 bh->b_uptodate = 1;
460 mark_buffer_dirty(bh, 1);
461 brelse (bh);
462
463 ext2_debug ("allocating block %d. "
464 "Goal hits %d of %d.\n", j, goal_hits, goal_attempts);
465
466 gdp->bg_free_blocks_count--;
467 mark_buffer_dirty(bh2, 1);
468 es->s_free_blocks_count--;
469 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
470 sb->s_dirt = 1;
471 unlock_super (sb);
472 return j;
473 }
474
475 unsigned long ext2_count_free_blocks (struct super_block * sb)
476 {
477 #ifdef EXT2FS_DEBUG
478 struct ext2_super_block * es;
479 unsigned long desc_count, bitmap_count, x;
480 int bitmap_nr;
481 struct ext2_group_desc * gdp;
482 int i;
483
484 lock_super (sb);
485 es = sb->u.ext2_sb.s_es;
486 desc_count = 0;
487 bitmap_count = 0;
488 gdp = NULL;
489 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
490 gdp = get_group_desc (sb, i, NULL);
491 desc_count += gdp->bg_free_blocks_count;
492 bitmap_nr = load_block_bitmap (sb, i);
493 x = ext2_count_free (sb->u.ext2_sb.s_block_bitmap[bitmap_nr],
494 sb->s_blocksize);
495 printk ("group %d: stored = %d, counted = %lu\n",
496 i, gdp->bg_free_blocks_count, x);
497 bitmap_count += x;
498 }
499 printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n",
500 es->s_free_blocks_count, desc_count, bitmap_count);
501 unlock_super (sb);
502 return bitmap_count;
503 #else
504 return sb->u.ext2_sb.s_es->s_free_blocks_count;
505 #endif
506 }
507
508 static inline int block_in_use (unsigned long block,
509 struct super_block * sb,
510 unsigned char * map)
511 {
512 return test_bit ((block - sb->u.ext2_sb.s_es->s_first_data_block) %
513 EXT2_BLOCKS_PER_GROUP(sb), map);
514 }
515
516 void ext2_check_blocks_bitmap (struct super_block * sb)
517 {
518 struct buffer_head * bh;
519 struct ext2_super_block * es;
520 unsigned long desc_count, bitmap_count, x;
521 unsigned long desc_blocks;
522 int bitmap_nr;
523 struct ext2_group_desc * gdp;
524 int i, j;
525
526 lock_super (sb);
527 es = sb->u.ext2_sb.s_es;
528 desc_count = 0;
529 bitmap_count = 0;
530 gdp = NULL;
531 desc_blocks = (sb->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
532 EXT2_DESC_PER_BLOCK(sb);
533 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
534 gdp = get_group_desc (sb, i, NULL);
535 desc_count += gdp->bg_free_blocks_count;
536 bitmap_nr = load_block_bitmap (sb, i);
537 bh = sb->u.ext2_sb.s_block_bitmap[bitmap_nr];
538
539 if (!test_bit (0, bh->b_data))
540 ext2_error (sb, "ext2_check_blocks_bitmap",
541 "Superblock in group %d is marked free", i);
542
543 for (j = 0; j < desc_blocks; j++)
544 if (!test_bit (j + 1, bh->b_data))
545 ext2_error (sb, "ext2_check_blocks_bitmap",
546 "Descriptor block #%d in group "
547 "%d is marked free", j, i);
548
549 if (!block_in_use (gdp->bg_block_bitmap, sb, bh->b_data))
550 ext2_error (sb, "ext2_check_blocks_bitmap",
551 "Block bitmap for group %d is marked free",
552 i);
553
554 if (!block_in_use (gdp->bg_inode_bitmap, sb, bh->b_data))
555 ext2_error (sb, "ext2_check_blocks_bitmap",
556 "Inode bitmap for group %d is marked free",
557 i);
558
559 for (j = 0; j < sb->u.ext2_sb.s_itb_per_group; j++)
560 if (!block_in_use (gdp->bg_inode_table + j, sb, bh->b_data))
561 ext2_error (sb, "ext2_check_blocks_bitmap",
562 "Block #%d of the inode table in "
563 "group %d is marked free", j, i);
564
565 x = ext2_count_free (bh, sb->s_blocksize);
566 if (gdp->bg_free_blocks_count != x)
567 ext2_error (sb, "ext2_check_blocks_bitmap",
568 "Wrong free blocks count for group %d, "
569 "stored = %d, counted = %lu", i,
570 gdp->bg_free_blocks_count, x);
571 bitmap_count += x;
572 }
573 if (es->s_free_blocks_count != bitmap_count)
574 ext2_error (sb, "ext2_check_blocks_bitmap",
575 "Wrong free blocks count in super block, "
576 "stored = %lu, counted = %lu",
577 es->s_free_blocks_count, bitmap_count);
578 unlock_super (sb);
579 }