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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [fs/] [ext2/] [super.c] - Blame information for rev 1275

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

Line No. Rev Author Line
1 1275 phoenix
/*
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/config.h>
20
#include <linux/module.h>
21
#include <linux/string.h>
22
#include <linux/fs.h>
23
#include <linux/ext2_fs.h>
24
#include <linux/slab.h>
25
#include <linux/init.h>
26
#include <linux/locks.h>
27
#include <linux/blkdev.h>
28
#include <asm/uaccess.h>
29
 
30
 
31
static void ext2_sync_super(struct super_block *sb,
32
                            struct ext2_super_block *es);
33
 
34
static char error_buf[1024];
35
 
36
void ext2_error (struct super_block * sb, const char * function,
37
                 const char * fmt, ...)
38
{
39
        va_list args;
40
        struct ext2_super_block *es = EXT2_SB(sb)->s_es;
41
 
42
        if (!(sb->s_flags & MS_RDONLY)) {
43
                sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
44
                es->s_state =
45
                        cpu_to_le16(le16_to_cpu(es->s_state) | EXT2_ERROR_FS);
46
                ext2_sync_super(sb, es);
47
        }
48
        va_start (args, fmt);
49
        vsprintf (error_buf, fmt, args);
50
        va_end (args);
51
        if (test_opt (sb, ERRORS_PANIC) ||
52
            (le16_to_cpu(sb->u.ext2_sb.s_es->s_errors) == EXT2_ERRORS_PANIC &&
53
             !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_RO)))
54
                panic ("EXT2-fs panic (device %s): %s: %s\n",
55
                       bdevname(sb->s_dev), function, error_buf);
56
        printk (KERN_CRIT "EXT2-fs error (device %s): %s: %s\n",
57
                bdevname(sb->s_dev), function, error_buf);
58
        if (test_opt (sb, ERRORS_RO) ||
59
            (le16_to_cpu(sb->u.ext2_sb.s_es->s_errors) == EXT2_ERRORS_RO &&
60
             !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_PANIC))) {
61
                printk ("Remounting filesystem read-only\n");
62
                sb->s_flags |= MS_RDONLY;
63
        }
64
}
65
 
66
NORET_TYPE void ext2_panic (struct super_block * sb, const char * function,
67
                            const char * fmt, ...)
68
{
69
        va_list args;
70
 
71
        if (!(sb->s_flags & MS_RDONLY)) {
72
                sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
73
                sb->u.ext2_sb.s_es->s_state =
74
                        cpu_to_le16(le16_to_cpu(sb->u.ext2_sb.s_es->s_state) | EXT2_ERROR_FS);
75
                mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
76
                sb->s_dirt = 1;
77
        }
78
        va_start (args, fmt);
79
        vsprintf (error_buf, fmt, args);
80
        va_end (args);
81
        sb->s_flags |= MS_RDONLY;
82
        panic ("EXT2-fs panic (device %s): %s: %s\n",
83
               bdevname(sb->s_dev), function, error_buf);
84
}
85
 
86
void ext2_warning (struct super_block * sb, const char * function,
87
                   const char * fmt, ...)
88
{
89
        va_list args;
90
 
91
        va_start (args, fmt);
92
        vsprintf (error_buf, fmt, args);
93
        va_end (args);
94
        printk (KERN_WARNING "EXT2-fs warning (device %s): %s: %s\n",
95
                bdevname(sb->s_dev), function, error_buf);
96
}
97
 
98
void ext2_update_dynamic_rev(struct super_block *sb)
99
{
100
        struct ext2_super_block *es = EXT2_SB(sb)->s_es;
101
 
102
        if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
103
                return;
104
 
105
        ext2_warning(sb, __FUNCTION__,
106
                     "updating to rev %d because of new feature flag, "
107
                     "running e2fsck is recommended",
108
                     EXT2_DYNAMIC_REV);
109
 
110
        es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
111
        es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
112
        es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
113
        /* leave es->s_feature_*compat flags alone */
114
        /* es->s_uuid will be set by e2fsck if empty */
115
 
116
        /*
117
         * The rest of the superblock fields should be zero, and if not it
118
         * means they are likely already in use, so leave them alone.  We
119
         * can leave it up to e2fsck to clean up any inconsistencies there.
120
         */
121
}
122
 
