OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [fs/] [ext2/] [balloc.c] - Blame information for rev 1777

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1628 jcastillo
/*
2
 *  linux/fs/ext2/balloc.c
3
 *
4
 * Copyright (C) 1992, 1993, 1994, 1995
5
 * Remy Card (card@masi.ibp.fr)
6
 * Laboratoire MASI - Institut Blaise Pascal
7
 * Universite Pierre et Marie Curie (Paris VI)
8
 *
9
 *  Enhanced block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
10
 *  Big-endian to little-endian byte-swapping/bitmaps by
11
 *        David S. Miller (davem@caip.rutgers.edu), 1995
12
 */
13
 
14
/*
15
 * balloc.c contains the blocks allocation and deallocation routines
16
 */
17
 
18
/*
19
 * The free blocks are managed by bitmaps.  A file system contains several
20
 * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
21
 * block for inodes, N blocks for the inode table and data blocks.
22
 *
23
 * The file system contains group descriptors which are located after the
24
 * super block.  Each descriptor contains the number of the bitmap block and
25
 * the free blocks count in the block.  The descriptors are loaded in memory
26
 * when a file system is mounted (see ext2_read_super).
27
 */
28
 
29
#include <linux/fs.h>
30
#include <linux/ext2_fs.h>
31
#include <linux/stat.h>
32
#include <linux/sched.h>
33
#include <linux/string.h>
34
#include <linux/locks.h>
35
 
36
#include <asm/bitops.h>
37
#include <asm/byteorder.h>
38
 
39
#define in_range(b, first, len)         ((b) >= (first) && (b) <= (first) + (len) - 1)
40
 
41
static struct ext2_group_desc * get_group_desc (struct super_block * sb,
42
                                                unsigned int block_group,
43
                                                struct buffer_head ** bh)
44
{
45
        unsigned long group_desc;
46
        unsigned long desc;
47
        struct ext2_group_desc * gdp;
48
 
49
        /*
50
         * This panic should never trigger on a bad filesystem: the caller
51
         * should have verified the block/group number already.
52
         */
53
        if (block_group >= sb->u.ext2_sb.s_groups_count)
54
                ext2_panic (sb, "get_group_desc",
55
                            "block_group >= groups_count - "
56
                            "block_group = %d, groups_count = %lu",
57
                            block_group, sb->u.ext2_sb.s_groups_count);
58
 
59
        group_desc = block_group / EXT2_DESC_PER_BLOCK(sb);
60
        desc = block_group % EXT2_DESC_PER_BLOCK(sb);
61
        if (!sb->u.ext2_sb.s_group_desc[group_desc])
62
                ext2_panic (sb, "get_group_desc",
63
                            "Group descriptor not loaded - "
64
                            "block_group = %d, group_desc = %lu, desc = %lu",
65
                             block_group, group_desc, desc);
66
        gdp = (struct ext2_group_desc *)
67
              sb->u.ext2_sb.s_group_desc[group_desc]->b_data;
68
        if (bh)
69
                *bh = sb->u.ext2_sb.s_group_desc[group_desc];
70
        return gdp + desc;
71
}
72
 
73
/*
74
 * Read the bitmap for a given block_group, reading into the specified
75
 * slot in the superblock's bitmap cache.
76
 *
77
 * Return >=0 on success or a -ve error code.
78
 */
79
 
80
static int read_block_bitmap (struct super_block * sb,
81
                              unsigned int block_group,
82
                              unsigned long bitmap_nr)
