Subversion Repositories HelenOS

Rev

Rev 2404 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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