Subversion Repositories HelenOS

Rev

Rev 2382 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2404 konopa 1
/*
2
 * Copyright (c) 1987,1997, Prentice Hall
3
 * All rights reserved.
4
 *
5
 * Redistribution and use of the MINIX operating system in source and
6
 * binary forms, with or without modification, are permitted provided
7
 * that the following conditions are met:
8
 
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 
12
 * - Redistributions in binary form must reproduce the above
13
 *   copyright notice, this list of conditions and the following
14
 *   disclaimer in the documentation and/or other materials provided
15
 *   with the distribution.
16
 
17
 * - Neither the name of Prentice Hall nor the names of the software
18
 *   authors or contributors may be used to endorse or promote
19
 *   products derived from this software without specific prior
20
 *   written permission.
21
 
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
23
 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26
 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
27
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
 */
35
 
36
 
2382 konopa 37
/* Superblock support. */
38
 
39
/* Methods:
40
 * init_super:  initializes the superblock structure for later use
41
 * get_super:   return pointer at the superblock
42
 * read_super:  read a superblock
43
 */
44
 
45
#include <string.h>
46
#include "fs.h"
47
#include "block.h"
48
#include "inode.h"
49
#include "super.h"
50
#include <stdio.h>
51
 
52
 
53
super_block_t* super_block;
54
 
55
static int v1(int magic, int *version, int *extend);
56
static int v2(int magic ,int*version, int *extend);
57
static int get_native(int magic, int *version, int *extend);
58
 
59
int (*versions[TOTAL_VERSIONS])(int , int*, int*)  = {
60
        v1, /* 0 = V1 version */
61
        v2  /* 1 = V2 version */
62
    };
63
 
64
int init_super_block()
65
{
66
 
67
    super_block = (super_block_t*)malloc(sizeof(super_block_t));
68
    if (super_block != NULL)
69
        return TRUE;
70
 
71
        return FALSE;  
72
}
73
 
74
super_block_t *get_super()
75
{  
76
    return super_block;
77
}
78
 
79
int read_super(register super_block_t *sp)
80
{
81
 
82
    /* Read a superblock. */
83
 
84
    register block_t *bp;
85
    int magic, version, extend, native;
86
 
87
 
88
    bp = get_block(SUPER_BLOCK);
89
    memcpy((void *)sp, (void *)(bp->b.b__data), (size_t)SUPER_SIZE);
90
    magic = sp->s_magic;        /* determines file system type */
91
 
92
 
93
    /* Get file system version and type. */
94
    native = get_native(magic, &version, &extend);
95
 
96
    if (native == FS_EINVAL) {
97
        print_console("Unknown version of file system\n");
98
        print_console("Magic is bad\n");
99
        return FS_EINVAL;  
100
    }
101
 
102
    /* In V1, the device size was kept in a short, s_nzones, which limited
103
       * devices to 32K zones.  For V2, it was decided to keep the size as a
104
       * long.  However, just changing s_nzones to a long would not work, since
105
       * then the position of s_magic in the super block would not be the same
106
       * in V1 and V2 file systems, and there would be no way to tell whether
107
       * a newly mounted file system was V1 or V2.  The solution was to introduce
108
       * a new variable, s_zones, and copy the size there.
109
       *
110
       * Calculate some other numbers that depend on the version here too, to
111
       * hide some of the differences.
112
       */
113
 
114
    if (version == V1) {
115
        sp->s_zones = sp->s_nzones;     /* only V1 needs this copy */
116
        sp->s_inodes_per_block = V1_INODES_PER_BLOCK;
117
        sp->s_ndzones = V1_NR_DZONES;
118
        sp->s_nindirs = V1_INDIRECTS;
119
    }
120
    else {
121
        sp->s_inodes_per_block = V2_INODES_PER_BLOCK;
122
        sp->s_ndzones = V2_NR_DZONES;
123
        sp->s_nindirs = V2_INDIRECTS;
124
    }
125
 
126
    sp->s_isearch = 0;  /* inode searches initially start at 0 */
127
    sp->s_zsearch = 0;  /* zone searches initially start at 0 */
128
    sp->s_version = version;
129
    sp->s_native  = native;    
130
    sp->s_extend = extend;
131
 
132
 
133
    /* Make a few basic checks to see if super block looks reasonable. */
134
    if (sp->s_imap_blocks < 1 || sp->s_zmap_blocks < 1
135
        || sp->s_ninodes < 1 || sp->s_zones < 1
136
        || (unsigned) sp->s_log_zone_size > 4) {
137
        return FS_EINVAL;
138
    }  
139
 
140
    return OK;
141
}
142
 
143
int v1(int magic, int* version, int *extend)
144
{
145
 
146
    if (magic == SUPER_MAGIC || magic == conv2(BYTE_SWAP,SUPER_MAGIC)) {
147
 
148
        *version = V1; *extend = FALSE;
149
        return TRUE;
150
    }
151
    if (magic == SUPER_MAGIC2 || magic == conv2(BYTE_SWAP,SUPER_MAGIC2)) {
152
        *version = V1; *extend = TRUE;
153
        return TRUE;
154
    }
155
 
156
    return FALSE;
157
}
158
 
159
int v2(int magic, int *version, int *extend)
160
{
161
 
162
    if (magic == SUPER_V2 || magic == conv2(BYTE_SWAP,SUPER_V2)) {
163
        *version = V2; *extend = FALSE;
164
        return TRUE;
165
    }
166
 
167
    if (magic == SUPER_V2E || magic == conv2(BYTE_SWAP,SUPER_V2E)) {
168
        *version = V2; *extend = TRUE;
169
        return TRUE;
170
    }
171
 
172
    return FALSE;
173
}
174
 
175
int get_native(int magic, int *version, int *extend)
176
{
177
 
178
 
179
    int i, result;
180
 
181
    /* Get file system version and type */
182
    for (i = 0; i < TOTAL_VERSIONS; i++) {
183
        result = versions[i](magic, version, extend);
184
        if (result) {
185
            return result;
186
        }
187
    }
188
 
189
    return FS_EINVAL;
190
}