Rev 2382 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2382 | Rev 2404 | ||
---|---|---|---|
- | 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 | ||
1 | /* Superblock support. */ |
37 | /* Superblock support. */ |
2 | 38 | ||
3 | /* Methods: |
39 | /* Methods: |
4 | * init_super: initializes the superblock structure for later use |
40 | * init_super: initializes the superblock structure for later use |
5 | * get_super: return pointer at the superblock |
41 | * get_super: return pointer at the superblock |
6 | * read_super: read a superblock |
42 | * read_super: read a superblock |
7 | */ |
43 | */ |
8 | 44 | ||
9 | #include <string.h> |
45 | #include <string.h> |
10 | #include "fs.h" |
46 | #include "fs.h" |
11 | #include "block.h" |
47 | #include "block.h" |
12 | #include "inode.h" |
48 | #include "inode.h" |
13 | #include "super.h" |
49 | #include "super.h" |
14 | #include <stdio.h> |
50 | #include <stdio.h> |
15 | 51 | ||
16 | 52 | ||
17 | super_block_t* super_block; |
53 | super_block_t* super_block; |
18 | 54 | ||
19 | static int v1(int magic, int *version, int *extend); |
55 | static int v1(int magic, int *version, int *extend); |
20 | static int v2(int magic ,int*version, int *extend); |
56 | static int v2(int magic ,int*version, int *extend); |
21 | static int get_native(int magic, int *version, int *extend); |
57 | static int get_native(int magic, int *version, int *extend); |
22 | 58 | ||
23 | int (*versions[TOTAL_VERSIONS])(int , int*, int*) = { |
59 | int (*versions[TOTAL_VERSIONS])(int , int*, int*) = { |
24 | v1, /* 0 = V1 version */ |
60 | v1, /* 0 = V1 version */ |
25 | v2 /* 1 = V2 version */ |
61 | v2 /* 1 = V2 version */ |
26 | }; |
62 | }; |
27 | 63 | ||
28 | int init_super_block() |
64 | int init_super_block() |
29 | { |
65 | { |
30 | 66 | ||
31 | super_block = (super_block_t*)malloc(sizeof(super_block_t)); |
67 | super_block = (super_block_t*)malloc(sizeof(super_block_t)); |
32 | if (super_block != NULL) |
68 | if (super_block != NULL) |
33 | return TRUE; |
69 | return TRUE; |
34 | 70 | ||
35 | return FALSE; |
71 | return FALSE; |
36 | } |
72 | } |
37 | 73 | ||
38 | super_block_t *get_super() |
74 | super_block_t *get_super() |
39 | { |
75 | { |
40 | return super_block; |
76 | return super_block; |
41 | } |
77 | } |
42 | 78 | ||
43 | int read_super(register super_block_t *sp) |
79 | int read_super(register super_block_t *sp) |
44 | { |
80 | { |
45 | 81 | ||
46 | /* Read a superblock. */ |
82 | /* Read a superblock. */ |
47 | 83 | ||
48 | register block_t *bp; |
84 | register block_t *bp; |
49 | int magic, version, extend, native; |
85 | int magic, version, extend, native; |
50 | 86 | ||
51 | 87 | ||
52 | bp = get_block(SUPER_BLOCK); |
88 | bp = get_block(SUPER_BLOCK); |
53 | memcpy((void *)sp, (void *)(bp->b.b__data), (size_t)SUPER_SIZE); |
89 | memcpy((void *)sp, (void *)(bp->b.b__data), (size_t)SUPER_SIZE); |
54 | magic = sp->s_magic; /* determines file system type */ |
90 | magic = sp->s_magic; /* determines file system type */ |
55 | 91 | ||
56 | 92 | ||
57 | /* Get file system version and type. */ |
93 | /* Get file system version and type. */ |
58 | native = get_native(magic, &version, &extend); |
94 | native = get_native(magic, &version, &extend); |
59 | 95 | ||
60 | if (native == FS_EINVAL) { |
96 | if (native == FS_EINVAL) { |
61 | print_console("Unknown version of file system\n"); |
97 | print_console("Unknown version of file system\n"); |
62 | print_console("Magic is bad\n"); |
98 | print_console("Magic is bad\n"); |
63 | return FS_EINVAL; |
99 | return FS_EINVAL; |
64 | } |
100 | } |
65 | 101 | ||
66 | /* In V1, the device size was kept in a short, s_nzones, which limited |
102 | /* In V1, the device size was kept in a short, s_nzones, which limited |
67 | * devices to 32K zones. For V2, it was decided to keep the size as a |
103 | * devices to 32K zones. For V2, it was decided to keep the size as a |
68 | * long. However, just changing s_nzones to a long would not work, since |
104 | * long. However, just changing s_nzones to a long would not work, since |
69 | * then the position of s_magic in the super block would not be the same |
105 | * then the position of s_magic in the super block would not be the same |
70 | * in V1 and V2 file systems, and there would be no way to tell whether |
106 | * in V1 and V2 file systems, and there would be no way to tell whether |
71 | * a newly mounted file system was V1 or V2. The solution was to introduce |
107 | * a newly mounted file system was V1 or V2. The solution was to introduce |
72 | * a new variable, s_zones, and copy the size there. |
108 | * a new variable, s_zones, and copy the size there. |
73 | * |
109 | * |
74 | * Calculate some other numbers that depend on the version here too, to |
110 | * Calculate some other numbers that depend on the version here too, to |
75 | * hide some of the differences. |
111 | * hide some of the differences. |
76 | */ |
112 | */ |
77 | 113 | ||
78 | if (version == V1) { |
114 | if (version == V1) { |
79 | sp->s_zones = sp->s_nzones; /* only V1 needs this copy */ |
115 | sp->s_zones = sp->s_nzones; /* only V1 needs this copy */ |
80 | sp->s_inodes_per_block = V1_INODES_PER_BLOCK; |
116 | sp->s_inodes_per_block = V1_INODES_PER_BLOCK; |
81 | sp->s_ndzones = V1_NR_DZONES; |
117 | sp->s_ndzones = V1_NR_DZONES; |
82 | sp->s_nindirs = V1_INDIRECTS; |
118 | sp->s_nindirs = V1_INDIRECTS; |
83 | } |
119 | } |
84 | else { |
120 | else { |
85 | sp->s_inodes_per_block = V2_INODES_PER_BLOCK; |
121 | sp->s_inodes_per_block = V2_INODES_PER_BLOCK; |
86 | sp->s_ndzones = V2_NR_DZONES; |
122 | sp->s_ndzones = V2_NR_DZONES; |
87 | sp->s_nindirs = V2_INDIRECTS; |
123 | sp->s_nindirs = V2_INDIRECTS; |
88 | } |
124 | } |
89 | 125 | ||
90 | sp->s_isearch = 0; /* inode searches initially start at 0 */ |
126 | sp->s_isearch = 0; /* inode searches initially start at 0 */ |
91 | sp->s_zsearch = 0; /* zone searches initially start at 0 */ |
127 | sp->s_zsearch = 0; /* zone searches initially start at 0 */ |
92 | sp->s_version = version; |
128 | sp->s_version = version; |
93 | sp->s_native = native; |
129 | sp->s_native = native; |
94 | sp->s_extend = extend; |
130 | sp->s_extend = extend; |
95 | 131 | ||
96 | 132 | ||
97 | /* Make a few basic checks to see if super block looks reasonable. */ |
133 | /* Make a few basic checks to see if super block looks reasonable. */ |
98 | if (sp->s_imap_blocks < 1 || sp->s_zmap_blocks < 1 |
134 | if (sp->s_imap_blocks < 1 || sp->s_zmap_blocks < 1 |
99 | || sp->s_ninodes < 1 || sp->s_zones < 1 |
135 | || sp->s_ninodes < 1 || sp->s_zones < 1 |
100 | || (unsigned) sp->s_log_zone_size > 4) { |
136 | || (unsigned) sp->s_log_zone_size > 4) { |
101 | return FS_EINVAL; |
137 | return FS_EINVAL; |
102 | } |
138 | } |
103 | 139 | ||
104 | return OK; |
140 | return OK; |
105 | } |
141 | } |
106 | 142 | ||
107 | int v1(int magic, int* version, int *extend) |
143 | int v1(int magic, int* version, int *extend) |
108 | { |
144 | { |
109 | 145 | ||
110 | if (magic == SUPER_MAGIC || magic == conv2(BYTE_SWAP,SUPER_MAGIC)) { |
146 | if (magic == SUPER_MAGIC || magic == conv2(BYTE_SWAP,SUPER_MAGIC)) { |
111 | 147 | ||
112 | *version = V1; *extend = FALSE; |
148 | *version = V1; *extend = FALSE; |
113 | return TRUE; |
149 | return TRUE; |
114 | } |
150 | } |
115 | if (magic == SUPER_MAGIC2 || magic == conv2(BYTE_SWAP,SUPER_MAGIC2)) { |
151 | if (magic == SUPER_MAGIC2 || magic == conv2(BYTE_SWAP,SUPER_MAGIC2)) { |
116 | *version = V1; *extend = TRUE; |
152 | *version = V1; *extend = TRUE; |
117 | return TRUE; |
153 | return TRUE; |
118 | } |
154 | } |
119 | 155 | ||
120 | return FALSE; |
156 | return FALSE; |
121 | } |
157 | } |
122 | 158 | ||
123 | int v2(int magic, int *version, int *extend) |
159 | int v2(int magic, int *version, int *extend) |
124 | { |
160 | { |
125 | 161 | ||
126 | if (magic == SUPER_V2 || magic == conv2(BYTE_SWAP,SUPER_V2)) { |
162 | if (magic == SUPER_V2 || magic == conv2(BYTE_SWAP,SUPER_V2)) { |
127 | *version = V2; *extend = FALSE; |
163 | *version = V2; *extend = FALSE; |
128 | return TRUE; |
164 | return TRUE; |
129 | } |
165 | } |
130 | 166 | ||
131 | if (magic == SUPER_V2E || magic == conv2(BYTE_SWAP,SUPER_V2E)) { |
167 | if (magic == SUPER_V2E || magic == conv2(BYTE_SWAP,SUPER_V2E)) { |
132 | *version = V2; *extend = TRUE; |
168 | *version = V2; *extend = TRUE; |
133 | return TRUE; |
169 | return TRUE; |
134 | } |
170 | } |
135 | 171 | ||
136 | return FALSE; |
172 | return FALSE; |
137 | } |
173 | } |
138 | 174 | ||
139 | int get_native(int magic, int *version, int *extend) |
175 | int get_native(int magic, int *version, int *extend) |
140 | { |
176 | { |
141 | 177 | ||
142 | 178 | ||
143 | int i, result; |
179 | int i, result; |
144 | 180 | ||
145 | /* Get file system version and type */ |
181 | /* Get file system version and type */ |
146 | for (i = 0; i < TOTAL_VERSIONS; i++) { |
182 | for (i = 0; i < TOTAL_VERSIONS; i++) { |
147 | result = versions[i](magic, version, extend); |
183 | result = versions[i](magic, version, extend); |
148 | if (result) { |
184 | if (result) { |
149 | return result; |
185 | return result; |
150 | } |
186 | } |
151 | } |
187 | } |
152 | 188 | ||
153 | return FS_EINVAL; |
189 | return FS_EINVAL; |
154 | } |
190 | } |
155 | 191 |