83
{
84
        struct ext2_group_desc * gdp;
85
        struct buffer_head * bh;
86
        int retval = 0;
87
 
88
        gdp = get_group_desc (sb, block_group, NULL);
89
        bh = bread (sb->s_dev, swab32(gdp->bg_block_bitmap), sb->s_blocksize);
90
        if (!bh) {
91
                ext2_error (sb, "read_block_bitmap",
92
                            "Cannot read block bitmap - "
93
                            "block_group = %d, block_bitmap = %lu",
94
                            block_group, (unsigned long) swab32(gdp->bg_block_bitmap));
95
                retval = -EIO;
96
        }
97
        /*
98
         * On IO error, just leave a zero in the superblock's block pointer for
99
         * this group.  The IO will be retried next time.
100
         */
101
        sb->u.ext2_sb.s_block_bitmap_number[bitmap_nr] = block_group;
102
        sb->u.ext2_sb.s_block_bitmap[bitmap_nr] = bh;
103
        return retval;
104
}
105
 
106
/*
107
 * load_block_bitmap loads the block bitmap for a blocks group
108
 *
109
 * It maintains a cache for the last bitmaps loaded.  This cache is managed
110
 * with a LRU algorithm.
111
 *
112
 * Notes:
113
 * 1/ There is one cache per mounted file system.
114
 * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
115
 *    this function reads the bitmap without maintaining a LRU cache.
116
 *
117
 * Return the slot used to store the bitmap, or a -ve error code.
118
 */
119
static int load__block_bitmap (struct super_block * sb,
120
                               unsigned int block_group)
121
{
122
        int i, j, retval = 0;
123
        unsigned long block_bitmap_number;
124
        struct buffer_head * block_bitmap;
125
 
126
        if (block_group >= sb->u.ext2_sb.s_groups_count)
127
                ext2_panic (sb, "load_block_bitmap",
128
                            "block_group >= groups_count - "
129
                            "block_group = %d, groups_count = %lu",
130
                            block_group, sb->u.ext2_sb.s_groups_count);
131
 
132
        if (sb->u.ext2_sb.s_groups_count <= EXT2_MAX_GROUP_LOADED) {
133
                if (sb->u.ext2_sb.s_block_bitmap[block_group]) {
134
                        if (sb->u.ext2_sb.s_block_bitmap_number[block_group] !=
135
                            block_group)
136
                                ext2_panic (sb, "load_block_bitmap",
137
                                            "block_group != block_bitmap_number");
138
                        else
139
                                return block_group;
140
                } else {
141
                        retval = read_block_bitmap (sb, block_group, block_group);
142
                        if (retval < 0)
143
                                return retval;
144
                        return block_group;
145
                }
146
        }
147
 
148
        for (i = 0; i < sb->u.ext2_sb.s_loaded_block_bitmaps &&
149
                    sb->u.ext2_sb.s_block_bitmap_number[i] != block_group; i++)
150
                ;
151
        if (i < sb->u.ext2_sb.s_loaded_block_bitmaps &&
152
            sb->u.ext2_sb.s_block_bitmap_number[i] == block_group) {
153
                block_bitmap_number = sb->u.ext2_sb.s_block_bitmap_number[i];
154
                block_bitmap = sb->u.ext2_sb.s_block_bitmap[i];
155
                for (j = i; j > 0; j--) {
156
                        sb->u.ext2_sb.s_block_bitmap_number[j] =
157
                                sb->u.ext2_sb.s_block_bitmap_number[j - 1];
158
                        sb->u.ext2_sb.s_block_bitmap[j] =
159
                                sb->u.ext2_sb.s_block_bitmap[j - 1];
160
                }
161
                sb->u.ext2_sb.s_block_bitmap_number[0] = block_bitmap_number;
162
                sb->u.ext2_sb.s_block_bitmap[0] = block_bitmap;
163
 
164
                /*
165
                 * There's still one special case here --- if block_bitmap == 0
166
                 * then our last attempt to read the bitmap failed and we have
167
                 * just ended up caching that failure.  Try again to read it.
168
                 */
169
                if (!block_bitmap)
170
                        retval = read_block_bitmap (sb, block_group, 0);
171
        } else {
172
                if (sb->u.ext2_sb.s_loaded_block_bitmaps < EXT2_MAX_GROUP_LOADED)
173
                        sb->u.ext2_sb.s_loaded_block_bitmaps++;
174
                else
175
                        brelse (sb->u.ext2_sb.s_block_bitmap[EXT2_MAX_GROUP_LOADED - 1]);
176
                for (j = sb->u.ext2_sb.s_loaded_block_bitmaps - 1; j > 0;  j--) {
177
                        sb->u.ext2_sb.s_block_bitmap_number[j] =
178
                                sb->u.ext2_sb.s_block_bitmap_number[j - 1];
179
                        sb->u.ext2_sb.s_block_bitmap[j] =
180
                                sb->u.ext2_sb.s_block_bitmap[j - 1];
181
                }
182
                retval = read_block_bitmap (sb, block_group, 0);
183
        }
184
        return retval;
185
}
186
 
