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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [fs/] [ext2/] [super.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 *  linux/fs/ext2/super.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
 *  from
10
 *
11
 *  linux/fs/minix/inode.c
12
 *
13
 *  Copyright (C) 1991, 1992  Linus Torvalds
14
 *
15
 *  Big-endian to little-endian byte-swapping/bitmaps by
16
 *        David S. Miller (davem@caip.rutgers.edu), 1995
17
 */
18
 
19
#include <linux/module.h>
20
 
21
#include <stdarg.h>
22
 
23
#include <asm/bitops.h>
24
#include <asm/segment.h>
25
#include <asm/system.h>
26
 
27
#include <linux/errno.h>
28
#include <linux/fs.h>
29
#include <linux/ext2_fs.h>
30
#include <linux/malloc.h>
31
#include <linux/sched.h>
32
#include <linux/stat.h>
33
#include <linux/string.h>
34
#include <linux/locks.h>
35
 
36
static char error_buf[1024];
37
 
38
void ext2_error (struct super_block * sb, const char * function,
39
                 const char * fmt, ...)
40
{
41
        va_list args;
42
 
43
        if (!(sb->s_flags & MS_RDONLY)) {
44
                sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
45
                sb->u.ext2_sb.s_es->s_state =
46
                        swab16(swab16(sb->u.ext2_sb.s_es->s_state) | EXT2_ERROR_FS);
47
                mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
48
                sb->s_dirt = 1;
49
        }
50
        va_start (args, fmt);
51
        vsprintf (error_buf, fmt, args);
52
        va_end (args);
53
        if (test_opt (sb, ERRORS_PANIC) ||
54
            (swab16(sb->u.ext2_sb.s_es->s_errors) == EXT2_ERRORS_PANIC &&
55
             !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_RO)))
56
                panic ("EXT2-fs panic (device %s): %s: %s\n",
57
                       kdevname(sb->s_dev), function, error_buf);
58
        printk (KERN_CRIT "EXT2-fs error (device %s): %s: %s\n",
59
                kdevname(sb->s_dev), function, error_buf);
60
        if (test_opt (sb, ERRORS_RO) ||
61
            (swab16(sb->u.ext2_sb.s_es->s_errors) == EXT2_ERRORS_RO &&
62
             !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_PANIC))) {
63
                printk ("Remounting filesystem read-only\n");
64
                sb->s_flags |= MS_RDONLY;
65
        }
66
}
67
 
68
NORET_TYPE void ext2_panic (struct super_block * sb, const char * function,
69
                            const char * fmt, ...)
70
{
71
        va_list args;
72
 
73
        if (!(sb->s_flags & MS_RDONLY)) {
74
                sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
75
                sb->u.ext2_sb.s_es->s_state =
76
                        swab16(swab16(sb->u.ext2_sb.s_es->s_state) | EXT2_ERROR_FS);
77
                mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
78
                sb->s_dirt = 1;
79
        }
80
        va_start (args, fmt);
81
        vsprintf (error_buf, fmt, args);
82
        va_end (args);
83
        /* this is to prevent panic from syncing this filesystem */
84
        if (sb->s_lock)
85
                sb->s_lock=0;
86
        sb->s_flags |= MS_RDONLY;
87
        panic ("EXT2-fs panic (device %s): %s: %s\n",
88
               kdevname(sb->s_dev), function, error_buf);
89
}
90
 
91
void ext2_warning (struct super_block * sb, const char * function,
92
                   const char * fmt, ...)
93
{
94
        va_list args;
95
 
96
        va_start (args, fmt);
97
        vsprintf (error_buf, fmt, args);
98
        va_end (args);
99
        printk (KERN_WARNING "EXT2-fs warning (device %s): %s: %s\n",
100
                kdevname(sb->s_dev), function, error_buf);
101
}
102
 