123
void ext2_put_super (struct super_block * sb)
124
{
125
        int db_count;
126
        int i;
127
 
128
        if (!(sb->s_flags & MS_RDONLY)) {
129
                struct ext2_super_block *es = EXT2_SB(sb)->s_es;
130
 
131
                es->s_state = le16_to_cpu(EXT2_SB(sb)->s_mount_state);
132
                ext2_sync_super(sb, es);
133
        }
134
        db_count = EXT2_SB(sb)->s_gdb_count;
135
        for (i = 0; i < db_count; i++)
136
                if (sb->u.ext2_sb.s_group_desc[i])
137
                        brelse (sb->u.ext2_sb.s_group_desc[i]);
138
        kfree(sb->u.ext2_sb.s_group_desc);
139
        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
140
                if (sb->u.ext2_sb.s_inode_bitmap[i])
141
                        brelse (sb->u.ext2_sb.s_inode_bitmap[i]);
142
        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
143
                if (sb->u.ext2_sb.s_block_bitmap[i])
144
                        brelse (sb->u.ext2_sb.s_block_bitmap[i]);
145
        brelse (sb->u.ext2_sb.s_sbh);
146
 
147
        return;
148
}
149
 
150
static struct super_operations ext2_sops = {
151
        read_inode:     ext2_read_inode,
152
        write_inode:    ext2_write_inode,
153
        put_inode:      ext2_put_inode,
154
        delete_inode:   ext2_delete_inode,
155
        put_super:      ext2_put_super,
156
        write_super:    ext2_write_super,
157
        statfs:         ext2_statfs,
158
        remount_fs:     ext2_remount,
159
};
160
 
161
/*
162
 * This function has been shamelessly adapted from the msdos fs
163
 */
164
static int parse_options (char * options, unsigned long * sb_block,
165
                          unsigned short *resuid, unsigned short * resgid,
166
                          unsigned long * mount_options)
167
{
168
        char * this_char;
169
        char * value;
170
 
171
        if (!options)
172
                return 1;
173
        for (this_char = strtok (options, ",");
174
             this_char != NULL;
175
             this_char = strtok (NULL, ",")) {
176
                if ((value = strchr (this_char, '=')) != NULL)
177
                        *value++ = 0;
178
                if (!strcmp (this_char, "bsddf"))
179
                        clear_opt (*mount_options, MINIX_DF);
180
                else if (!strcmp (this_char, "nouid32")) {
181
                        set_opt (*mount_options, NO_UID32);
182
                }
183
                else if (!strcmp (this_char, "check")) {
184
                        if (!value || !*value || !strcmp (value, "none"))
185
                                clear_opt (*mount_options, CHECK);
186
                        else
187
#ifdef CONFIG_EXT2_CHECK
188
                                set_opt (*mount_options, CHECK);
189
#else
190
                                printk("EXT2 Check option not supported\n");
191
#endif
192
                }
193
                else if (!strcmp (this_char, "debug"))
194
                        set_opt (*mount_options, DEBUG);
195
                else if (!strcmp (this_char, "errors")) {
196
                        if (!value || !*value) {
197
                                printk ("EXT2-fs: the errors option requires "
198
                                        "an argument\n");
199
                                return 0;
200
                        }
201
                        if (!strcmp (value, "continue")) {
202
                                clear_opt (*mount_options, ERRORS_RO);
203
                                clear_opt (*mount_options, ERRORS_PANIC);
204
                                set_opt (*mount_options, ERRORS_CONT);
205
                        }
206
                        else if (!strcmp (value, "remount-ro")) {
207
                                clear_opt (*mount_options, ERRORS_CONT);
208
                                clear_opt (*mount_options, ERRORS_PANIC);
209
                                set_opt (*mount_options, ERRORS_RO);
210
                        }
211
                        else if (!strcmp (value, "panic")) {
212
                                clear_opt (*mount_options, ERRORS_CONT);
213
                                clear_opt (*mount_options, ERRORS_RO);
214
                                set_opt (*mount_options, ERRORS_PANIC);
215
                        }
216
                        else {
217
                                printk ("EXT2-fs: Invalid errors option: %s\n",
218
                                        value);
219
                                return 0;
220
                        }
221
                }
222
                else if (!strcmp (this_char, "grpid") ||
223
                         !strcmp (this_char, "bsdgroups"))
224
                        set_opt (*mount_options, GRPID);
225
                else if (!strcmp (this_char, "minixdf"))
226
                        set_opt (*mount_options, MINIX_DF);
227
                else if (!strcmp (this_char, "nocheck"))
228
                        clear_opt (*mount_options, CHECK);
229
                else if (!strcmp (this_char, "nogrpid") ||
230
                         !strcmp (this_char, "sysvgroups"))
231
                        clear_opt (*mount_options, GRPID);
232
                else if (!strcmp (this_char, "resgid")) {
233
                        if (!value || !*value) {
234
                                printk ("EXT2-fs: the resgid option requires "
235
                                        "an argument\n");
236
                                return 0;
237
                        }
238
                        *resgid = simple_strtoul (value, &value, 0);
239
                        if (*value) {
240
                                printk ("EXT2-fs: Invalid resgid option: %s\n",
241
                                        value);
242
                                return 0;
243
                        }
244
                }
245
                else if (!strcmp (this_char, "resuid")) {
246
                        if (!value || !*value) {
247
                                printk ("EXT2-fs: the resuid option requires "
248
                                        "an argument");
249
                                return 0;
250
                        }
251
                        *resuid = simple_strtoul (value, &value, 0);
252
                        if (*value) {
253
                                printk ("EXT2-fs: Invalid resuid option: %s\n",
254
                                        value);
255
                                return 0;
256
                        }
257
                }
258
                else if (!strcmp (this_char, "sb")) {
259
                        if (!value || !*value) {
260
                                printk ("EXT2-fs: the sb option requires "
261
                                        "an argument");
262
                                return 0;
263
                        }
264
                        *sb_block = simple_strtoul (value, &value, 0);
265
                        if (*value) {
266
                                printk ("EXT2-fs: Invalid sb option: %s\n",
267
                                        value);
268
                                return 0;
269
                        }
270
                }
271
                /* Silently ignore the quota options */
272
                else if (!strcmp (this_char, "grpquota")
273
                         || !strcmp (this_char, "noquota")
274
                         || !strcmp (this_char, "quota")
275
                         || !strcmp (this_char, "usrquota"))
276
                        /* Don't do anything ;-) */ ;
277
                else {
278
                        printk ("EXT2-fs: Unrecognized mount option %s\n", this_char);
279
                        return 0;
280
                }
281
        }
282
        return 1;
283
}
284
 