187
/*
188
 * Load the block bitmap for a given block group.  First of all do a couple
189
 * of fast lookups for common cases and then pass the request onto the guts
190
 * of the bitmap loader.
191
 *
192
 * Return the slot number of the group in the superblock bitmap cache's on
193
 * success, or a -ve error code.
194
 *
195
 * There is still one inconsistancy here --- if the number of groups in this
196
 * filesystems is <= EXT2_MAX_GROUP_LOADED, then we have no way of
197
 * differentiating between a group for which we have never performed a bitmap
198
 * IO request, and a group for which the last bitmap read request failed.
199
 */
200
static inline int load_block_bitmap (struct super_block * sb,
201
                                     unsigned int block_group)
202
{
203
        int slot;
204
 
205
        /*
206
         * Do the lookup for the slot.  First of all, check if we're asking
207
         * for the same slot as last time, and did we succeed that last time?
208
         */
209
        if (sb->u.ext2_sb.s_loaded_block_bitmaps > 0 &&
210
            sb->u.ext2_sb.s_block_bitmap_number[0] == block_group &&
211
            sb->u.ext2_sb.s_block_bitmap[block_group]) {
212
                slot = 0;
213
        }
214
        /*
215
         * Or can we do a fast lookup based on a loaded group on a filesystem
216
         * small enough to be mapped directly into the superblock?
217
         */
218
        else if (sb->u.ext2_sb.s_groups_count <= EXT2_MAX_GROUP_LOADED &&
219
                 sb->u.ext2_sb.s_block_bitmap_number[block_group] == block_group &&
220
                 sb->u.ext2_sb.s_block_bitmap[block_group]) {
221
                slot = block_group;
222
        }
223
        /*
224
         * If not, then do a full lookup for this block group.
225
         */
226
        else {
227
                slot = load__block_bitmap (sb, block_group);
228
        }
229
 
230
        /*
231
         * <0 means we just got an error
232
         */
233
        if (slot < 0)
234
                return slot;
235
 
236
        /*
237
         * If it's a valid slot, we may still have cached a previous IO error,
238
         * in which case the bh in the superblock cache will be zero.
239
         */
240
        if (!sb->u.ext2_sb.s_block_bitmap[slot])
241
                return -EIO;
242
 
243
        /*
244
         * Must have been read in OK to get this far.
245
         */
246
        return slot;
247
}
248
 
249
void ext2_free_blocks (const struct inode * inode, unsigned long block,
250
                       unsigned long count)
