source: trunk/minix/commands/de/de_diskio.c@ 11

Last change on this file since 11 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 8.4 KB
Line 
1/****************************************************************/
2/* */
3/* de_diskio.c */
4/* */
5/* Reading and writing to a file system device. */
6/* */
7/****************************************************************/
8/* origination 1989-Jan-15 Terrence W. Holm */
9/****************************************************************/
10
11
12#include <minix/config.h>
13#include <sys/types.h>
14#include <unistd.h>
15#include <limits.h>
16#include <string.h>
17#include <dirent.h>
18
19#include <minix/const.h>
20#include <minix/type.h>
21#include "../../servers/fs/const.h"
22#include "../../servers/fs/type.h"
23#include "../../servers/fs/super.h"
24#include "../../servers/fs/inode.h"
25#include <minix/fslib.h>
26
27#include "de.h"
28
29
30
31
32/****************************************************************/
33/* */
34/* Read_Disk( state, block_addr, buffer ) */
35/* */
36/* Reads a 1k block at "block_addr" into "buffer". */
37/* */
38/****************************************************************/
39
40
41void Read_Disk( s, block_addr, buffer )
42 de_state *s;
43 off_t block_addr;
44 char *buffer;
45
46 {
47 if ( lseek( s->device_d, block_addr, SEEK_SET ) == -1 )
48 Error( "Error seeking %s", s->device_name );
49
50 if ( read( s->device_d, buffer, s->block_size ) != s->block_size )
51 Error( "Error reading %s", s->device_name );
52 }
53
54
55
56
57
58
59/****************************************************************/
60/* */
61/* Read_Block( state, buffer ) */
62/* */
63/* Reads a 1k block from "state->address" into */
64/* "buffer". Checks "address", and updates */
65/* "block" and "offset". */
66/* */
67/****************************************************************/
68
69
70void Read_Block( s, buffer )
71 de_state *s;
72 char *buffer;
73
74 {
75 off_t end_addr;
76 off_t block_addr;
77 end_addr = (long) s->device_size * s->block_size - 1;
78
79 if ( s->address < 0 )
80 s->address = 0L;
81
82 if ( s->address > end_addr )
83 s->address = end_addr;
84
85 /* The address must be rounded off for */
86 /* certain visual display modes. */
87
88 if ( s->mode == WORD )
89 s->address &= ~1L;
90 else if ( s->mode == MAP )
91 s->address &= ~3L;
92
93
94 block_addr = s->address & K_MASK;
95
96 s->block = (zone_t) (block_addr >> K_SHIFT);
97 s->offset = (unsigned) (s->address - block_addr);
98
99 Read_Disk( s, block_addr, buffer );
100 }
101
102
103
104
105
106
107/****************************************************************/
108/* */
109/* Read_Super_Block( state ) */
110/* */
111/* Read and check the super block. */
112/* */
113/****************************************************************/
114
115
116void Read_Super_Block( s )
117 de_state *s;
118
119 {
120 struct super_block *super = (struct super_block *) s->buffer;
121 unsigned inodes_per_block;
122 off_t size;
123
124 s->block_size = K;
125 Read_Disk( s, (long) SUPER_BLOCK_BYTES, s->buffer );
126
127 s->magic = super->s_magic;
128 if ( s->magic == SUPER_MAGIC )
129 {
130 s->is_fs = TRUE;
131 s->v1 = TRUE;
132 s->inode_size = V1_INODE_SIZE;
133 inodes_per_block = V1_INODES_PER_BLOCK;
134 s->nr_indirects = V1_INDIRECTS;
135 s->zone_num_size = V1_ZONE_NUM_SIZE;
136 s->zones = super->s_nzones;
137 s->ndzones = V1_NR_DZONES;
138 s->block_size = _STATIC_BLOCK_SIZE;
139 }
140 else if ( s->magic == SUPER_V2 || s->magic == SUPER_V3)
141 {
142 if(s->magic == SUPER_V3)
143 s->block_size = super->s_block_size;
144 else
145 s->block_size = _STATIC_BLOCK_SIZE;
146 s->is_fs = TRUE;
147 s->v1 = FALSE;
148 s->inode_size = V2_INODE_SIZE;
149 inodes_per_block = V2_INODES_PER_BLOCK(s->block_size);
150 s->nr_indirects = V2_INDIRECTS(s->block_size);
151 s->zone_num_size = V2_ZONE_NUM_SIZE;
152 s->zones = super->s_zones;
153 s->ndzones = V2_NR_DZONES;
154 }
155 else
156 {
157 if ( super->s_magic == SUPER_REV )
158 Warning( "V1-bytes-swapped file system (?)" );
159 else if ( super->s_magic == SUPER_V2_REV )
160 Warning( "V2-bytes-swapped file system (?)" );
161 else
162 Warning( "Not a Minix file system" );
163 Warning( "The file system features will not be available" );
164 s->zones = 100000L;
165 return;
166 }
167
168 s->inodes = super->s_ninodes;
169 s->inode_maps = bitmapsize( (bit_t) s->inodes + 1 , s->block_size);
170 if ( s->inode_maps != super->s_imap_blocks )
171 {
172 if ( s->inode_maps > super->s_imap_blocks )
173 Error( "Corrupted inode map count or inode count in super block" );
174 else
175 Warning( "Count of inode map blocks in super block suspiciously high" );
176 s->inode_maps = super->s_imap_blocks;
177 }
178
179 s->zone_maps = bitmapsize( (bit_t) s->zones , s->block_size);
180 if ( s->zone_maps != super->s_zmap_blocks )
181 {
182 if ( s->zone_maps > super->s_zmap_blocks )
183 Error( "Corrupted zone map count or zone count in super block" );
184 else
185 Warning( "Count of zone map blocks in super block suspiciously high" );
186 s->zone_maps = super->s_zmap_blocks;
187 }
188
189 s->inode_blocks = (s->inodes + inodes_per_block - 1) / inodes_per_block;
190 s->first_data = 2 + s->inode_maps + s->zone_maps + s->inode_blocks;
191 if ( s->first_data != super->s_firstdatazone )
192 {
193 if ( s->first_data > super->s_firstdatazone )
194 Error( "Corrupted first data zone offset or inode count in super block" );
195 else
196 Warning( "First data zone in super block suspiciously high" );
197 s->first_data = super->s_firstdatazone;
198 }
199
200 s->inodes_in_map = s->inodes + 1;
201 s->zones_in_map = s->zones + 1 - s->first_data;
202
203 /*
204 if ( s->zones != s->device_size )
205 Warning( "Zone count does not equal device size" );
206 */
207
208 s->device_size = s->zones;
209
210 if ( super->s_log_zone_size != 0 )
211 Error( "Can not handle multiple blocks per zone" );
212 }
213
214
215
216
217
218
219/****************************************************************/
220/* */
221/* Read_Bit_Maps( state ) */
222/* */
223/* Read in the i-node and zone bit maps from the */
224/* specified file system device. */
225/* */
226/****************************************************************/
227
228
229void Read_Bit_Maps( s )
230 de_state *s;
231
232 {
233 int i;
234
235 if ( s->inode_maps > I_MAP_SLOTS || s->zone_maps > Z_MAP_SLOTS )
236 {
237 Warning( "Super block specifies too many bit map blocks" );
238 return;
239 }
240
241 for ( i = 0; i < s->inode_maps; ++i )
242 {
243 Read_Disk( s, (long) (2 + i) * K,
244 (char *) &s->inode_map[ i * K / sizeof (bitchunk_t ) ] );
245 }
246
247 for ( i = 0; i < s->zone_maps; ++i )
248 {
249 Read_Disk( s, (long) (2 + s->inode_maps + i) * K,
250 (char *) &s->zone_map[ i * K / sizeof (bitchunk_t ) ] );
251 }
252 }
253
254
255
256
257
258
259/****************************************************************/
260/* */
261/* Search( state, string ) */
262/* */
263/* Search from the current address for the ASCII */
264/* "string" on the device. */
265/* */
266/****************************************************************/
267
268
269off_t Search( s, string )
270 de_state *s;
271 char *string;
272
273 {
274 off_t address = s->address + 1;
275 off_t last_addr = address;
276 char buffer[ SEARCH_BUFFER ];
277 int offset;
278 int tail_length = strlen( string ) - 1;
279 int count = SEARCH_BUFFER;
280 int last_offset;
281
282
283 for ( ; count == SEARCH_BUFFER; address += SEARCH_BUFFER - tail_length )
284 {
285 if ( lseek( s->device_d, address, SEEK_SET ) == -1 )
286 Error( "Error seeking %s", s->device_name );
287
288 if ( (count = read( s->device_d, buffer, SEARCH_BUFFER)) == -1 )
289 Error( "Error reading %s", s->device_name );
290
291
292 if ( address - last_addr >= 500L * K )
293 {
294 putchar( '.' );
295 fflush( stdout );
296
297 last_addr += 500L * K;
298 }
299
300
301 last_offset = count - tail_length;
302
303 for ( offset = 0; offset < last_offset; ++offset )
304 {
305 register char c = buffer[ offset ];
306
307 if ( c == *string )
308 {
309 char *tail_buffer = &buffer[ offset + 1 ];
310 char *tail_string = string + 1;
311
312 do
313 {
314 if ( *tail_string == '\0' )
315 return( address + offset );
316 }
317 while ( *tail_buffer++ == *tail_string++ );
318 }
319 } /* end for ( offset ) */
320 } /* end for ( address ) */
321
322 return( -1L );
323 }
324
325
326
327
328
329
330/****************************************************************/
331/* */
332/* Write_Word( state, word ) */
333/* */
334/* Write a word at address. */
335/* */
336/****************************************************************/
337
338
339void Write_Word( s, word )
340 de_state *s;
341 word_t word;
342
343 {
344 if ( s->address & 01 )
345 Error( "Internal fault (unaligned address)" );
346
347 if ( lseek( s->device_d, s->address, SEEK_SET ) == -1 )
348 Error( "Error seeking %s", s->device_name );
349
350 if ( write( s->device_d, (char *) &word, sizeof word ) != sizeof word )
351 Error( "Error writing %s", s->device_name );
352 }
Note: See TracBrowser for help on using the repository browser.