285
static int ext2_setup_super (struct super_block * sb,
286
                              struct ext2_super_block * es,
287
                              int read_only)
288
{
289
        int res = 0;
290
        if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
291
                printk ("EXT2-fs warning: revision level too high, "
292
                        "forcing read-only mode\n");
293
                res = MS_RDONLY;
294
        }
295
        if (read_only)
296
                return res;
297
        if (!(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
298
                printk ("EXT2-fs warning: mounting unchecked fs, "
299
                        "running e2fsck is recommended\n");
300
        else if ((sb->u.ext2_sb.s_mount_state & EXT2_ERROR_FS))
301
                printk ("EXT2-fs warning: mounting fs with errors, "
302
                        "running e2fsck is recommended\n");
303
        else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
304
                 le16_to_cpu(es->s_mnt_count) >=
305
                 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
306
                printk ("EXT2-fs warning: maximal mount count reached, "
307
                        "running e2fsck is recommended\n");
308
        else if (le32_to_cpu(es->s_checkinterval) &&
309
                (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= CURRENT_TIME))
310
                printk ("EXT2-fs warning: checktime reached, "
311
                        "running e2fsck is recommended\n");
312
        if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
313
                es->s_max_mnt_count = (__s16) cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
314
        es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
315
        ext2_write_super(sb);
316
        if (test_opt (sb, DEBUG))
317
                printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
318
                        "bpg=%lu, ipg=%lu, mo=%04lx]\n",
319
                        EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
320
                        sb->u.ext2_sb.s_frag_size,
321
                        sb->u.ext2_sb.s_groups_count,
322
                        EXT2_BLOCKS_PER_GROUP(sb),
323
                        EXT2_INODES_PER_GROUP(sb),
324
                        sb->u.ext2_sb.s_mount_opt);
325
#ifdef CONFIG_EXT2_CHECK
326
        if (test_opt (sb, CHECK)) {
327
                ext2_check_blocks_bitmap (sb);
328
                ext2_check_inodes_bitmap (sb);
329
        }
330
#endif
331
        return res;
332
}
333
 