251
{
252
        struct buffer_head * bh;
253
        struct buffer_head * bh2;
254
        unsigned long block_group;
255
        unsigned long bit;
256
        unsigned long i;
257
        int bitmap_nr;
258
        struct super_block * sb;
259
        struct ext2_group_desc * gdp;
260
        struct ext2_super_block * es;
261
 
262
        sb = inode->i_sb;
263
        if (!sb) {
264
                printk ("ext2_free_blocks: nonexistent device");
265
                return;
266
        }
267
        lock_super (sb);
268
        es = sb->u.ext2_sb.s_es;
269
        if (block < swab32(es->s_first_data_block) ||
270
            (block + count) > swab32(es->s_blocks_count)) {
271
                ext2_error (sb, "ext2_free_blocks",
272
                            "Freeing blocks not in datazone - "
273
                            "block = %lu, count = %lu", block, count);
274
                unlock_super (sb);
275
                return;
276
        }
277
 
278
        ext2_debug ("freeing block %lu\n", block);
279
 
280
        block_group = (block - swab32(es->s_first_data_block)) /
281
                      EXT2_BLOCKS_PER_GROUP(sb);
282
        bit = (block - swab32(es->s_first_data_block)) % EXT2_BLOCKS_PER_GROUP(sb);
283
        if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) {
284
                ext2_error (sb, "ext2_free_blocks",
285
                            "Freeing blocks across group boundary - "
286
                            "Block = %lu, count = %lu",
287
                            block, count);
288
                unlock_super (sb);
289
                return;
290
        }
291
 
292
        bitmap_nr = load_block_bitmap (sb, block_group);
293
        if (bitmap_nr < 0)
294
                goto error_return;
295
 
296
        bh = sb->u.ext2_sb.s_block_bitmap[bitmap_nr];
297
        gdp = get_group_desc (sb, block_group, &bh2);
298
 
299
        if (test_opt (sb, CHECK_STRICT) &&
300
            (in_range (swab32(gdp->bg_block_bitmap), block, count) ||
301
             in_range (swab32(gdp->bg_inode_bitmap), block, count) ||
302
             in_range (block, swab32(gdp->bg_inode_table),
303
                       sb->u.ext2_sb.s_itb_per_group) ||
304
             in_range (block + count - 1, swab32(gdp->bg_inode_table),
305
                       sb->u.ext2_sb.s_itb_per_group))) {
306
                ext2_error (sb, "ext2_free_blocks",
307
                            "Freeing blocks in system zones - "
308
                            "Block = %lu, count = %lu",
309
                            block, count);
310
                unlock_super (sb);
311
                return;
312
        }
313
 
314
        for (i = 0; i < count; i++) {
315
                if (!__ext2_clear_bit (bit + i, bh->b_data))
316
                        ext2_warning (sb, "ext2_free_blocks",
317
                                      "bit already cleared for block %lu",
318
                                      block);
319
                else {
320
                        if (sb->dq_op)
321
                                sb->dq_op->free_block(inode, fs_to_dq_blocks(1, sb->s_blocksize));
322
                        gdp->bg_free_blocks_count =
323
                                swab16(swab16(gdp->bg_free_blocks_count)+1);
324
                        es->s_free_blocks_count =
325
                                swab32(swab32(es->s_free_blocks_count)+1);
326
                }
327
        }
328
 
329
        mark_buffer_dirty(bh2, 1);
330
        mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
331
 
332
        mark_buffer_dirty(bh, 1);
333
        if (sb->s_flags & MS_SYNCHRONOUS) {
334
                ll_rw_block (WRITE, 1, &bh);
335
                wait_on_buffer (bh);
336
        }
337
        sb->s_dirt = 1;
338
error_return:
339
        unlock_super (sb);
340
        return;
341
}
342
 
343
/*
344
 * ext2_new_block uses a goal block to assist allocation.  If the goal is
345
 * free, or there is a free block within 32 blocks of the goal, that block
346
 * is allocated.  Otherwise a forward search is made for a free block; within
347
 * each block group the search first looks for an entire free byte in the block
348
 * bitmap, and then for any free bit if that fails.
349
 */
350
int ext2_new_block (const struct inode * inode, unsigned long goal,
351
                    u32 * prealloc_count, u32 * prealloc_block, int * err)
