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