334
static int ext2_check_descriptors (struct super_block * sb)
335
{
336
        int i;
337
        int desc_block = 0;
338
        unsigned long block = le32_to_cpu(sb->u.ext2_sb.s_es->s_first_data_block);
339
        struct ext2_group_desc * gdp = NULL;
340
 
341
        ext2_debug ("Checking group descriptors");
342
 
343
        for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++)
344
        {
345
                if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
346
                        gdp = (struct ext2_group_desc *) sb->u.ext2_sb.s_group_desc[desc_block++]->b_data;
347
                if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
348
                    le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
349
                {
350
                        ext2_error (sb, "ext2_check_descriptors",
351
                                    "Block bitmap for group %d"
352
                                    " not in group (block %lu)!",
353
                                    i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
354
                        return 0;
355
                }
356
                if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
357
                    le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
358
                {
359
                        ext2_error (sb, "ext2_check_descriptors",
360
                                    "Inode bitmap for group %d"
361
                                    " not in group (block %lu)!",
362
                                    i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
363
                        return 0;
364
                }
365
                if (le32_to_cpu(gdp->bg_inode_table) < block ||
366
                    le32_to_cpu(gdp->bg_inode_table) + sb->u.ext2_sb.s_itb_per_group >=
367
                    block + EXT2_BLOCKS_PER_GROUP(sb))
368
                {
369
                        ext2_error (sb, "ext2_check_descriptors",
370
                                    "Inode table for group %d"
371
                                    " not in group (block %lu)!",
372
                                    i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
373
                        return 0;
374
                }
375
                block += EXT2_BLOCKS_PER_GROUP(sb);
376
                gdp++;
377
        }
378
        return 1;
379
}
380
 
381
#define log2(n) ffz(~(n))
382
 
383
/*
384
 * Maximal file size.  There is a direct, and {,double-,triple-}indirect
385
 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
386
 * We need to be 1 filesystem block less than the 2^32 sector limit.
387
 */
388
static loff_t ext2_max_size(int bits)
389
{
390
        loff_t res = EXT2_NDIR_BLOCKS;
391
        res += 1LL << (bits-2);
392
        res += 1LL << (2*(bits-2));
393
        res += 1LL << (3*(bits-2));
394
        res <<= bits;
395
        if (res > (512LL << 32) - (1 << bits))
396
                res = (512LL << 32) - (1 << bits);
397
        return res;
398
}
399
 
400
static unsigned long descriptor_loc(struct super_block *sb,
401
                                    unsigned long logic_sb_block,
402
                                    int nr)
403
{
404
        struct ext2_sb_info *sbi = EXT2_SB(sb);
405
        unsigned long bg, first_data_block, first_meta_bg;
406
        int has_super = 0;
407
 
408
        first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
409
        first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
410
 
411
        if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
412
            nr < first_meta_bg)
413
                return (logic_sb_block + nr + 1);
414
        bg = sbi->s_desc_per_block * nr;
415
        if (ext2_bg_has_super(sb, bg))
416
                has_super = 1;
417
        return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
418
}
419
 
420
struct super_block * ext2_read_super (struct super_block * sb, void * data,
421
                                      int silent)
422
{
423
        struct buffer_head * bh;
424
        struct ext2_sb_info * sbi = EXT2_SB(sb);
425
        struct ext2_super_block * es;
426
        unsigned long sb_block = 1;
427
        unsigned short resuid = EXT2_DEF_RESUID;
428
        unsigned short resgid = EXT2_DEF_RESGID;
429
        unsigned long block;
430
        unsigned long logic_sb_block = 1;
431
        unsigned long offset = 0;
432
        kdev_t dev = sb->s_dev;
433
        int blocksize = BLOCK_SIZE;
434
        int db_count;
435
        int i, j;
436
 
437
        /*
438
         * See what the current blocksize for the device is, and
439
         * use that as the blocksize.  Otherwise (or if the blocksize
440
         * is smaller than the default) use the default.
441
         * This is important for devices that have a hardware
442
         * sectorsize that is larger than the default.
443
         */
444
        blocksize = get_hardsect_size(dev);
445
        if(blocksize < BLOCK_SIZE )
446
            blocksize = BLOCK_SIZE;
447
 
448
        sb->u.ext2_sb.s_mount_opt = 0;
449
        if (!parse_options ((char *) data, &sb_block, &resuid, &resgid,
450
            &sb->u.ext2_sb.s_mount_opt)) {
451
                return NULL;
452
        }
453
 
454
        if (set_blocksize(dev, blocksize) < 0) {
455
                printk ("EXT2-fs: unable to set blocksize %d\n", blocksize);
456
                return NULL;
457
        }
458
        sb->s_blocksize = blocksize;
459
 
460
        /*
461
         * If the superblock doesn't start on a sector boundary,
462
         * calculate the offset.  FIXME(eric) this doesn't make sense
463
         * that we would have to do this.
464
         */
465
        if (blocksize != BLOCK_SIZE) {
466
                logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
467
                offset = (sb_block*BLOCK_SIZE) % blocksize;
468
        }
469
 
470
        if (!(bh = sb_bread(sb, logic_sb_block))) {
471
                printk ("EXT2-fs: unable to read superblock\n");
472
                return NULL;
473
        }
474
        /*
475
         * Note: s_es must be initialized as soon as possible because
476
         *       some ext2 macro-instructions depend on its value
477
         */
478
        es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
479
        sb->u.ext2_sb.s_es = es;
480
        sb->s_magic = le16_to_cpu(es->s_magic);
481
        if (sb->s_magic != EXT2_SUPER_MAGIC) {
482
                if (!silent)
483
                        printk ("VFS: Can't find ext2 filesystem on dev %s.\n",
484
                                bdevname(dev));
485
                goto failed_mount;
486
        }
487
        if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
488
            (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
489
             EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
490
             EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
491
                printk("EXT2-fs warning: feature flags set on rev 0 fs, "
492
                       "running e2fsck is recommended\n");
493
        /*
494
         * Check feature flags regardless of the revision level, since we
495
         * previously didn't change the revision level when setting the flags,
496
         * so there is a chance incompat flags are set on a rev 0 filesystem.
497
         */
498
        if ((i = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))) {
499
                printk("EXT2-fs: %s: couldn't mount because of "
500
                       "unsupported optional features (%x).\n",
501
                       bdevname(dev), i);
502
                goto failed_mount;
503
        }
504
        if (!(sb->s_flags & MS_RDONLY) &&
505
            (i = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
506
                printk("EXT2-fs: %s: couldn't mount RDWR because of "
507
                       "unsupported optional features (%x).\n",
508
                       bdevname(dev), i);
509
                goto failed_mount;
510
        }
511
        if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
512
                ext2_warning(sb, __FUNCTION__,
513
                        "mounting ext3 filesystem as ext2\n");
514
        sb->s_blocksize_bits =
515
                le32_to_cpu(EXT2_SB(sb)->s_es->s_log_block_size) + 10;
516
        sb->s_blocksize = 1 << sb->s_blocksize_bits;
517
 
518
        sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
519
 
520
        /* If the blocksize doesn't match, re-read the thing.. */
521
        if (sb->s_blocksize != blocksize) {
522
                blocksize = sb->s_blocksize;
523
                brelse(bh);
524
 
525
                if (set_blocksize(dev, blocksize) < 0) {
526
                        printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
527
                        return NULL;
528
                }
529
 
530
                logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
531
                offset = (sb_block*BLOCK_SIZE) % blocksize;
532
                bh = sb_bread(sb, logic_sb_block);
533
                if(!bh) {
534
                        printk("EXT2-fs: Couldn't read superblock on "
535
                               "2nd try.\n");
536
                        goto failed_mount;
537
                }
538
                es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
539
                sb->u.ext2_sb.s_es = es;
540
                if (es->s_magic != le16_to_cpu(EXT2_SUPER_MAGIC)) {
541
                        printk ("EXT2-fs: Magic mismatch, very weird !\n");
542
                        goto failed_mount;
543
                }
544
        }
545
 
546
        if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
547
                sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
548
                sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
549
        } else {
550
                sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
551
                sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
552
                if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
553
                    (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
554
                    (sbi->s_inode_size > blocksize)) {
555
                        printk ("EXT2-fs: unsupported inode size: %d\n",
556
                                sbi->s_inode_size);
557
                        goto failed_mount;
558
                }
559
        }
560
        sb->u.ext2_sb.s_frag_size = EXT2_MIN_FRAG_SIZE <<
561
                                   le32_to_cpu(es->s_log_frag_size);
562
        if (sb->u.ext2_sb.s_frag_size)
563
                sb->u.ext2_sb.s_frags_per_block = sb->s_blocksize /
564
                                                  sb->u.ext2_sb.s_frag_size;
565
        else
566
                sb->s_magic = 0;
567
        sb->u.ext2_sb.s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
568
        sb->u.ext2_sb.s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
569
        sb->u.ext2_sb.s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
570
        sb->u.ext2_sb.s_inodes_per_block = sb->s_blocksize /
571
                                           EXT2_INODE_SIZE(sb);
572
        sb->u.ext2_sb.s_itb_per_group = sb->u.ext2_sb.s_inodes_per_group /
573
                                        sb->u.ext2_sb.s_inodes_per_block;
574
        sb->u.ext2_sb.s_desc_per_block = sb->s_blocksize /
575
                                         sizeof (struct ext2_group_desc);
576
        sb->u.ext2_sb.s_sbh = bh;
577
        if (resuid != EXT2_DEF_RESUID)
578
                sb->u.ext2_sb.s_resuid = resuid;
579
        else
580
                sb->u.ext2_sb.s_resuid = le16_to_cpu(es->s_def_resuid);
581
        if (resgid != EXT2_DEF_RESGID)
582
                sb->u.ext2_sb.s_resgid = resgid;
583
        else
584
                sb->u.ext2_sb.s_resgid = le16_to_cpu(es->s_def_resgid);
585
        sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
586
        sb->u.ext2_sb.s_addr_per_block_bits =
587
                log2 (EXT2_ADDR_PER_BLOCK(sb));
588
        sb->u.ext2_sb.s_desc_per_block_bits =
589
                log2 (EXT2_DESC_PER_BLOCK(sb));
590
        if (sb->s_magic != EXT2_SUPER_MAGIC) {
591
                if (!silent)
592
                        printk ("VFS: Can't find an ext2 filesystem on dev "
593
                                "%s.\n",
594
                                bdevname(dev));
595
                goto failed_mount;
596
        }
597
        if (sb->s_blocksize != bh->b_size) {
598
                if (!silent)
599
                        printk ("VFS: Unsupported blocksize on dev "
600
                                "%s.\n", bdevname(dev));
601
                goto failed_mount;
602
        }
603
 
604
        if (sb->s_blocksize != sb->u.ext2_sb.s_frag_size) {
605
                printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
606
                        sb->u.ext2_sb.s_frag_size, sb->s_blocksize);
607
                goto failed_mount;
608
        }
609
 
610
        if (sb->u.ext2_sb.s_blocks_per_group > sb->s_blocksize * 8) {
611
                printk ("EXT2-fs: #blocks per group too big: %lu\n",
612
                        sb->u.ext2_sb.s_blocks_per_group);
613
                goto failed_mount;
614
        }
615
        if (sb->u.ext2_sb.s_frags_per_group > sb->s_blocksize * 8) {
616
                printk ("EXT2-fs: #fragments per group too big: %lu\n",
617
                        sb->u.ext2_sb.s_frags_per_group);
618
                goto failed_mount;
619
        }
620
        if (sb->u.ext2_sb.s_inodes_per_group > sb->s_blocksize * 8) {
621
                printk ("EXT2-fs: #inodes per group too big: %lu\n",
622
                        sb->u.ext2_sb.s_inodes_per_group);
623
                goto failed_mount;
624
        }
625
 
626
        sb->u.ext2_sb.s_groups_count = (le32_to_cpu(es->s_blocks_count) -
627
                                        le32_to_cpu(es->s_first_data_block) +
628
                                       EXT2_BLOCKS_PER_GROUP(sb) - 1) /
629
                                       EXT2_BLOCKS_PER_GROUP(sb);
630
        db_count = (sb->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
631
                   EXT2_DESC_PER_BLOCK(sb);
632
        sb->u.ext2_sb.s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
633
        if (sb->u.ext2_sb.s_group_desc == NULL) {
634
                printk ("EXT2-fs: not enough memory\n");
635
                goto failed_mount;
636
        }
637
        for (i = 0; i < db_count; i++) {
638
                block = descriptor_loc(sb, logic_sb_block, i);
639
                sbi->s_group_desc[i] = sb_bread(sb, block);
640
                if (!sbi->s_group_desc[i]) {
641
                        for (j = 0; j < i; j++)
642
                                brelse (sbi->s_group_desc[j]);
643
                        kfree(sbi->s_group_desc);
644
                        printk ("EXT2-fs: unable to read group descriptors\n");
645
                        goto failed_mount;
646
                }
647
        }
648
        if (!ext2_check_descriptors (sb)) {
649
                printk ("EXT2-fs: group descriptors corrupted!\n");
650
                db_count = i;
651
                goto failed_mount2;
652
        }
653
        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
654
                sb->u.ext2_sb.s_inode_bitmap_number[i] = 0;
655
                sb->u.ext2_sb.s_inode_bitmap[i] = NULL;
656
                sb->u.ext2_sb.s_block_bitmap_number[i] = 0;
657
                sb->u.ext2_sb.s_block_bitmap[i] = NULL;
658
        }
659
        sb->u.ext2_sb.s_loaded_inode_bitmaps = 0;
660
        sb->u.ext2_sb.s_loaded_block_bitmaps = 0;
661
        sb->u.ext2_sb.s_gdb_count = db_count;
662
        /*
663
         * set up enough so that it can read an inode
664
         */
665
        sb->s_op = &ext2_sops;
666
        sb->s_root = d_alloc_root(iget(sb, EXT2_ROOT_INO));
667
        if (!sb->s_root || !S_ISDIR(sb->s_root->d_inode->i_mode) ||
668
            !sb->s_root->d_inode->i_blocks || !sb->s_root->d_inode->i_size) {
669
                if (sb->s_root) {
670
                        dput(sb->s_root);
671
                        sb->s_root = NULL;
672
                        printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
673
                } else
674
                        printk(KERN_ERR "EXT2-fs: get root inode failed\n");
675
                goto failed_mount2;
676
        }
677
        ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
678
        return sb;
679
failed_mount2:
680
        for (i = 0; i < db_count; i++)
681
                brelse(sb->u.ext2_sb.s_group_desc[i]);
682
        kfree(sb->u.ext2_sb.s_group_desc);
683
failed_mount:
684
        brelse(bh);
685
        return NULL;
686
}
687
 