352
{
353
        struct buffer_head * bh;
354
        struct buffer_head * bh2;
355
        char * p, * r;
356
        int i, j, k, tmp;
357
        int bitmap_nr;
358
        struct super_block * sb;
359
        struct ext2_group_desc * gdp;
360
        struct ext2_super_block * es;
361
 
362
        *err = -ENOSPC;
363
#ifdef EXT2FS_DEBUG
364
        static int goal_hits = 0, goal_attempts = 0;
365
#endif
366
        sb = inode->i_sb;
367
        if (!sb) {
368
                printk ("ext2_new_block: nonexistent device");
369
                return 0;
370
        }
371
        lock_super (sb);
372
        es = sb->u.ext2_sb.s_es;
373
        if (swab32(es->s_free_blocks_count) <= swab32(es->s_r_blocks_count) &&
374
            (!fsuser() && (sb->u.ext2_sb.s_resuid != current->fsuid) &&
375
             (sb->u.ext2_sb.s_resgid == 0 ||
376
              !in_group_p (sb->u.ext2_sb.s_resgid)))) {
377
                unlock_super (sb);
378
                return 0;
379
        }
380
 
381
        ext2_debug ("goal=%lu.\n", goal);
382
 
383
repeat:
384
        /*
385
         * First, test whether the goal block is free.
386
         */
387
        if (goal < swab32(es->s_first_data_block) || goal >= swab32(es->s_blocks_count))
388
                goal = swab32(es->s_first_data_block);
389
        i = (goal - swab32(es->s_first_data_block)) / EXT2_BLOCKS_PER_GROUP(sb);
390
        gdp = get_group_desc (sb, i, &bh2);
391
        if (swab16(gdp->bg_free_blocks_count) > 0) {
392
                j = ((goal - swab32(es->s_first_data_block)) % EXT2_BLOCKS_PER_GROUP(sb));
393
#ifdef EXT2FS_DEBUG
394
                if (j)
395
                        goal_attempts++;
396
#endif
397
                bitmap_nr = load_block_bitmap (sb, i);
398
                if (bitmap_nr < 0) {
399
                        *err = -EIO;
400
                        unlock_super (sb);
401
                        return 0;
402
                }
403
 
404
                bh = sb->u.ext2_sb.s_block_bitmap[bitmap_nr];
405
 
406
                ext2_debug ("goal is at %d:%d.\n", i, j);
407
 
408
                if (!__ext2_test_bit(j, bh->b_data)) {
409
#ifdef EXT2FS_DEBUG
410
                        goal_hits++;
411
                        ext2_debug ("goal bit allocated.\n");
412
#endif
413
                        goto got_block;
414
                }
415
                if (j) {
416
                        /*
417
                         * The goal was occupied; search forward for a free
418
                         * block within the next XX blocks.
419
                         *
420
                         * end_goal is more or less random, but it has to be
421
                         * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
422
                         * next 64-bit boundary is simple..
423
                         */
424
                        int end_goal = (j + 63) & ~63;
425
                        j = __ext2_find_next_zero_bit(bh->b_data, end_goal, j);
426
                        if (j < end_goal)
427
                                goto got_block;
428
                }
429
 
430
                ext2_debug ("Bit not found near goal\n");
431
 
432
                /*
433
                 * There has been no free block found in the near vicinity
434
                 * of the goal: do a search forward through the block groups,
435
                 * searching in each group first for an entire free byte in
436
                 * the bitmap and then for any free bit.
437
                 *
438
                 * Search first in the remainder of the current group; then,
439
                 * cyclicly search through the rest of the groups.
440
                 */
441
                p = ((char *) bh->b_data) + (j >> 3);
442
                r = memscan(p, 0, (EXT2_BLOCKS_PER_GROUP(sb) - j + 7) >> 3);
443
                k = (r - ((char *) bh->b_data)) << 3;
444
                if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
445
                        j = k;
446
                        goto search_back;
447
                }
448
                k = __ext2_find_next_zero_bit ((unsigned long *) bh->b_data,
449
                                        EXT2_BLOCKS_PER_GROUP(sb),
450
                                        j);
451
                if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
452
                        j = k;
453
                        goto got_block;
454
                }
455
        }
456
 
