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