688
static void ext2_commit_super (struct super_block * sb,
689
                               struct ext2_super_block * es)
690
{
691
        es->s_wtime = cpu_to_le32(CURRENT_TIME);
692
        mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
693
        sb->s_dirt = 0;
694
}
695
 
696
static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
697
{
698
        es->s_wtime = cpu_to_le32(CURRENT_TIME);
699
        mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
700
        ll_rw_block(WRITE, 1, &EXT2_SB(sb)->s_sbh);
701
        wait_on_buffer(EXT2_SB(sb)->s_sbh);
702
        sb->s_dirt = 0;
703
}
704
 
705
/*
706
 * In the second extended file system, it is not necessary to
707
 * write the super block since we use a mapping of the
708
 * disk super block in a buffer.
709
 *
710
 * However, this function is still used to set the fs valid
711
 * flags to 0.  We need to set this flag to 0 since the fs
712
 * may have been checked while mounted and e2fsck may have
713
 * set s_state to EXT2_VALID_FS after some corrections.
714
 */
715
 
716
void ext2_write_super (struct super_block * sb)
717
{
718
        struct ext2_super_block * es;
719
 
720
        if (!(sb->s_flags & MS_RDONLY)) {
721
                es = sb->u.ext2_sb.s_es;
722
 
723
                if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
724
                        ext2_debug ("setting valid to 0\n");
725
                        es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
726
                                                  ~EXT2_VALID_FS);
727
                        es->s_mtime = cpu_to_le32(CURRENT_TIME);
728
                        ext2_sync_super(sb, es);
729
                } else