457
        ext2_debug ("Bit not found in block group %d.\n", i);
458
 
459
        /*
460
         * Now search the rest of the groups.  We assume that
461
         * i and gdp correctly point to the last group visited.
462
         */
463
        for (k = 0; k < sb->u.ext2_sb.s_groups_count; k++) {
464
                i++;
465
                if (i >= sb->u.ext2_sb.s_groups_count)
466
                        i = 0;
467
                gdp = get_group_desc (sb, i, &bh2);
468
                if (swab16(gdp->bg_free_blocks_count) > 0)
469
                        break;
470
        }
471
        if (k >= sb->u.ext2_sb.s_groups_count) {
472
                unlock_super (sb);
473
                return 0;
474
        }
475
 
476
        bitmap_nr = load_block_bitmap (sb, i);
477
        if (bitmap_nr < 0) {
478
                *err = -EIO;
479
                unlock_super (sb);
480
                return 0;
481
        }
482
 
483
        bh = sb->u.ext2_sb.s_block_bitmap[bitmap_nr];
484
        r = memscan(bh->b_data, 0, EXT2_BLOCKS_PER_GROUP(sb) >> 3);
485
        j = (r - bh->b_data) << 3;
486
        if (j < EXT2_BLOCKS_PER_GROUP(sb))
487
                goto search_back;
488
        else
489
                j = __ext2_find_first_zero_bit ((unsigned long *) bh->b_data,
490
                                         EXT2_BLOCKS_PER_GROUP(sb));
491
        if (j >= EXT2_BLOCKS_PER_GROUP(sb)) {
492
                ext2_error (sb, "ext2_new_block",
493
                            "Free blocks count corrupted for block group %d", i);
494
                unlock_super (sb);
495
                return 0;
496
        }
497
 
498
search_back:
499
        /*
500
         * We have succeeded in finding a free byte in the block
501
         * bitmap.  Now search backwards up to 7 bits to find the
502
         * start of this group of free blocks.
503
         */
504
        for (k = 0; k < 7 && j > 0 && !__ext2_test_bit (j - 1, bh->b_data); k++, j--);
505
 
506
got_block:
507
 
508
        ext2_debug ("using block group %d(%d)\n", i, gdp->bg_free_blocks_count);
509
 
510
        /*
511
         * Check quota for allocation of this block.
512
         */
513
        if (sb->dq_op)
514
                if (sb->dq_op->alloc_block (inode, fs_to_dq_blocks(1, sb->s_blocksize))) {
515
                        unlock_super (sb);
516
                        *err = -EDQUOT;
517
                        return 0;
518
                }
519
 
520
        tmp = j + i * EXT2_BLOCKS_PER_GROUP(sb) + swab32(es->s_first_data_block);
521
 
522
        if (test_opt (sb, CHECK_STRICT) &&
523
            (tmp == swab32(gdp->bg_block_bitmap) ||
524
             tmp == swab32(gdp->bg_inode_bitmap) ||
525
             in_range (tmp, swab32(gdp->bg_inode_table), sb->u.ext2_sb.s_itb_per_group))) {
526
                ext2_error (sb, "ext2_new_block",
527
                            "Allocating block in system zone - "
528
                            "block = %u", tmp);
529
                unlock_super (sb);
530
                *err = -EIO;
531
                return 0;
532
        }
533
 
534
        if (__ext2_set_bit (j, bh->b_data)) {
535
                ext2_warning (sb, "ext2_new_block",
536
                              "bit already set for block %d", j);
537
                if (sb->dq_op)
538
                        sb->dq_op->free_block(inode, fs_to_dq_blocks(1, sb->s_blocksize));
539
                goto repeat;
540
        }
541
 
542
        ext2_debug ("found bit %d\n", j);
543
 
544
        /*
545
         * Do block preallocation now if required.
546
         */