103
void ext2_put_super (struct super_block * sb)
104
{
105
        int db_count;
106
        int i;
107
 
108
        lock_super (sb);
109
        if (!(sb->s_flags & MS_RDONLY)) {
110
                sb->u.ext2_sb.s_es->s_state = swab16(sb->u.ext2_sb.s_mount_state);
111
                mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
112
        }
113
        sb->s_dev = 0;
114
        db_count = sb->u.ext2_sb.s_db_per_group;
115
        for (i = 0; i < db_count; i++)
116
                if (sb->u.ext2_sb.s_group_desc[i])
117
                        brelse (sb->u.ext2_sb.s_group_desc[i]);
118
        kfree_s (sb->u.ext2_sb.s_group_desc,
119
                 db_count * sizeof (struct buffer_head *));
120
        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
121
                if (sb->u.ext2_sb.s_inode_bitmap[i])
122
                        brelse (sb->u.ext2_sb.s_inode_bitmap[i]);
123
        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
124
                if (sb->u.ext2_sb.s_block_bitmap[i])
125
                        brelse (sb->u.ext2_sb.s_block_bitmap[i]);
126
        brelse (sb->u.ext2_sb.s_sbh);
127
        unlock_super (sb);
128
        MOD_DEC_USE_COUNT;
129
        return;
130
}
131
 
132
static struct super_operations ext2_sops = {
133
        ext2_read_inode,
134
        NULL,
135
        ext2_write_inode,
136
        ext2_put_inode,
137
        ext2_put_super,
138
        ext2_write_super,
139
        ext2_statfs,
140
        ext2_remount
141
};
142
 
143
/*
144
 * This function has been shamelessly adapted from the msdos fs
145
 */
146
static int parse_options (char * options, unsigned long * sb_block,
147
                          unsigned short *resuid, unsigned short * resgid,
148
                          unsigned long * mount_options)
149
{
150
        char * this_char;
151
        char * value;
152
 
153
        if (!options)
154
                return 1;
155
        for (this_char = strtok (options, ",");
156
             this_char != NULL;
157
             this_char = strtok (NULL, ",")) {
158
                if ((value = strchr (this_char, '=')) != NULL)
159
                        *value++ = 0;
160
                if (!strcmp (this_char, "bsddf"))
161
                        clear_opt (*mount_options, MINIX_DF);
162
                else if (!strcmp (this_char, "check")) {
163
                        if (!value || !*value)
164
                                set_opt (*mount_options, CHECK_NORMAL);
165
                        else if (!strcmp (value, "none")) {
166
                                clear_opt (*mount_options, CHECK_NORMAL);
167
                                clear_opt (*mount_options, CHECK_STRICT);
168
                        }
169
                        else if (!strcmp (value, "normal"))
170
                                set_opt (*mount_options, CHECK_NORMAL);
171
                        else if (!strcmp (value, "strict")) {
172
                                set_opt (*mount_options, CHECK_NORMAL);
173
                                set_opt (*mount_options, CHECK_STRICT);
174
                        }
175
                        else {
176
                                printk ("EXT2-fs: Invalid check option: %s\n",
177
                                        value);
178
                                return 0;
179
                        }
180
                }
181
                else if (!strcmp (this_char, "debug"))
182
                        set_opt (*mount_options, DEBUG);
183
                else if (!strcmp (this_char, "errors")) {
184
                        if (!value || !*value) {
185
                                printk ("EXT2-fs: the errors option requires "
186
                                        "an argument");
187
                                return 0;
188
                        }
189
                        if (!strcmp (value, "continue")) {
190
                                clear_opt (*mount_options, ERRORS_RO);
191
                                clear_opt (*mount_options, ERRORS_PANIC);
192
                                set_opt (*mount_options, ERRORS_CONT);
193
                        }
194
                        else if (!strcmp (value, "remount-ro")) {
195
                                clear_opt (*mount_options, ERRORS_CONT);
196
                                clear_opt (*mount_options, ERRORS_PANIC);
197
                                set_opt (*mount_options, ERRORS_RO);
198
                        }
199
                        else if (!strcmp (value, "panic")) {
200
                                clear_opt (*mount_options, ERRORS_CONT);
201
                                clear_opt (*mount_options, ERRORS_RO);
202
                                set_opt (*mount_options, ERRORS_PANIC);
203
                        }
204
                        else {
205
                                printk ("EXT2-fs: Invalid errors option: %s\n",
206
                                        value);
207
                                return 0;
208
                        }
209
                }
210
                else if (!strcmp (this_char, "grpid") ||
211
                         !strcmp (this_char, "bsdgroups"))
212
                        set_opt (*mount_options, GRPID);
213
                else if (!strcmp (this_char, "minixdf"))
214
                        set_opt (*mount_options, MINIX_DF);
215
                else if (!strcmp (this_char, "nocheck")) {
216
                        clear_opt (*mount_options, CHECK_NORMAL);
217
                        clear_opt (*mount_options, CHECK_STRICT);
218
                }
219
                else if (!strcmp (this_char, "nogrpid") ||
220
                         !strcmp (this_char, "sysvgroups"))
221
                        clear_opt (*mount_options, GRPID);
222
                else if (!strcmp (this_char, "resgid")) {
223
                        if (!value || !*value) {
224
                                printk ("EXT2-fs: the resgid option requires "
225
                                        "an argument");
226
                                return 0;
227
                        }
228
                        *resgid = simple_strtoul (value, &value, 0);
229
                        if (*value) {
230
                                printk ("EXT2-fs: Invalid resgid option: %s\n",
231
                                        value);
232
                                return 0;
233
                        }
234
                }
235
                else if (!strcmp (this_char, "resuid")) {
236
                        if (!value || !*value) {
237
                                printk ("EXT2-fs: the resuid option requires "
238
                                        "an argument");
239
                                return 0;
240
                        }
241
                        *resuid = simple_strtoul (value, &value, 0);
242
                        if (*value) {
243
                                printk ("EXT2-fs: Invalid resuid option: %s\n",
244
                                        value);
245
                                return 0;
246
                        }
247
                }
248
                else if (!strcmp (this_char, "sb")) {
249
                        if (!value || !*value) {
250
                                printk ("EXT2-fs: the sb option requires "
251
                                        "an argument");
252
                                return 0;
253
                        }
254
                        *sb_block = simple_strtoul (value, &value, 0);
255
                        if (*value) {
256
                                printk ("EXT2-fs: Invalid sb option: %s\n",
257
                                        value);
258
                                return 0;
259
                        }
260
                }
261
                /* Silently ignore the quota options */
262
                else if (!strcmp (this_char, "grpquota")
263
                         || !strcmp (this_char, "noquota")
264
                         || !strcmp (this_char, "quota")
265
                         || !strcmp (this_char, "usrquota"))
266
                        /* Don't do anything ;-) */ ;
267
                else {
268
                        printk ("EXT2-fs: Unrecognized mount option %s\n", this_char);
269
                        return 0;
270
                }
271
        }
272
        return 1;
273
}
274
 