730
                        ext2_commit_super (sb, es);
731
        }
732
        sb->s_dirt = 0;
733
}
734
 
735
int ext2_remount (struct super_block * sb, int * flags, char * data)
736
{
737
        struct ext2_super_block * es;
738
        unsigned short resuid = sb->u.ext2_sb.s_resuid;
739
        unsigned short resgid = sb->u.ext2_sb.s_resgid;
740
        unsigned long new_mount_opt;
741
        unsigned long tmp;
742
 
743
        /*
744
         * Allow the "check" option to be passed as a remount option.
745
         */
746
        new_mount_opt = sb->u.ext2_sb.s_mount_opt;
747
        if (!parse_options (data, &tmp, &resuid, &resgid,
748
                            &new_mount_opt))
749
                return -EINVAL;
750
 
751
        sb->u.ext2_sb.s_mount_opt = new_mount_opt;
752
        sb->u.ext2_sb.s_resuid = resuid;
753
        sb->u.ext2_sb.s_resgid = resgid;
754
        es = sb->u.ext2_sb.s_es;
755
        if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
756
                return 0;
757
        if (*flags & MS_RDONLY) {
758
                if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
759
                    !(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
760
                        return 0;
761
                /*
762
                 * OK, we are remounting a valid rw partition rdonly, so set
763
                 * the rdonly flag and then mark the partition as valid again.
764
                 */
765
                es->s_state = cpu_to_le16(sb->u.ext2_sb.s_mount_state);
766
                es->s_mtime = cpu_to_le32(CURRENT_TIME);
767
        } else {
768
                int ret;
769
                if ((ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
770
                                               ~EXT2_FEATURE_RO_COMPAT_SUPP))) {
771
                        printk("EXT2-fs: %s: couldn't remount RDWR because of "
772
                               "unsupported optional features (%x).\n",
773
                               bdevname(sb->s_dev), ret);
774
                        return -EROFS;
775
                }
776
                /*
777
                 * Mounting a RDONLY partition read-write, so reread and
778
                 * store the current valid flag.  (It may have been changed
779
                 * by e2fsck since we originally mounted the partition.)
780
                 */
781
                sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
782
                if (!ext2_setup_super (sb, es, 0))
783
                        sb->s_flags &= ~MS_RDONLY;
784
        }
785
        ext2_sync_super(sb, es);
786
        return 0;
787
}
788
 