547
#ifdef EXT2_PREALLOCATE
548
        if (prealloc_block) {
549
                *prealloc_count = 0;
550
                *prealloc_block = tmp + 1;
551
                for (k = 1;
552
                     k < 8 && (j + k) < EXT2_BLOCKS_PER_GROUP(sb); k++) {
553
                        if (sb->dq_op)
554
                                if (sb->dq_op->alloc_block(inode, fs_to_dq_blocks(1, sb->s_blocksize)))
555
                                        break;
556
                        if (__ext2_set_bit (j + k, bh->b_data)) {
557
                                if (sb->dq_op)
558
                                        sb->dq_op->free_block(inode, fs_to_dq_blocks(1, sb->s_blocksize));
559
                                break;
560
                        }
561
                        (*prealloc_count)++;
562
                }
563
                gdp->bg_free_blocks_count =
564
                        swab16(swab16(gdp->bg_free_blocks_count) -
565
                               *prealloc_count);
566
                es->s_free_blocks_count =
567
                        swab32(swab32(es->s_free_blocks_count) -
568
                               *prealloc_count);
569
                ext2_debug ("Preallocated a further %lu bits.\n",
570
                            *prealloc_count);
571
        }
572
#endif
573
 
574
        j = tmp;
575
 
576
        mark_buffer_dirty(bh, 1);
577
        if (sb->s_flags & MS_SYNCHRONOUS) {
578
                ll_rw_block (WRITE, 1, &bh);
579
                wait_on_buffer (bh);
580
        }
581
 
582
        if (j >= swab32(es->s_blocks_count)) {
583
                ext2_error (sb, "ext2_new_block",
584
                            "block >= blocks count - "
585
                            "block_group = %d, block=%d", i, j);
586
                unlock_super (sb);
587
                return 0;
588
        }
589
        if (!(bh = getblk (sb->s_dev, j, sb->s_blocksize))) {
590
                ext2_error (sb, "ext2_new_block", "cannot get block %d", j);
591
                unlock_super (sb);
592
                return 0;
593
        }
594
        memset(bh->b_data, 0, sb->s_blocksize);
595
        mark_buffer_uptodate(bh, 1);
596
        mark_buffer_dirty(bh, 1);
597
        brelse (bh);
598
 
599
        ext2_debug ("allocating block %d. "
600
                    "Goal hits %d of %d.\n", j, goal_hits, goal_attempts);
601
 
602
        gdp->bg_free_blocks_count = swab16(swab16(gdp->bg_free_blocks_count) - 1);
603
        mark_buffer_dirty(bh2, 1);
604
        es->s_free_blocks_count = swab32(swab32(es->s_free_blocks_count) - 1);
605
        mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
606
        sb->s_dirt = 1;
607
        unlock_super (sb);
608
        *err = 0;
609
        return j;
610
}
611
 
612
unsigned long ext2_count_free_blocks (struct super_block * sb)
613
{
614
#ifdef EXT2FS_DEBUG
615
        struct ext2_super_block * es;
616
        unsigned long desc_count, bitmap_count, x;
617
        int bitmap_nr;
618
        struct ext2_group_desc * gdp;
619
        int i;
620
 
621
        lock_super (sb);
622
        es = sb->u.ext2_sb.s_es;
623
        desc_count = 0;
624
        bitmap_count = 0;
625
        gdp = NULL;
626
        for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
627
                gdp = get_group_desc (sb, i, NULL);
628
                desc_count += swab16(gdp->bg_free_blocks_count);
629
 
630
                bitmap_nr = load_block_bitmap (sb, i);
631
                if (bitmap_nr < 0)
632
                        continue;
633
 
634
                x = ext2_count_free (sb->u.ext2_sb.s_block_bitmap[bitmap_nr],
635
                                     sb->s_blocksize);
636
                printk ("group %d: stored = %d, counted = %lu\n",
637
                        i, swab16(gdp->bg_free_blocks_count), x);
638
                bitmap_count += x;
639
        }
640
        printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n",