275
static void ext2_setup_super (struct super_block * sb,
276
                              struct ext2_super_block * es)
277
{
278
        if (swab32(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
279
                        printk ("EXT2-fs warning: revision level too high, "
280
                                "forcing read/only mode\n");
281
                        sb->s_flags |= MS_RDONLY;
282
        }
283
        if (!(sb->s_flags & MS_RDONLY)) {
284
                if (!(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
285
                        printk ("EXT2-fs warning: mounting unchecked fs, "
286
                                "running e2fsck is recommended\n");
287
                else if ((sb->u.ext2_sb.s_mount_state & EXT2_ERROR_FS))
288
                        printk ("EXT2-fs warning: mounting fs with errors, "
289
                                "running e2fsck is recommended\n");
290
                else if ((__s16) swab16(es->s_max_mnt_count) >= 0 &&
291
                         swab16(es->s_mnt_count) >=
292
                         (unsigned short) (__s16) swab16(es->s_max_mnt_count))
293
                        printk ("EXT2-fs warning: maximal mount count reached, "
294
                                "running e2fsck is recommended\n");
295
                else if (swab32(es->s_checkinterval) &&
296
                        (swab32(es->s_lastcheck) + swab32(es->s_checkinterval) <= CURRENT_TIME))
297
                        printk ("EXT2-fs warning: checktime reached, "
298
                                "running e2fsck is recommended\n");
299
                es->s_state = swab16(swab16(es->s_state) & ~EXT2_VALID_FS);
300
                if (!(__s16) swab16(es->s_max_mnt_count))
301
                        es->s_max_mnt_count = (__s16) swab16(EXT2_DFL_MAX_MNT_COUNT);
302
                es->s_mnt_count=swab16(swab16(es->s_mnt_count) + 1);
303
                es->s_mtime = swab32(CURRENT_TIME);
304
                mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
305
                sb->s_dirt = 1;
306
                if (test_opt (sb, DEBUG))
307
                        printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
308
                                "bpg=%lu, ipg=%lu, mo=%04lx]\n",
309
                                EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
310
                                sb->u.ext2_sb.s_frag_size,
311
                                sb->u.ext2_sb.s_groups_count,
312
                                EXT2_BLOCKS_PER_GROUP(sb),
313
                                EXT2_INODES_PER_GROUP(sb),
314
                                sb->u.ext2_sb.s_mount_opt);
315
                if (test_opt (sb, CHECK)) {
316
                        ext2_check_blocks_bitmap (sb);
317
                        ext2_check_inodes_bitmap (sb);
318
                }
319
        }
320
}
321
 
322
static int ext2_check_descriptors (struct super_block * sb)
323
{
324
        int i;
325
        int desc_block = 0;
326
        unsigned long block = swab32(sb->u.ext2_sb.s_es->s_first_data_block);
327
        struct ext2_group_desc * gdp = NULL;
328
 
329
        ext2_debug ("Checking group descriptors");
330
 
331
        for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++)
332
        {
333
                if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
334
                        gdp = (struct ext2_group_desc *) sb->u.ext2_sb.s_group_desc[desc_block++]->b_data;
335
                if (swab32(gdp->bg_block_bitmap) < block ||
336
                    swab32(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
337
                {
338
                        ext2_error (sb, "ext2_check_descriptors",
339
                                    "Block bitmap for group %d"
340
                                    " not in group (block %lu)!",
341
                                    i, (unsigned long) swab32(gdp->bg_block_bitmap));
342
                        return 0;
343
                }
344
                if (swab32(gdp->bg_inode_bitmap) < block ||
345
                    swab32(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
346
                {
347
                        ext2_error (sb, "ext2_check_descriptors",
348
                                    "Inode bitmap for group %d"
349
                                    " not in group (block %lu)!",
350
                                    i, (unsigned long) swab32(gdp->bg_inode_bitmap));
351
                        return 0;
352
                }
353
                if (swab32(gdp->bg_inode_table) < block ||
354
                    swab32(gdp->bg_inode_table) + sb->u.ext2_sb.s_itb_per_group >=
355
                    block + EXT2_BLOCKS_PER_GROUP(sb))
356
                {
357
                        ext2_error (sb, "ext2_check_descriptors",
358
                                    "Inode table for group %d"
359
                                    " not in group (block %lu)!",
360
                                    i, (unsigned long) swab32(gdp->bg_inode_table));
361
                        return 0;
362
                }
363
                block += EXT2_BLOCKS_PER_GROUP(sb);
364
                gdp++;
365
        }
366
        return 1;
367
}
368
 
369
#define log2(n) ffz(~(n))
370
 
371
struct super_block * ext2_read_super (struct super_block * sb, void * data,
372
                                      int silent)
373
{
374
        struct buffer_head * bh;
375
        struct ext2_super_block * es;
376
        unsigned long sb_block = 1;
377
        unsigned short resuid = EXT2_DEF_RESUID;
378
        unsigned short resgid = EXT2_DEF_RESGID;
379
        unsigned long logic_sb_block = 1;
380
        kdev_t dev = sb->s_dev;
381
        int db_count;
382
        int i, j;
383
 
384
        sb->u.ext2_sb.s_mount_opt = 0;
385
        set_opt (sb->u.ext2_sb.s_mount_opt, CHECK_NORMAL);
386
        if (!parse_options ((char *) data, &sb_block, &resuid, &resgid,
387
            &sb->u.ext2_sb.s_mount_opt)) {
388
                sb->s_dev = 0;
389
                return NULL;
390
        }
391
 
392
        MOD_INC_USE_COUNT;
393
        lock_super (sb);
394
        set_blocksize (dev, BLOCK_SIZE);
395
        if (!(bh = bread (dev, sb_block, BLOCK_SIZE))) {
396
                sb->s_dev = 0;
397
                unlock_super (sb);
398
                printk ("EXT2-fs: unable to read superblock\n");
399
                MOD_DEC_USE_COUNT;
400
                return NULL;
401
        }
402
        /*
403
         * Note: s_es must be initialized s_es as soon as possible because
404
         * some ext2 macro-instructions depend on its value
405
         */
406
        es = (struct ext2_super_block *) bh->b_data;
407
        sb->u.ext2_sb.s_es = es;
408
        sb->s_magic = swab16(es->s_magic);
409
        if (sb->s_magic != EXT2_SUPER_MAGIC) {
410
                if (!silent)
411
                        printk ("VFS: Can't find an ext2 filesystem on dev "
412
                                "%s.\n", kdevname(dev));
413
        failed_mount:
414
                sb->s_dev = 0;
415
                unlock_super (sb);
416
                if (bh)
417
                        brelse(bh);
418
                MOD_DEC_USE_COUNT;
419
                return NULL;
420
        }
421
        if (swab32(es->s_rev_level) > EXT2_GOOD_OLD_REV) {
422
                if (swab32(es->s_feature_incompat) & ~EXT2_FEATURE_INCOMPAT_SUPP) {
423
                        printk("EXT2-fs: %s: couldn't mount because of "
424
                               "unsupported optional features.\n",
425
                               kdevname(dev));
426
                        goto failed_mount;
427
                }
428
                if (!(sb->s_flags & MS_RDONLY) &&
429
                    (swab32(es->s_feature_ro_compat) & ~EXT2_FEATURE_RO_COMPAT_SUPP)) {
430
                        printk("EXT2-fs: %s: couldn't mount RDWR because of "
431
                               "unsupported optional features.\n",
432
                               kdevname(dev));
433
                        goto failed_mount;
434
                }
435
        }
436
        sb->s_blocksize_bits = swab32(sb->u.ext2_sb.s_es->s_log_block_size) + 10;
437
        sb->s_blocksize = 1 << sb->s_blocksize_bits;
438
        if (sb->s_blocksize != BLOCK_SIZE &&
439
            (sb->s_blocksize == 1024 || sb->s_blocksize == 2048 ||
440
             sb->s_blocksize == 4096)) {
441
                unsigned long offset;
442
 
443
                brelse (bh);
444
                set_blocksize (dev, sb->s_blocksize);
445
                logic_sb_block = (sb_block*BLOCK_SIZE) / sb->s_blocksize;
446
                offset = (sb_block*BLOCK_SIZE) % sb->s_blocksize;
447
                bh = bread (dev, logic_sb_block, sb->s_blocksize);
448
                if(!bh) {
449
                        printk("EXT2-fs: Couldn't read superblock on "
450
                               "2nd try.\n");
451
                        goto failed_mount;
452
                }
453
                es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
454
                sb->u.ext2_sb.s_es = es;
455
                if (es->s_magic != swab16(EXT2_SUPER_MAGIC)) {
456
                        printk ("EXT2-fs: Magic mismatch, very weird !\n");
457
                        goto failed_mount;
458
                }
459
        }
460
        if (swab32(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
461
                sb->u.ext2_sb.s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
462
                sb->u.ext2_sb.s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
463
        } else {
464
                sb->u.ext2_sb.s_inode_size = swab16(es->s_inode_size);
465
                sb->u.ext2_sb.s_first_ino = swab32(es->s_first_ino);
466
                if (sb->u.ext2_sb.s_inode_size != EXT2_GOOD_OLD_INODE_SIZE) {
467
                        printk ("EXT2-fs: unsupported inode size: %d\n",
468
                                sb->u.ext2_sb.s_inode_size);
469
                        goto failed_mount;
470
                }
471
        }
472
        sb->u.ext2_sb.s_frag_size = EXT2_MIN_FRAG_SIZE <<
473
                                   (__s32) swab32(es->s_log_frag_size);
474
        if (sb->u.ext2_sb.s_frag_size)
475
                sb->u.ext2_sb.s_frags_per_block = sb->s_blocksize /
476
                                                  sb->u.ext2_sb.s_frag_size;
477
        else
478
                sb->s_magic = 0;
479
        sb->u.ext2_sb.s_blocks_per_group = swab32(es->s_blocks_per_group);
480
        sb->u.ext2_sb.s_frags_per_group = swab32(es->s_frags_per_group);
481
        sb->u.ext2_sb.s_inodes_per_group = swab32(es->s_inodes_per_group);
482
        sb->u.ext2_sb.s_inodes_per_block = sb->s_blocksize /
483
                                           EXT2_INODE_SIZE(sb);
484
        sb->u.ext2_sb.s_itb_per_group = sb->u.ext2_sb.s_inodes_per_group /
485
                                        sb->u.ext2_sb.s_inodes_per_block;
486
        sb->u.ext2_sb.s_desc_per_block = sb->s_blocksize /
487
                                         sizeof (struct ext2_group_desc);
488
        sb->u.ext2_sb.s_sbh = bh;
489
        if (resuid != EXT2_DEF_RESUID)
490
                sb->u.ext2_sb.s_resuid = resuid;
491
        else
492
                sb->u.ext2_sb.s_resuid = swab16(es->s_def_resuid);
493
        if (resgid != EXT2_DEF_RESGID)
494
                sb->u.ext2_sb.s_resgid = resgid;
495
        else
496
                sb->u.ext2_sb.s_resgid = swab16(es->s_def_resgid);
497
        sb->u.ext2_sb.s_mount_state = swab16(es->s_state);
498
        sb->u.ext2_sb.s_rename_lock = 0;
499
        sb->u.ext2_sb.s_rename_wait = NULL;
500
        sb->u.ext2_sb.s_addr_per_block_bits =
501
                log2 (EXT2_ADDR_PER_BLOCK(sb));
502
        sb->u.ext2_sb.s_desc_per_block_bits =
503
                log2 (EXT2_DESC_PER_BLOCK(sb));
504
        if (sb->s_magic != EXT2_SUPER_MAGIC) {
505
                if (!silent)
506
                        printk ("VFS: Can't find an ext2 filesystem on dev "
507
                                "%s.\n",
508
                                kdevname(dev));
509
                goto failed_mount;
510
        }
511
        if (sb->s_blocksize != bh->b_size) {
512
                if (!silent)
513
                        printk ("VFS: Unsupported blocksize on dev "
514
                                "%s.\n", kdevname(dev));
515
                goto failed_mount;
516
        }
517
 
518
        if (sb->s_blocksize != sb->u.ext2_sb.s_frag_size) {
519
                printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
520
                        sb->u.ext2_sb.s_frag_size, sb->s_blocksize);
521
                goto failed_mount;
522
        }
523
 
524
        if (sb->u.ext2_sb.s_blocks_per_group > sb->s_blocksize * 8) {
525
                printk ("EXT2-fs: #blocks per group too big: %lu\n",
526
                        sb->u.ext2_sb.s_blocks_per_group);
527
                goto failed_mount;
528
        }
529
        if (sb->u.ext2_sb.s_frags_per_group > sb->s_blocksize * 8) {
530
                printk ("EXT2-fs: #fragments per group too big: %lu\n",
531
                        sb->u.ext2_sb.s_frags_per_group);
532
                goto failed_mount;
533
        }
534
        if (sb->u.ext2_sb.s_inodes_per_group > sb->s_blocksize * 8) {
535
                printk ("EXT2-fs: #inodes per group too big: %lu\n",
536
                        sb->u.ext2_sb.s_inodes_per_group);
537
                goto failed_mount;
538
        }
539
 
540
        sb->u.ext2_sb.s_groups_count = (swab32(es->s_blocks_count) -
541
                                        swab32(es->s_first_data_block) +
542
                                       EXT2_BLOCKS_PER_GROUP(sb) - 1) /
543
                                       EXT2_BLOCKS_PER_GROUP(sb);
544
        db_count = (sb->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
545
                   EXT2_DESC_PER_BLOCK(sb);
546
        sb->u.ext2_sb.s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
547
        if (sb->u.ext2_sb.s_group_desc == NULL) {
548
                printk ("EXT2-fs: not enough memory\n");
549
                goto failed_mount;
550
        }
551
        for (i = 0; i < db_count; i++) {
552
                sb->u.ext2_sb.s_group_desc[i] = bread (dev, logic_sb_block + i + 1,
553
                                                       sb->s_blocksize);
554
                if (!sb->u.ext2_sb.s_group_desc[i]) {
555
                        for (j = 0; j < i; j++)
556
                                brelse (sb->u.ext2_sb.s_group_desc[j]);
557
                        kfree_s (sb->u.ext2_sb.s_group_desc,
558
                                 db_count * sizeof (struct buffer_head *));
559
                        printk ("EXT2-fs: unable to read group descriptors\n");
560
                        goto failed_mount;
561
                }
562
        }
563
        if (!ext2_check_descriptors (sb)) {
564
                for (j = 0; j < db_count; j++)
565
                        brelse (sb->u.ext2_sb.s_group_desc[j]);
566
                kfree_s (sb->u.ext2_sb.s_group_desc,
567
                         db_count * sizeof (struct buffer_head *));
568
                printk ("EXT2-fs: group descriptors corrupted !\n");
569
                goto failed_mount;
570
        }
571
        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
572
                sb->u.ext2_sb.s_inode_bitmap_number[i] = 0;
573
                sb->u.ext2_sb.s_inode_bitmap[i] = NULL;
574
                sb->u.ext2_sb.s_block_bitmap_number[i] = 0;
575
                sb->u.ext2_sb.s_block_bitmap[i] = NULL;
576
        }
577
        sb->u.ext2_sb.s_loaded_inode_bitmaps = 0;
578
        sb->u.ext2_sb.s_loaded_block_bitmaps = 0;
579
        sb->u.ext2_sb.s_db_per_group = db_count;
580
        unlock_super (sb);
581
        /*
582
         * set up enough so that it can read an inode
583
         */
584
        sb->s_dev = dev;
585
        sb->s_op = &ext2_sops;
586
        if (!(sb->s_mounted = iget (sb, EXT2_ROOT_INO))) {
587
                sb->s_dev = 0;
588
                for (i = 0; i < db_count; i++)
589
                        if (sb->u.ext2_sb.s_group_desc[i])
590
                                brelse (sb->u.ext2_sb.s_group_desc[i]);
591
                kfree_s (sb->u.ext2_sb.s_group_desc,
592
                         db_count * sizeof (struct buffer_head *));
593
                brelse (bh);
594
                printk ("EXT2-fs: get root inode failed\n");
595
                MOD_DEC_USE_COUNT;
596
                return NULL;
597
        }
598
        ext2_setup_super (sb, es);
599
        return sb;
600
}
601
 
602
static void ext2_commit_super (struct super_block * sb,
603
                               struct ext2_super_block * es)
604
{
605
        es->s_wtime = swab32(CURRENT_TIME);
606
        mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
607
        sb->s_dirt = 0;
608
}
609
 
610
/*
611
 * In the second extended file system, it is not necessary to
612
 * write the super block since we use a mapping of the
613
 * disk super block in a buffer.
614
 *
615
 * However, this function is still used to set the fs valid
616
 * flags to 0.  We need to set this flag to 0 since the fs
617
 * may have been checked while mounted and e2fsck may have
618
 * set s_state to EXT2_VALID_FS after some corrections.
619
 */
620
 
621
void ext2_write_super (struct super_block * sb)
622
{
623
        struct ext2_super_block * es;
624
 
625
        if (!(sb->s_flags & MS_RDONLY)) {
626
                es = sb->u.ext2_sb.s_es;
627
 
628
                ext2_debug ("setting valid to 0\n");
629
 
630
                if (swab16(es->s_state) & EXT2_VALID_FS) {
631
                        es->s_state = swab16(swab16(es->s_state) & ~EXT2_VALID_FS);
632
                        es->s_mtime = swab32(CURRENT_TIME);
633
                }
634
                ext2_commit_super (sb, es);
635
        }
636
        sb->s_dirt = 0;
637
}
638
 
639
int ext2_remount (struct super_block * sb, int * flags, char * data)
640
{
641
        struct ext2_super_block * es;
642
        unsigned short resuid = sb->u.ext2_sb.s_resuid;
643
        unsigned short resgid = sb->u.ext2_sb.s_resgid;
644
        unsigned long new_mount_opt;
645
        unsigned long tmp;
646
 
647
        /*
648
         * Allow the "check" option to be passed as a remount option.
649
         */
650
        new_mount_opt = EXT2_MOUNT_CHECK_NORMAL;
651
        if (!parse_options (data, &tmp, &resuid, &resgid,
652
                            &new_mount_opt))
653
                return -EINVAL;
654
 
655
        sb->u.ext2_sb.s_mount_opt = new_mount_opt;
656
        sb->u.ext2_sb.s_resuid = resuid;
657
        sb->u.ext2_sb.s_resgid = resgid;
658
        es = sb->u.ext2_sb.s_es;
659
        if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
660
                return 0;
661
        if (*flags & MS_RDONLY) {
662
                if (swab16(es->s_state) & EXT2_VALID_FS ||
663
                    !(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
664
                        return 0;
665
                /*
666
                 * OK, we are remounting a valid rw partition rdonly, so set
667
                 * the rdonly flag and then mark the partition as valid again.
668
                 */
669
                es->s_state = swab16(sb->u.ext2_sb.s_mount_state);
670
                es->s_mtime = swab32(CURRENT_TIME);
671
                mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
672
                sb->s_dirt = 1;
673
                ext2_commit_super (sb, es);
674
        }
675
        else {
676
                /*
677
                 * Mounting a RDONLY partition read-write, so reread and
678
                 * store the current valid flag.  (It may have been changed
679
                 * by e2fsck since we originally mounted the partition.)
680
                 */
681
                sb->u.ext2_sb.s_mount_state = swab16(es->s_state);
682
                sb->s_flags &= ~MS_RDONLY;
683
                ext2_setup_super (sb, es);
684
        }
685
        return 0;
686
}
687
 
688
static struct file_system_type ext2_fs_type = {
689
        ext2_read_super, "ext2", 1, NULL
690
};
691
 
692
int init_ext2_fs(void)
693
{
694
        return register_filesystem(&ext2_fs_type);
695
}
696
 
697
#ifdef MODULE
698
int init_module(void)
699
{
700
        int status;
701
 
702
        if ((status = init_ext2_fs()) == 0)
703
                register_symtab(0);
704
        return status;
705
}
706
 
707
void cleanup_module(void)
708
{
709
        unregister_filesystem(&ext2_fs_type);
710
}
711
 
712
#endif
713
 
714
void ext2_statfs (struct super_block * sb, struct statfs * buf, int bufsiz)
715
{
716
        unsigned long overhead;
717
        unsigned long overhead_per_group;
718
        struct statfs tmp;
719
 
720
        if (test_opt (sb, MINIX_DF))
721
                overhead = 0;
722
        else {
723
                /*
724
                 * Compute the overhead (FS structures)
725
                 */
726
                overhead_per_group = 1 /* super block */ +
727
                                     sb->u.ext2_sb.s_db_per_group /* descriptors */ +
728
                                     1 /* block bitmap */ +
729
                                     1 /* inode bitmap */ +
730
                                     sb->u.ext2_sb.s_itb_per_group /* inode table */;
731
                overhead = swab32(sb->u.ext2_sb.s_es->s_first_data_block) +
732
                           sb->u.ext2_sb.s_groups_count * overhead_per_group;
733
        }
734
 
735
        tmp.f_type = EXT2_SUPER_MAGIC;
736
        tmp.f_bsize = sb->s_blocksize;
737
        tmp.f_blocks = swab32(sb->u.ext2_sb.s_es->s_blocks_count) - overhead;
738
        tmp.f_bfree = ext2_count_free_blocks (sb);
739
        tmp.f_bavail = tmp.f_bfree - swab32(sb->u.ext2_sb.s_es->s_r_blocks_count);
740
        if (tmp.f_bfree < swab32(sb->u.ext2_sb.s_es->s_r_blocks_count))
741
                tmp.f_bavail = 0;
742
        tmp.f_files = swab32(sb->u.ext2_sb.s_es->s_inodes_count);
743
        tmp.f_ffree = ext2_count_free_inodes (sb);
744
        tmp.f_namelen = EXT2_NAME_LEN;
745
        memcpy_tofs(buf, &tmp, bufsiz);
746
}

powered by: WebSVN 2.1.0

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