789
int ext2_statfs (struct super_block * sb, struct statfs * buf)
790
{
791
        unsigned long overhead;
792
        int i;
793
 
794
        if (test_opt (sb, MINIX_DF))
795
                overhead = 0;
796
        else {
797
                /*
798
                 * Compute the overhead (FS structures)
799
                 */
800
 
801
                /*
802
                 * All of the blocks before first_data_block are
803
                 * overhead
804
                 */
805
                overhead = le32_to_cpu(sb->u.ext2_sb.s_es->s_first_data_block);
806
 
807
                /*
808
                 * Add the overhead attributed to the superblock and
809
                 * block group descriptors.  If the sparse superblocks
810
                 * feature is turned on, then not all groups have this.
811
                 */
812
                for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++)
813
                        overhead += ext2_bg_has_super(sb, i) +
814
                                ext2_bg_num_gdb(sb, i);
815
 
816
                /*
817
                 * Every block group has an inode bitmap, a block
818
                 * bitmap, and an inode table.
819
                 */
820
                overhead += (sb->u.ext2_sb.s_groups_count *
821
                             (2 + sb->u.ext2_sb.s_itb_per_group));
822
        }
823
 
824
        buf->f_type = EXT2_SUPER_MAGIC;
825
        buf->f_bsize = sb->s_blocksize;
826
        buf->f_blocks = le32_to_cpu(sb->u.ext2_sb.s_es->s_blocks_count) - overhead;
827
        buf->f_bfree = ext2_count_free_blocks (sb);
828
        buf->f_bavail = buf->f_bfree - le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count);
829
        if (buf->f_bfree < le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count))
830
                buf->f_bavail = 0;
831
        buf->f_files = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
832
        buf->f_ffree = ext2_count_free_inodes (sb);
833
        buf->f_namelen = EXT2_NAME_LEN;
834
        return 0;
835
}
836
 
837
static DECLARE_FSTYPE_DEV(ext2_fs_type, "ext2", ext2_read_super);
838
 
839
static int __init init_ext2_fs(void)
840
{
841
        return register_filesystem(&ext2_fs_type);
842
}
843
 
844
static void __exit exit_ext2_fs(void)
845
{
846
        unregister_filesystem(&ext2_fs_type);
847
}
848
 
849
EXPORT_NO_SYMBOLS;
850
 
851
module_init(init_ext2_fs)
852
module_exit(exit_ext2_fs)

powered by: WebSVN 2.1.0

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