641
               swab32(es->s_free_blocks_count), desc_count, bitmap_count);
642
        unlock_super (sb);
643
        return bitmap_count;
644
#else
645
        return swab32(sb->u.ext2_sb.s_es->s_free_blocks_count);
646
#endif
647
}
648
 
649
static inline int block_in_use (unsigned long block,
650
                                struct super_block * sb,
651
                                unsigned char * map)
652
{
653
        return __ext2_test_bit ((block - swab32(sb->u.ext2_sb.s_es->s_first_data_block)) %
654
                         EXT2_BLOCKS_PER_GROUP(sb), map);
655
}
656
 
657
void ext2_check_blocks_bitmap (struct super_block * sb)
658
{
659
        struct buffer_head * bh;
660
        struct ext2_super_block * es;
661
        unsigned long desc_count, bitmap_count, x;
662
        unsigned long desc_blocks;
663
        int bitmap_nr;
664
        struct ext2_group_desc * gdp;
665
        int i, j;
666
 
667
        lock_super (sb);
668
        es = sb->u.ext2_sb.s_es;
669
        desc_count = 0;
670
        bitmap_count = 0;
671
        gdp = NULL;
672
        desc_blocks = (sb->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
673
                      EXT2_DESC_PER_BLOCK(sb);
674
        for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
675
                gdp = get_group_desc (sb, i, NULL);
676
                desc_count += swab16(gdp->bg_free_blocks_count);
677
                bitmap_nr = load_block_bitmap (sb, i);
678
                if (bitmap_nr < 0)
679
                        continue;
680
 
681
                bh = sb->u.ext2_sb.s_block_bitmap[bitmap_nr];
682
 
683
                if (!__ext2_test_bit (0, bh->b_data))
684
                        ext2_error (sb, "ext2_check_blocks_bitmap",
685
                                    "Superblock in group %d is marked free", i);
686
 
687
                for (j = 0; j < desc_blocks; j++)
688
                        if (!__ext2_test_bit (j + 1, bh->b_data))
689
                                ext2_error (sb, "ext2_check_blocks_bitmap",
690
                                            "Descriptor block #%d in group "
691
                                            "%d is marked free", j, i);
692
 
693
                if (!block_in_use (swab32(gdp->bg_block_bitmap), sb, bh->b_data))
694
                        ext2_error (sb, "ext2_check_blocks_bitmap",
695
                                    "Block bitmap for group %d is marked free",
696
                                    i);
697
 
698
                if (!block_in_use (swab32(gdp->bg_inode_bitmap), sb, bh->b_data))
699
                        ext2_error (sb, "ext2_check_blocks_bitmap",
700
                                    "Inode bitmap for group %d is marked free",
701
                                    i);
702
 
703
                for (j = 0; j < sb->u.ext2_sb.s_itb_per_group; j++)
704
                        if (!block_in_use (swab32(gdp->bg_inode_table) + j, sb, bh->b_data))
705
                                ext2_error (sb, "ext2_check_blocks_bitmap",
706
                                            "Block #%d of the inode table in "
707
                                            "group %d is marked free", j, i);
708
 
709
                x = ext2_count_free (bh, sb->s_blocksize);
710
                if (swab16(gdp->bg_free_blocks_count) != x)
711
                        ext2_error (sb, "ext2_check_blocks_bitmap",
712
                                    "Wrong free blocks count for group %d, "
713
                                    "stored = %d, counted = %lu", i,
714
                                    swab16(gdp->bg_free_blocks_count), x);
715
                bitmap_count += x;
716
        }
717
        if (swab32(es->s_free_blocks_count) != bitmap_count)
718
                ext2_error (sb, "ext2_check_blocks_bitmap",
719
                            "Wrong free blocks count in super block, "
720
                            "stored = %lu, counted = %lu",
721
                            (unsigned long) swab32(es->s_free_blocks_count), bitmap_count);
722
        unlock_super (sb);
723
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.