diff options
author | Eric Andersen <andersen@codepoet.org> | 1999-10-19 20:23:03 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 1999-10-19 20:23:03 +0000 |
commit | e494fdd2c73f7f2979f7e02d65e46a50bd86a59f (patch) | |
tree | 421436a8d83b6974088dd1e97d53139406616085 | |
parent | e77ae3a2c0328590b43447550bdb1284650b8236 (diff) | |
download | busybox-w32-e494fdd2c73f7f2979f7e02d65e46a50bd86a59f.tar.gz busybox-w32-e494fdd2c73f7f2979f7e02d65e46a50bd86a59f.tar.bz2 busybox-w32-e494fdd2c73f7f2979f7e02d65e46a50bd86a59f.zip |
More stuff
-rw-r--r-- | applets/busybox.c | 6 | ||||
-rw-r--r-- | busybox.c | 6 | ||||
-rw-r--r-- | busybox.def.h | 2 | ||||
-rw-r--r-- | fsck_minix.c | 1377 | ||||
-rw-r--r-- | internal.h | 2 | ||||
-rw-r--r-- | mkfs_minix.c | 755 | ||||
-rw-r--r-- | util-linux/fsck_minix.c | 1377 | ||||
-rw-r--r-- | util-linux/mkfs_minix.c | 755 |
8 files changed, 4280 insertions, 0 deletions
diff --git a/applets/busybox.c b/applets/busybox.c index 6bcb65297..4849ba0a4 100644 --- a/applets/busybox.c +++ b/applets/busybox.c | |||
@@ -51,6 +51,12 @@ static const struct Applet applets[] = { | |||
51 | #ifdef BB_FIND //usr/bin | 51 | #ifdef BB_FIND //usr/bin |
52 | {"find", find_main}, | 52 | {"find", find_main}, |
53 | #endif | 53 | #endif |
54 | #ifdef BB_FSCK_MINIX //sbin | ||
55 | {"fsck.minix", fsck_minix_main}, | ||
56 | #endif | ||
57 | #ifdef BB_MKFS_MINIX //sbin | ||
58 | {"mkfs.minix", mkfs_minix_main}, | ||
59 | #endif | ||
54 | #ifdef BB_GREP //bin | 60 | #ifdef BB_GREP //bin |
55 | {"grep", grep_main}, | 61 | {"grep", grep_main}, |
56 | #endif | 62 | #endif |
@@ -51,6 +51,12 @@ static const struct Applet applets[] = { | |||
51 | #ifdef BB_FIND //usr/bin | 51 | #ifdef BB_FIND //usr/bin |
52 | {"find", find_main}, | 52 | {"find", find_main}, |
53 | #endif | 53 | #endif |
54 | #ifdef BB_FSCK_MINIX //sbin | ||
55 | {"fsck.minix", fsck_minix_main}, | ||
56 | #endif | ||
57 | #ifdef BB_MKFS_MINIX //sbin | ||
58 | {"mkfs.minix", mkfs_minix_main}, | ||
59 | #endif | ||
54 | #ifdef BB_GREP //bin | 60 | #ifdef BB_GREP //bin |
55 | {"grep", grep_main}, | 61 | {"grep", grep_main}, |
56 | #endif | 62 | #endif |
diff --git a/busybox.def.h b/busybox.def.h index c90297aa2..39bde4ddd 100644 --- a/busybox.def.h +++ b/busybox.def.h | |||
@@ -16,6 +16,8 @@ | |||
16 | //#define BB_DUTMP | 16 | //#define BB_DUTMP |
17 | #define BB_FDFLUSH | 17 | #define BB_FDFLUSH |
18 | #define BB_FIND | 18 | #define BB_FIND |
19 | #define BB_FSCK_MINIX | ||
20 | #define BB_MKFS_MINIX | ||
19 | #define BB_GREP | 21 | #define BB_GREP |
20 | #define BB_HALT | 22 | #define BB_HALT |
21 | #define BB_INIT | 23 | #define BB_INIT |
diff --git a/fsck_minix.c b/fsck_minix.c new file mode 100644 index 000000000..e4b14d705 --- /dev/null +++ b/fsck_minix.c | |||
@@ -0,0 +1,1377 @@ | |||
1 | /* | ||
2 | * fsck.c - a file system consistency checker for Linux. | ||
3 | * | ||
4 | * (C) 1991, 1992 Linus Torvalds. This file may be redistributed | ||
5 | * as per the GNU copyleft. | ||
6 | */ | ||
7 | |||
8 | /* | ||
9 | * 09.11.91 - made the first rudimetary functions | ||
10 | * | ||
11 | * 10.11.91 - updated, does checking, no repairs yet. | ||
12 | * Sent out to the mailing-list for testing. | ||
13 | * | ||
14 | * 14.11.91 - Testing seems to have gone well. Added some | ||
15 | * correction-code, and changed some functions. | ||
16 | * | ||
17 | * 15.11.91 - More correction code. Hopefully it notices most | ||
18 | * cases now, and tries to do something about them. | ||
19 | * | ||
20 | * 16.11.91 - More corrections (thanks to Mika Jalava). Most | ||
21 | * things seem to work now. Yeah, sure. | ||
22 | * | ||
23 | * | ||
24 | * 19.04.92 - Had to start over again from this old version, as a | ||
25 | * kernel bug ate my enhanced fsck in february. | ||
26 | * | ||
27 | * 28.02.93 - added support for different directory entry sizes.. | ||
28 | * | ||
29 | * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with | ||
30 | * super-block information | ||
31 | * | ||
32 | * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform | ||
33 | * to that required by fsutil | ||
34 | * | ||
35 | * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu) | ||
36 | * Added support for file system valid flag. Also | ||
37 | * added program_version variable and output of | ||
38 | * program name and version number when program | ||
39 | * is executed. | ||
40 | * | ||
41 | * 30.10.94 - added support for v2 filesystem | ||
42 | * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de) | ||
43 | * | ||
44 | * 10.12.94 - added test to prevent checking of mounted fs adapted | ||
45 | * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck | ||
46 | * program. (Daniel Quinlan, quinlan@yggdrasil.com) | ||
47 | * | ||
48 | * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such | ||
49 | * for modern libcs (janl@math.uio.no, Nicolai Langfeldt) | ||
50 | * | ||
51 | * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk | ||
52 | * (Russell King). He made them for ARM. It would seem | ||
53 | * that the ARM is powerful enough to do this in C whereas | ||
54 | * i386 and m64k must use assembly to get it fast >:-) | ||
55 | * This should make minix fsck systemindependent. | ||
56 | * (janl@math.uio.no, Nicolai Langfeldt) | ||
57 | * | ||
58 | * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler | ||
59 | * warnings. Added mc68k bitops from | ||
60 | * Joerg Dorchain <dorchain@mpi-sb.mpg.de>. | ||
61 | * | ||
62 | * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by | ||
63 | * Andreas Schwab. | ||
64 | * | ||
65 | * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org> | ||
66 | * - added Native Language Support | ||
67 | * | ||
68 | * | ||
69 | * I've had no time to add comments - hopefully the function names | ||
70 | * are comments enough. As with all file system checkers, this assumes | ||
71 | * the file system is quiescent - don't use it on a mounted device | ||
72 | * unless you can be sure nobody is writing to it (and remember that the | ||
73 | * kernel can write to it when it searches for files). | ||
74 | * | ||
75 | * Usuage: fsck [-larvsm] device | ||
76 | * -l for a listing of all the filenames | ||
77 | * -a for automatic repairs (not implemented) | ||
78 | * -r for repairs (interactive) (not implemented) | ||
79 | * -v for verbose (tells how many files) | ||
80 | * -s for super-block info | ||
81 | * -m for minix-like "mode not cleared" warnings | ||
82 | * -f force filesystem check even if filesystem marked as valid | ||
83 | * | ||
84 | * The device may be a block device or a image of one, but this isn't | ||
85 | * enforced (but it's not much fun on a character device :-). | ||
86 | */ | ||
87 | |||
88 | #include <stdio.h> | ||
89 | #include <errno.h> | ||
90 | #include <unistd.h> | ||
91 | #include <string.h> | ||
92 | #include <fcntl.h> | ||
93 | #include <ctype.h> | ||
94 | #include <stdlib.h> | ||
95 | #include <termios.h> | ||
96 | #include <mntent.h> | ||
97 | #include <sys/stat.h> | ||
98 | |||
99 | #include <linux/fs.h> | ||
100 | #include <linux/minix_fs.h> | ||
101 | #include "../version.h" | ||
102 | #include "nls.h" | ||
103 | |||
104 | #ifdef MINIX2_SUPER_MAGIC2 | ||
105 | #define HAVE_MINIX2 1 | ||
106 | #endif | ||
107 | |||
108 | #ifndef __linux__ | ||
109 | #define volatile | ||
110 | #endif | ||
111 | |||
112 | #define ROOT_INO 1 | ||
113 | |||
114 | #define UPPER(size,n) ((size+((n)-1))/(n)) | ||
115 | #define INODE_SIZE (sizeof(struct minix_inode)) | ||
116 | #ifdef HAVE_MINIX2 | ||
117 | #define INODE_SIZE2 (sizeof(struct minix2_inode)) | ||
118 | #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \ | ||
119 | : MINIX_INODES_PER_BLOCK)) | ||
120 | #else | ||
121 | #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK)) | ||
122 | #endif | ||
123 | #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE) | ||
124 | |||
125 | #define BITS_PER_BLOCK (BLOCK_SIZE<<3) | ||
126 | |||
127 | static char * program_name = "fsck.minix"; | ||
128 | static char * program_version = "1.2 - 11/11/96"; | ||
129 | static char * device_name = NULL; | ||
130 | static int IN; | ||
131 | static int repair=0, automatic=0, verbose=0, list=0, show=0, warn_mode=0, | ||
132 | force=0; | ||
133 | static int directory=0, regular=0, blockdev=0, chardev=0, links=0, | ||
134 | symlinks=0, total=0; | ||
135 | |||
136 | static int changed = 0; /* flags if the filesystem has been changed */ | ||
137 | static int errors_uncorrected = 0; /* flag if some error was not corrected */ | ||
138 | static int dirsize = 16; | ||
139 | static int namelen = 14; | ||
140 | static int version2 = 0; | ||
141 | static struct termios termios; | ||
142 | static int termios_set = 0; | ||
143 | |||
144 | /* File-name data */ | ||
145 | #define MAX_DEPTH 50 | ||
146 | static int name_depth = 0; | ||
147 | static char name_list[MAX_DEPTH][NAME_MAX+1]; | ||
148 | |||
149 | static char * inode_buffer = NULL; | ||
150 | #define Inode (((struct minix_inode *) inode_buffer)-1) | ||
151 | #define Inode2 (((struct minix2_inode *) inode_buffer)-1) | ||
152 | static char super_block_buffer[BLOCK_SIZE]; | ||
153 | #define Super (*(struct minix_super_block *)super_block_buffer) | ||
154 | #define INODES ((unsigned long)Super.s_ninodes) | ||
155 | #ifdef HAVE_MINIX2 | ||
156 | #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones)) | ||
157 | #else | ||
158 | #define ZONES ((unsigned long)(Super.s_nzones)) | ||
159 | #endif | ||
160 | #define IMAPS ((unsigned long)Super.s_imap_blocks) | ||
161 | #define ZMAPS ((unsigned long)Super.s_zmap_blocks) | ||
162 | #define FIRSTZONE ((unsigned long)Super.s_firstdatazone) | ||
163 | #define ZONESIZE ((unsigned long)Super.s_log_zone_size) | ||
164 | #define MAXSIZE ((unsigned long)Super.s_max_size) | ||
165 | #define MAGIC (Super.s_magic) | ||
166 | #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS) | ||
167 | |||
168 | static char *inode_map; | ||
169 | static char *zone_map; | ||
170 | |||
171 | static unsigned char * inode_count = NULL; | ||
172 | static unsigned char * zone_count = NULL; | ||
173 | |||
174 | void recursive_check(unsigned int ino); | ||
175 | void recursive_check2(unsigned int ino); | ||
176 | |||
177 | #include "bitops.h" | ||
178 | |||
179 | #define inode_in_use(x) (bit(inode_map,(x))) | ||
180 | #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1)) | ||
181 | |||
182 | #define mark_inode(x) (setbit(inode_map,(x)),changed=1) | ||
183 | #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1) | ||
184 | |||
185 | #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1) | ||
186 | #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1) | ||
187 | |||
188 | void leave(int) __attribute__ ((noreturn)); | ||
189 | void leave(int status) | ||
190 | { | ||
191 | if (termios_set) | ||
192 | tcsetattr(0, TCSANOW, &termios); | ||
193 | exit(status); | ||
194 | } | ||
195 | |||
196 | void usage(void) { | ||
197 | fprintf(stderr, | ||
198 | _("Usage: %s [-larvsmf] /dev/name\n"), | ||
199 | program_name); | ||
200 | leave(16); | ||
201 | } | ||
202 | |||
203 | void die(const char *str) { | ||
204 | fprintf(stderr, "%s: %s\n", program_name, str); | ||
205 | leave(8); | ||
206 | } | ||
207 | |||
208 | /* | ||
209 | * This simply goes through the file-name data and prints out the | ||
210 | * current file. | ||
211 | */ | ||
212 | void print_current_name(void) | ||
213 | { | ||
214 | int i=0; | ||
215 | |||
216 | while (i<name_depth) | ||
217 | printf("/%.*s",namelen,name_list[i++]); | ||
218 | if (i == 0) | ||
219 | printf ("/"); | ||
220 | } | ||
221 | |||
222 | int ask(const char * string, int def) | ||
223 | { | ||
224 | int c; | ||
225 | |||
226 | if (!repair) { | ||
227 | printf("\n"); | ||
228 | errors_uncorrected = 1; | ||
229 | return 0; | ||
230 | } | ||
231 | if (automatic) { | ||
232 | printf("\n"); | ||
233 | if (!def) | ||
234 | errors_uncorrected = 1; | ||
235 | return def; | ||
236 | } | ||
237 | printf(def?"%s (y/n)? ":"%s (n/y)? ",string); | ||
238 | for (;;) { | ||
239 | fflush(stdout); | ||
240 | if ((c=getchar())==EOF) { | ||
241 | if (!def) | ||
242 | errors_uncorrected = 1; | ||
243 | return def; | ||
244 | } | ||
245 | c=toupper(c); | ||
246 | if (c == 'Y') { | ||
247 | def = 1; | ||
248 | break; | ||
249 | } else if (c == 'N') { | ||
250 | def = 0; | ||
251 | break; | ||
252 | } else if (c == ' ' || c == '\n') | ||
253 | break; | ||
254 | } | ||
255 | if (def) | ||
256 | printf("y\n"); | ||
257 | else { | ||
258 | printf("n\n"); | ||
259 | errors_uncorrected = 1; | ||
260 | } | ||
261 | return def; | ||
262 | } | ||
263 | |||
264 | /* | ||
265 | * Make certain that we aren't checking a filesystem that is on a | ||
266 | * mounted partition. Code adapted from e2fsck, Copyright (C) 1993, | ||
267 | * 1994 Theodore Ts'o. Also licensed under GPL. | ||
268 | */ | ||
269 | static void check_mount(void) | ||
270 | { | ||
271 | FILE * f; | ||
272 | struct mntent * mnt; | ||
273 | int cont; | ||
274 | int fd; | ||
275 | |||
276 | if ((f = setmntent (MOUNTED, "r")) == NULL) | ||
277 | return; | ||
278 | while ((mnt = getmntent (f)) != NULL) | ||
279 | if (strcmp (device_name, mnt->mnt_fsname) == 0) | ||
280 | break; | ||
281 | endmntent (f); | ||
282 | if (!mnt) | ||
283 | return; | ||
284 | |||
285 | /* | ||
286 | * If the root is mounted read-only, then /etc/mtab is | ||
287 | * probably not correct; so we won't issue a warning based on | ||
288 | * it. | ||
289 | */ | ||
290 | fd = open(MOUNTED, O_RDWR); | ||
291 | if (fd < 0 && errno == EROFS) | ||
292 | return; | ||
293 | else | ||
294 | close(fd); | ||
295 | |||
296 | printf (_("%s is mounted. "), device_name); | ||
297 | if (isatty(0) && isatty(1)) | ||
298 | cont = ask(_("Do you really want to continue"), 0); | ||
299 | else | ||
300 | cont = 0; | ||
301 | if (!cont) { | ||
302 | printf (_("check aborted.\n")); | ||
303 | exit (0); | ||
304 | } | ||
305 | return; | ||
306 | } | ||
307 | |||
308 | /* | ||
309 | * check_zone_nr checks to see that *nr is a valid zone nr. If it | ||
310 | * isn't, it will possibly be repaired. Check_zone_nr sets *corrected | ||
311 | * if an error was corrected, and returns the zone (0 for no zone | ||
312 | * or a bad zone-number). | ||
313 | */ | ||
314 | int check_zone_nr(unsigned short * nr, int * corrected) | ||
315 | { | ||
316 | if (!*nr) | ||
317 | return 0; | ||
318 | if (*nr < FIRSTZONE) | ||
319 | printf(_("Zone nr < FIRSTZONE in file `")); | ||
320 | else if (*nr >= ZONES) | ||
321 | printf(_("Zone nr >= ZONES in file `")); | ||
322 | else | ||
323 | return *nr; | ||
324 | print_current_name(); | ||
325 | printf("'."); | ||
326 | if (ask(_("Remove block"),1)) { | ||
327 | *nr = 0; | ||
328 | *corrected = 1; | ||
329 | } | ||
330 | return 0; | ||
331 | } | ||
332 | |||
333 | #ifdef HAVE_MINIX2 | ||
334 | int check_zone_nr2 (unsigned int *nr, int *corrected) | ||
335 | { | ||
336 | if (!*nr) | ||
337 | return 0; | ||
338 | if (*nr < FIRSTZONE) | ||
339 | printf (_("Zone nr < FIRSTZONE in file `")); | ||
340 | else if (*nr >= ZONES) | ||
341 | printf (_("Zone nr >= ZONES in file `")); | ||
342 | else | ||
343 | return *nr; | ||
344 | print_current_name (); | ||
345 | printf ("'."); | ||
346 | if (ask (_("Remove block"), 1)) { | ||
347 | *nr = 0; | ||
348 | *corrected = 1; | ||
349 | } | ||
350 | return 0; | ||
351 | } | ||
352 | #endif | ||
353 | |||
354 | /* | ||
355 | * read-block reads block nr into the buffer at addr. | ||
356 | */ | ||
357 | void read_block(unsigned int nr, char * addr) | ||
358 | { | ||
359 | if (!nr) { | ||
360 | memset(addr,0,BLOCK_SIZE); | ||
361 | return; | ||
362 | } | ||
363 | if (BLOCK_SIZE*nr != lseek(IN, BLOCK_SIZE*nr, SEEK_SET)) { | ||
364 | printf(_("Read error: unable to seek to block in file '")); | ||
365 | print_current_name(); | ||
366 | printf("'\n"); | ||
367 | memset(addr,0,BLOCK_SIZE); | ||
368 | errors_uncorrected = 1; | ||
369 | } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) { | ||
370 | printf(_("Read error: bad block in file '")); | ||
371 | print_current_name(); | ||
372 | printf("'\n"); | ||
373 | memset(addr,0,BLOCK_SIZE); | ||
374 | errors_uncorrected = 1; | ||
375 | } | ||
376 | } | ||
377 | |||
378 | /* | ||
379 | * write_block writes block nr to disk. | ||
380 | */ | ||
381 | void write_block(unsigned int nr, char * addr) | ||
382 | { | ||
383 | if (!nr) | ||
384 | return; | ||
385 | if (nr < FIRSTZONE || nr >= ZONES) { | ||
386 | printf(_("Internal error: trying to write bad block\n" | ||
387 | "Write request ignored\n")); | ||
388 | errors_uncorrected = 1; | ||
389 | return; | ||
390 | } | ||
391 | if (BLOCK_SIZE*nr != lseek(IN, BLOCK_SIZE*nr, SEEK_SET)) | ||
392 | die(_("seek failed in write_block")); | ||
393 | if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) { | ||
394 | printf(_("Write error: bad block in file '")); | ||
395 | print_current_name(); | ||
396 | printf("'\n"); | ||
397 | errors_uncorrected = 1; | ||
398 | } | ||
399 | } | ||
400 | |||
401 | /* | ||
402 | * map-block calculates the absolute block nr of a block in a file. | ||
403 | * It sets 'changed' if the inode has needed changing, and re-writes | ||
404 | * any indirect blocks with errors. | ||
405 | */ | ||
406 | int map_block(struct minix_inode * inode, unsigned int blknr) | ||
407 | { | ||
408 | unsigned short ind[BLOCK_SIZE>>1]; | ||
409 | unsigned short dind[BLOCK_SIZE>>1]; | ||
410 | int blk_chg, block, result; | ||
411 | |||
412 | if (blknr<7) | ||
413 | return check_zone_nr(inode->i_zone + blknr, &changed); | ||
414 | blknr -= 7; | ||
415 | if (blknr<512) { | ||
416 | block = check_zone_nr(inode->i_zone + 7, &changed); | ||
417 | read_block(block, (char *) ind); | ||
418 | blk_chg = 0; | ||
419 | result = check_zone_nr(blknr + ind, &blk_chg); | ||
420 | if (blk_chg) | ||
421 | write_block(block, (char *) ind); | ||
422 | return result; | ||
423 | } | ||
424 | blknr -= 512; | ||
425 | block = check_zone_nr(inode->i_zone + 8, &changed); | ||
426 | read_block(block, (char *) dind); | ||
427 | blk_chg = 0; | ||
428 | result = check_zone_nr(dind + (blknr/512), &blk_chg); | ||
429 | if (blk_chg) | ||
430 | write_block(block, (char *) dind); | ||
431 | block = result; | ||
432 | read_block(block, (char *) ind); | ||
433 | blk_chg = 0; | ||
434 | result = check_zone_nr(ind + (blknr%512), &blk_chg); | ||
435 | if (blk_chg) | ||
436 | write_block(block, (char *) ind); | ||
437 | return result; | ||
438 | } | ||
439 | |||
440 | #ifdef HAVE_MINIX2 | ||
441 | int map_block2 (struct minix2_inode *inode, unsigned int blknr) | ||
442 | { | ||
443 | unsigned int ind[BLOCK_SIZE >> 2]; | ||
444 | unsigned int dind[BLOCK_SIZE >> 2]; | ||
445 | unsigned int tind[BLOCK_SIZE >> 2]; | ||
446 | int blk_chg, block, result; | ||
447 | |||
448 | if (blknr < 7) | ||
449 | return check_zone_nr2 (inode->i_zone + blknr, &changed); | ||
450 | blknr -= 7; | ||
451 | if (blknr < 256) { | ||
452 | block = check_zone_nr2 (inode->i_zone + 7, &changed); | ||
453 | read_block (block, (char *) ind); | ||
454 | blk_chg = 0; | ||
455 | result = check_zone_nr2 (blknr + ind, &blk_chg); | ||
456 | if (blk_chg) | ||
457 | write_block (block, (char *) ind); | ||
458 | return result; | ||
459 | } | ||
460 | blknr -= 256; | ||
461 | if (blknr >= 256 * 256) { | ||
462 | block = check_zone_nr2 (inode->i_zone + 8, &changed); | ||
463 | read_block (block, (char *) dind); | ||
464 | blk_chg = 0; | ||
465 | result = check_zone_nr2 (dind + blknr / 256, &blk_chg); | ||
466 | if (blk_chg) | ||
467 | write_block (block, (char *) dind); | ||
468 | block = result; | ||
469 | read_block (block, (char *) ind); | ||
470 | blk_chg = 0; | ||
471 | result = check_zone_nr2 (ind + blknr % 256, &blk_chg); | ||
472 | if (blk_chg) | ||
473 | write_block (block, (char *) ind); | ||
474 | return result; | ||
475 | } | ||
476 | blknr -= 256 * 256; | ||
477 | block = check_zone_nr2 (inode->i_zone + 9, &changed); | ||
478 | read_block (block, (char *) tind); | ||
479 | blk_chg = 0; | ||
480 | result = check_zone_nr2 (tind + blknr / (256 * 256), &blk_chg); | ||
481 | if (blk_chg) | ||
482 | write_block (block, (char *) tind); | ||
483 | block = result; | ||
484 | read_block (block, (char *) dind); | ||
485 | blk_chg = 0; | ||
486 | result = check_zone_nr2 (dind + (blknr / 256) % 256, &blk_chg); | ||
487 | if (blk_chg) | ||
488 | write_block (block, (char *) dind); | ||
489 | block = result; | ||
490 | read_block (block, (char *) ind); | ||
491 | blk_chg = 0; | ||
492 | result = check_zone_nr2 (ind + blknr % 256, &blk_chg); | ||
493 | if (blk_chg) | ||
494 | write_block (block, (char *) ind); | ||
495 | return result; | ||
496 | } | ||
497 | #endif | ||
498 | |||
499 | void write_super_block(void) | ||
500 | { | ||
501 | /* | ||
502 | * Set the state of the filesystem based on whether or not there | ||
503 | * are uncorrected errors. The filesystem valid flag is | ||
504 | * unconditionally set if we get this far. | ||
505 | */ | ||
506 | Super.s_state |= MINIX_VALID_FS; | ||
507 | if ( errors_uncorrected ) | ||
508 | Super.s_state |= MINIX_ERROR_FS; | ||
509 | else | ||
510 | Super.s_state &= ~MINIX_ERROR_FS; | ||
511 | |||
512 | if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET)) | ||
513 | die(_("seek failed in write_super_block")); | ||
514 | if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE)) | ||
515 | die(_("unable to write super-block")); | ||
516 | |||
517 | return; | ||
518 | } | ||
519 | |||
520 | void write_tables(void) | ||
521 | { | ||
522 | write_super_block(); | ||
523 | |||
524 | if (IMAPS*BLOCK_SIZE != write(IN,inode_map,IMAPS*BLOCK_SIZE)) | ||
525 | die(_("Unable to write inode map")); | ||
526 | if (ZMAPS*BLOCK_SIZE != write(IN,zone_map,ZMAPS*BLOCK_SIZE)) | ||
527 | die(_("Unable to write zone map")); | ||
528 | if (INODE_BUFFER_SIZE != write(IN,inode_buffer,INODE_BUFFER_SIZE)) | ||
529 | die(_("Unable to write inodes")); | ||
530 | } | ||
531 | |||
532 | void get_dirsize (void) | ||
533 | { | ||
534 | int block; | ||
535 | char blk[BLOCK_SIZE]; | ||
536 | int size; | ||
537 | |||
538 | #if HAVE_MINIX2 | ||
539 | if (version2) | ||
540 | block = Inode2[ROOT_INO].i_zone[0]; | ||
541 | else | ||
542 | #endif | ||
543 | block = Inode[ROOT_INO].i_zone[0]; | ||
544 | read_block (block, blk); | ||
545 | for (size = 16; size < BLOCK_SIZE; size <<= 1) { | ||
546 | if (strcmp (blk + size + 2, "..") == 0) { | ||
547 | dirsize = size; | ||
548 | namelen = size - 2; | ||
549 | return; | ||
550 | } | ||
551 | } | ||
552 | /* use defaults */ | ||
553 | } | ||
554 | |||
555 | void read_superblock(void) | ||
556 | { | ||
557 | if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET)) | ||
558 | die(_("seek failed")); | ||
559 | if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE)) | ||
560 | die(_("unable to read super block")); | ||
561 | if (MAGIC == MINIX_SUPER_MAGIC) { | ||
562 | namelen = 14; | ||
563 | dirsize = 16; | ||
564 | version2 = 0; | ||
565 | } else if (MAGIC == MINIX_SUPER_MAGIC2) { | ||
566 | namelen = 30; | ||
567 | dirsize = 32; | ||
568 | version2 = 0; | ||
569 | #ifdef HAVE_MINIX2 | ||
570 | } else if (MAGIC == MINIX2_SUPER_MAGIC) { | ||
571 | namelen = 14; | ||
572 | dirsize = 16; | ||
573 | version2 = 1; | ||
574 | } else if (MAGIC == MINIX2_SUPER_MAGIC2) { | ||
575 | namelen = 30; | ||
576 | dirsize = 32; | ||
577 | version2 = 1; | ||
578 | #endif | ||
579 | } else | ||
580 | die(_("bad magic number in super-block")); | ||
581 | if (ZONESIZE != 0 || BLOCK_SIZE != 1024) | ||
582 | die(_("Only 1k blocks/zones supported")); | ||
583 | if (IMAPS * BLOCK_SIZE * 8 < INODES + 1) | ||
584 | die(_("bad s_imap_blocks field in super-block")); | ||
585 | if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1) | ||
586 | die(_("bad s_zmap_blocks field in super-block")); | ||
587 | } | ||
588 | |||
589 | void read_tables(void) | ||
590 | { | ||
591 | inode_map = malloc(IMAPS * BLOCK_SIZE); | ||
592 | if (!inode_map) | ||
593 | die(_("Unable to allocate buffer for inode map")); | ||
594 | zone_map = malloc(ZMAPS * BLOCK_SIZE); | ||
595 | if (!inode_map) | ||
596 | die("Unable to allocate buffer for zone map"); | ||
597 | memset(inode_map,0,sizeof(inode_map)); | ||
598 | memset(zone_map,0,sizeof(zone_map)); | ||
599 | inode_buffer = malloc(INODE_BUFFER_SIZE); | ||
600 | if (!inode_buffer) | ||
601 | die(_("Unable to allocate buffer for inodes")); | ||
602 | inode_count = malloc(INODES + 1); | ||
603 | if (!inode_count) | ||
604 | die(_("Unable to allocate buffer for inode count")); | ||
605 | zone_count = malloc(ZONES); | ||
606 | if (!zone_count) | ||
607 | die(_("Unable to allocate buffer for zone count")); | ||
608 | if (IMAPS*BLOCK_SIZE != read(IN,inode_map,IMAPS*BLOCK_SIZE)) | ||
609 | die(_("Unable to read inode map")); | ||
610 | if (ZMAPS*BLOCK_SIZE != read(IN,zone_map,ZMAPS*BLOCK_SIZE)) | ||
611 | die(_("Unable to read zone map")); | ||
612 | if (INODE_BUFFER_SIZE != read(IN,inode_buffer,INODE_BUFFER_SIZE)) | ||
613 | die(_("Unable to read inodes")); | ||
614 | if (NORM_FIRSTZONE != FIRSTZONE) { | ||
615 | printf(_("Warning: Firstzone != Norm_firstzone\n")); | ||
616 | errors_uncorrected = 1; | ||
617 | } | ||
618 | get_dirsize (); | ||
619 | if (show) { | ||
620 | printf(_("%ld inodes\n"),INODES); | ||
621 | printf(_("%ld blocks\n"),ZONES); | ||
622 | printf(_("Firstdatazone=%ld (%ld)\n"),FIRSTZONE,NORM_FIRSTZONE); | ||
623 | printf(_("Zonesize=%d\n"),BLOCK_SIZE<<ZONESIZE); | ||
624 | printf(_("Maxsize=%ld\n"),MAXSIZE); | ||
625 | printf(_("Filesystem state=%d\n"), Super.s_state); | ||
626 | printf(_("namelen=%d\n\n"),namelen); | ||
627 | } | ||
628 | } | ||
629 | |||
630 | struct minix_inode * get_inode(unsigned int nr) | ||
631 | { | ||
632 | struct minix_inode * inode; | ||
633 | |||
634 | if (!nr || nr > INODES) | ||
635 | return NULL; | ||
636 | total++; | ||
637 | inode = Inode + nr; | ||
638 | if (!inode_count[nr]) { | ||
639 | if (!inode_in_use(nr)) { | ||
640 | printf(_("Inode %d marked not used, but used for file '"), | ||
641 | nr); | ||
642 | print_current_name(); | ||
643 | printf("'\n"); | ||
644 | if (repair) { | ||
645 | if (ask(_("Mark in use"),1)) | ||
646 | mark_inode(nr); | ||
647 | } else { | ||
648 | errors_uncorrected = 1; | ||
649 | } | ||
650 | } | ||
651 | if (S_ISDIR(inode->i_mode)) | ||
652 | directory++; | ||
653 | else if (S_ISREG(inode->i_mode)) | ||
654 | regular++; | ||
655 | else if (S_ISCHR(inode->i_mode)) | ||
656 | chardev++; | ||
657 | else if (S_ISBLK(inode->i_mode)) | ||
658 | blockdev++; | ||
659 | else if (S_ISLNK(inode->i_mode)) | ||
660 | symlinks++; | ||
661 | else if (S_ISSOCK(inode->i_mode)) | ||
662 | ; | ||
663 | else if (S_ISFIFO(inode->i_mode)) | ||
664 | ; | ||
665 | else { | ||
666 | print_current_name(); | ||
667 | printf(_(" has mode %05o\n"),inode->i_mode); | ||
668 | } | ||
669 | |||
670 | } else | ||
671 | links++; | ||
672 | if (!++inode_count[nr]) { | ||
673 | printf(_("Warning: inode count too big.\n")); | ||
674 | inode_count[nr]--; | ||
675 | errors_uncorrected = 1; | ||
676 | } | ||
677 | return inode; | ||
678 | } | ||
679 | |||
680 | #ifdef HAVE_MINIX2 | ||
681 | struct minix2_inode * | ||
682 | get_inode2 (unsigned int nr) | ||
683 | { | ||
684 | struct minix2_inode *inode; | ||
685 | |||
686 | if (!nr || nr > INODES) | ||
687 | return NULL; | ||
688 | total++; | ||
689 | inode = Inode2 + nr; | ||
690 | if (!inode_count[nr]) { | ||
691 | if (!inode_in_use (nr)) { | ||
692 | printf (_("Inode %d marked not used, but used for file '"), nr); | ||
693 | print_current_name (); | ||
694 | printf ("'\n"); | ||
695 | if (repair) { | ||
696 | if (ask (_("Mark in use"), 1)) | ||
697 | mark_inode (nr); | ||
698 | else | ||
699 | errors_uncorrected = 1; | ||
700 | } | ||
701 | } | ||
702 | if (S_ISDIR (inode->i_mode)) | ||
703 | directory++; | ||
704 | else if (S_ISREG (inode->i_mode)) | ||
705 | regular++; | ||
706 | else if (S_ISCHR (inode->i_mode)) | ||
707 | chardev++; | ||
708 | else if (S_ISBLK (inode->i_mode)) | ||
709 | blockdev++; | ||
710 | else if (S_ISLNK (inode->i_mode)) | ||
711 | symlinks++; | ||
712 | else if (S_ISSOCK (inode->i_mode)); | ||
713 | else if (S_ISFIFO (inode->i_mode)); | ||
714 | else { | ||
715 | print_current_name (); | ||
716 | printf (_(" has mode %05o\n"), inode->i_mode); | ||
717 | } | ||
718 | } else | ||
719 | links++; | ||
720 | if (!++inode_count[nr]) { | ||
721 | printf (_("Warning: inode count too big.\n")); | ||
722 | inode_count[nr]--; | ||
723 | errors_uncorrected = 1; | ||
724 | } | ||
725 | return inode; | ||
726 | } | ||
727 | #endif | ||
728 | |||
729 | void check_root(void) | ||
730 | { | ||
731 | struct minix_inode * inode = Inode + ROOT_INO; | ||
732 | |||
733 | if (!inode || !S_ISDIR(inode->i_mode)) | ||
734 | die(_("root inode isn't a directory")); | ||
735 | } | ||
736 | |||
737 | #ifdef HAVE_MINIX2 | ||
738 | void check_root2 (void) | ||
739 | { | ||
740 | struct minix2_inode *inode = Inode2 + ROOT_INO; | ||
741 | |||
742 | if (!inode || !S_ISDIR (inode->i_mode)) | ||
743 | die ("root inode isn't a directory"); | ||
744 | } | ||
745 | #endif | ||
746 | |||
747 | static int add_zone(unsigned short * znr, int * corrected) | ||
748 | { | ||
749 | int result; | ||
750 | int block; | ||
751 | |||
752 | result = 0; | ||
753 | block = check_zone_nr(znr, corrected); | ||
754 | if (!block) | ||
755 | return 0; | ||
756 | if (zone_count[block]) { | ||
757 | printf(_("Block has been used before. Now in file `")); | ||
758 | print_current_name(); | ||
759 | printf("'."); | ||
760 | if (ask(_("Clear"),1)) { | ||
761 | *znr = 0; | ||
762 | block = 0; | ||
763 | *corrected = 1; | ||
764 | } | ||
765 | } | ||
766 | if (!block) | ||
767 | return 0; | ||
768 | if (!zone_in_use(block)) { | ||
769 | printf(_("Block %d in file `"),block); | ||
770 | print_current_name(); | ||
771 | printf(_("' is marked not in use.")); | ||
772 | if (ask(_("Correct"),1)) | ||
773 | mark_zone(block); | ||
774 | } | ||
775 | if (!++zone_count[block]) | ||
776 | zone_count[block]--; | ||
777 | return block; | ||
778 | } | ||
779 | |||
780 | #ifdef HAVE_MINIX2 | ||
781 | static int add_zone2 (unsigned int *znr, int *corrected) | ||
782 | { | ||
783 | int result; | ||
784 | int block; | ||
785 | |||
786 | result = 0; | ||
787 | block = check_zone_nr2 (znr, corrected); | ||
788 | if (!block) | ||
789 | return 0; | ||
790 | if (zone_count[block]) { | ||
791 | printf (_("Block has been used before. Now in file `")); | ||
792 | print_current_name (); | ||
793 | printf ("'."); | ||
794 | if (ask (_("Clear"), 1)) { | ||
795 | *znr = 0; | ||
796 | block = 0; | ||
797 | *corrected = 1; | ||
798 | } | ||
799 | } | ||
800 | if (!block) | ||
801 | return 0; | ||
802 | if (!zone_in_use (block)) { | ||
803 | printf (_("Block %d in file `"), block); | ||
804 | print_current_name (); | ||
805 | printf (_("' is marked not in use.")); | ||
806 | if (ask (_("Correct"), 1)) | ||
807 | mark_zone (block); | ||
808 | } | ||
809 | if (!++zone_count[block]) | ||
810 | zone_count[block]--; | ||
811 | return block; | ||
812 | } | ||
813 | #endif | ||
814 | |||
815 | static void add_zone_ind(unsigned short * znr, int * corrected) | ||
816 | { | ||
817 | static char blk[BLOCK_SIZE]; | ||
818 | int i, chg_blk=0; | ||
819 | int block; | ||
820 | |||
821 | block = add_zone(znr, corrected); | ||
822 | if (!block) | ||
823 | return; | ||
824 | read_block(block, blk); | ||
825 | for (i=0 ; i < (BLOCK_SIZE>>1) ; i++) | ||
826 | add_zone(i + (unsigned short *) blk, &chg_blk); | ||
827 | if (chg_blk) | ||
828 | write_block(block, blk); | ||
829 | } | ||
830 | |||
831 | #ifdef HAVE_MINIX2 | ||
832 | static void | ||
833 | add_zone_ind2 (unsigned int *znr, int *corrected) | ||
834 | { | ||
835 | static char blk[BLOCK_SIZE]; | ||
836 | int i, chg_blk = 0; | ||
837 | int block; | ||
838 | |||
839 | block = add_zone2 (znr, corrected); | ||
840 | if (!block) | ||
841 | return; | ||
842 | read_block (block, blk); | ||
843 | for (i = 0; i < BLOCK_SIZE >> 2; i++) | ||
844 | add_zone2 (i + (unsigned int *) blk, &chg_blk); | ||
845 | if (chg_blk) | ||
846 | write_block (block, blk); | ||
847 | } | ||
848 | #endif | ||
849 | |||
850 | static void add_zone_dind(unsigned short * znr, int * corrected) | ||
851 | { | ||
852 | static char blk[BLOCK_SIZE]; | ||
853 | int i, blk_chg=0; | ||
854 | int block; | ||
855 | |||
856 | block = add_zone(znr, corrected); | ||
857 | if (!block) | ||
858 | return; | ||
859 | read_block(block, blk); | ||
860 | for (i=0 ; i < (BLOCK_SIZE>>1) ; i++) | ||
861 | add_zone_ind(i + (unsigned short *) blk, &blk_chg); | ||
862 | if (blk_chg) | ||
863 | write_block(block, blk); | ||
864 | } | ||
865 | |||
866 | #ifdef HAVE_MINIX2 | ||
867 | static void | ||
868 | add_zone_dind2 (unsigned int *znr, int *corrected) | ||
869 | { | ||
870 | static char blk[BLOCK_SIZE]; | ||
871 | int i, blk_chg = 0; | ||
872 | int block; | ||
873 | |||
874 | block = add_zone2 (znr, corrected); | ||
875 | if (!block) | ||
876 | return; | ||
877 | read_block (block, blk); | ||
878 | for (i = 0; i < BLOCK_SIZE >> 2; i++) | ||
879 | add_zone_ind2 (i + (unsigned int *) blk, &blk_chg); | ||
880 | if (blk_chg) | ||
881 | write_block (block, blk); | ||
882 | } | ||
883 | |||
884 | static void | ||
885 | add_zone_tind2 (unsigned int *znr, int *corrected) | ||
886 | { | ||
887 | static char blk[BLOCK_SIZE]; | ||
888 | int i, blk_chg = 0; | ||
889 | int block; | ||
890 | |||
891 | block = add_zone2 (znr, corrected); | ||
892 | if (!block) | ||
893 | return; | ||
894 | read_block (block, blk); | ||
895 | for (i = 0; i < BLOCK_SIZE >> 2; i++) | ||
896 | add_zone_dind2 (i + (unsigned int *) blk, &blk_chg); | ||
897 | if (blk_chg) | ||
898 | write_block (block, blk); | ||
899 | } | ||
900 | #endif | ||
901 | |||
902 | void check_zones(unsigned int i) | ||
903 | { | ||
904 | struct minix_inode * inode; | ||
905 | |||
906 | if (!i || i > INODES) | ||
907 | return; | ||
908 | if (inode_count[i] > 1) /* have we counted this file already? */ | ||
909 | return; | ||
910 | inode = Inode + i; | ||
911 | if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && | ||
912 | !S_ISLNK(inode->i_mode)) | ||
913 | return; | ||
914 | for (i=0 ; i<7 ; i++) | ||
915 | add_zone(i + inode->i_zone, &changed); | ||
916 | add_zone_ind(7 + inode->i_zone, &changed); | ||
917 | add_zone_dind(8 + inode->i_zone, &changed); | ||
918 | } | ||
919 | |||
920 | #ifdef HAVE_MINIX2 | ||
921 | void | ||
922 | check_zones2 (unsigned int i) | ||
923 | { | ||
924 | struct minix2_inode *inode; | ||
925 | |||
926 | if (!i || i > INODES) | ||
927 | return; | ||
928 | if (inode_count[i] > 1) /* have we counted this file already? */ | ||
929 | return; | ||
930 | inode = Inode2 + i; | ||
931 | if (!S_ISDIR (inode->i_mode) && !S_ISREG (inode->i_mode) | ||
932 | && !S_ISLNK (inode->i_mode)) | ||
933 | return; | ||
934 | for (i = 0; i < 7; i++) | ||
935 | add_zone2 (i + inode->i_zone, &changed); | ||
936 | add_zone_ind2 (7 + inode->i_zone, &changed); | ||
937 | add_zone_dind2 (8 + inode->i_zone, &changed); | ||
938 | add_zone_tind2 (9 + inode->i_zone, &changed); | ||
939 | } | ||
940 | #endif | ||
941 | |||
942 | void check_file(struct minix_inode * dir, unsigned int offset) | ||
943 | { | ||
944 | static char blk[BLOCK_SIZE]; | ||
945 | struct minix_inode * inode; | ||
946 | int ino; | ||
947 | char * name; | ||
948 | int block; | ||
949 | |||
950 | block = map_block(dir,offset/BLOCK_SIZE); | ||
951 | read_block(block, blk); | ||
952 | name = blk + (offset % BLOCK_SIZE) + 2; | ||
953 | ino = * (unsigned short *) (name-2); | ||
954 | if (ino > INODES) { | ||
955 | print_current_name(); | ||
956 | printf(_(" contains a bad inode number for file '")); | ||
957 | printf("%.*s'.",namelen,name); | ||
958 | if (ask(_(" Remove"),1)) { | ||
959 | *(unsigned short *)(name-2) = 0; | ||
960 | write_block(block, blk); | ||
961 | } | ||
962 | ino = 0; | ||
963 | } | ||
964 | if (name_depth < MAX_DEPTH) | ||
965 | strncpy (name_list[name_depth], name, namelen); | ||
966 | name_depth++; | ||
967 | inode = get_inode(ino); | ||
968 | name_depth--; | ||
969 | if (!offset) { | ||
970 | if (!inode || strcmp(".",name)) { | ||
971 | print_current_name(); | ||
972 | printf(_(": bad directory: '.' isn't first\n")); | ||
973 | errors_uncorrected = 1; | ||
974 | } else return; | ||
975 | } | ||
976 | if (offset == dirsize) { | ||
977 | if (!inode || strcmp("..",name)) { | ||
978 | print_current_name(); | ||
979 | printf(_(": bad directory: '..' isn't second\n")); | ||
980 | errors_uncorrected = 1; | ||
981 | } else return; | ||
982 | } | ||
983 | if (!inode) | ||
984 | return; | ||
985 | if (name_depth < MAX_DEPTH) | ||
986 | strncpy(name_list[name_depth],name,namelen); | ||
987 | name_depth++; | ||
988 | if (list) { | ||
989 | if (verbose) | ||
990 | printf("%6d %07o %3d ",ino,inode->i_mode,inode->i_nlinks); | ||
991 | print_current_name(); | ||
992 | if (S_ISDIR(inode->i_mode)) | ||
993 | printf(":\n"); | ||
994 | else | ||
995 | printf("\n"); | ||
996 | } | ||
997 | check_zones(ino); | ||
998 | if (inode && S_ISDIR(inode->i_mode)) | ||
999 | recursive_check(ino); | ||
1000 | name_depth--; | ||
1001 | return; | ||
1002 | } | ||
1003 | |||
1004 | #ifdef HAVE_MINIX2 | ||
1005 | void | ||
1006 | check_file2 (struct minix2_inode *dir, unsigned int offset) | ||
1007 | { | ||
1008 | static char blk[BLOCK_SIZE]; | ||
1009 | struct minix2_inode *inode; | ||
1010 | int ino; | ||
1011 | char *name; | ||
1012 | int block; | ||
1013 | |||
1014 | block = map_block2 (dir, offset / BLOCK_SIZE); | ||
1015 | read_block (block, blk); | ||
1016 | name = blk + (offset % BLOCK_SIZE) + 2; | ||
1017 | ino = *(unsigned short *) (name - 2); | ||
1018 | if (ino > INODES) { | ||
1019 | print_current_name (); | ||
1020 | printf (_(" contains a bad inode number for file '")); | ||
1021 | printf ("%.*s'.", namelen, name); | ||
1022 | if (ask (_(" Remove"), 1)) { | ||
1023 | *(unsigned short *) (name - 2) = 0; | ||
1024 | write_block (block, blk); | ||
1025 | } | ||
1026 | ino = 0; | ||
1027 | } | ||
1028 | if (name_depth < MAX_DEPTH) | ||
1029 | strncpy (name_list[name_depth], name, namelen); | ||
1030 | name_depth++; | ||
1031 | inode = get_inode2 (ino); | ||
1032 | name_depth--; | ||
1033 | if (!offset) { | ||
1034 | if (!inode || strcmp (".", name)) { | ||
1035 | print_current_name (); | ||
1036 | printf (_(": bad directory: '.' isn't first\n")); | ||
1037 | errors_uncorrected = 1; | ||
1038 | } else | ||
1039 | return; | ||
1040 | } | ||
1041 | if (offset == dirsize) { | ||
1042 | if (!inode || strcmp ("..", name)) { | ||
1043 | print_current_name (); | ||
1044 | printf (_(": bad directory: '..' isn't second\n")); | ||
1045 | errors_uncorrected = 1; | ||
1046 | } else | ||
1047 | return; | ||
1048 | } | ||
1049 | if (!inode) | ||
1050 | return; | ||
1051 | name_depth++; | ||
1052 | if (list) { | ||
1053 | if (verbose) | ||
1054 | printf ("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks); | ||
1055 | print_current_name (); | ||
1056 | if (S_ISDIR (inode->i_mode)) | ||
1057 | printf (":\n"); | ||
1058 | else | ||
1059 | printf ("\n"); | ||
1060 | } | ||
1061 | check_zones2 (ino); | ||
1062 | if (inode && S_ISDIR (inode->i_mode)) | ||
1063 | recursive_check2 (ino); | ||
1064 | name_depth--; | ||
1065 | return; | ||
1066 | } | ||
1067 | #endif | ||
1068 | |||
1069 | void recursive_check(unsigned int ino) | ||
1070 | { | ||
1071 | struct minix_inode * dir; | ||
1072 | unsigned int offset; | ||
1073 | |||
1074 | dir = Inode + ino; | ||
1075 | if (!S_ISDIR(dir->i_mode)) | ||
1076 | die(_("internal error")); | ||
1077 | if (dir->i_size < 2 * dirsize) { | ||
1078 | print_current_name(); | ||
1079 | printf(_(": bad directory: size<32")); | ||
1080 | errors_uncorrected = 1; | ||
1081 | } | ||
1082 | for (offset = 0 ; offset < dir->i_size ; offset += dirsize) | ||
1083 | check_file(dir,offset); | ||
1084 | } | ||
1085 | |||
1086 | #ifdef HAVE_MINIX2 | ||
1087 | void | ||
1088 | recursive_check2 (unsigned int ino) | ||
1089 | { | ||
1090 | struct minix2_inode *dir; | ||
1091 | unsigned int offset; | ||
1092 | |||
1093 | dir = Inode2 + ino; | ||
1094 | if (!S_ISDIR (dir->i_mode)) | ||
1095 | die ("internal error"); | ||
1096 | if (dir->i_size < 2 * dirsize) { | ||
1097 | print_current_name (); | ||
1098 | printf (_(": bad directory: size < 32")); | ||
1099 | errors_uncorrected = 1; | ||
1100 | } | ||
1101 | for (offset = 0; offset < dir->i_size; offset += dirsize) | ||
1102 | check_file2 (dir, offset); | ||
1103 | } | ||
1104 | #endif | ||
1105 | |||
1106 | int bad_zone(int i) | ||
1107 | { | ||
1108 | char buffer[1024]; | ||
1109 | |||
1110 | if (BLOCK_SIZE*i != lseek(IN, BLOCK_SIZE*i, SEEK_SET)) | ||
1111 | die(_("seek failed in bad_zone")); | ||
1112 | return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE)); | ||
1113 | } | ||
1114 | |||
1115 | void check_counts(void) | ||
1116 | { | ||
1117 | int i; | ||
1118 | |||
1119 | for (i=1 ; i <= INODES ; i++) { | ||
1120 | if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) { | ||
1121 | printf(_("Inode %d mode not cleared."),i); | ||
1122 | if (ask(_("Clear"),1)) { | ||
1123 | Inode[i].i_mode = 0; | ||
1124 | changed = 1; | ||
1125 | } | ||
1126 | } | ||
1127 | if (!inode_count[i]) { | ||
1128 | if (!inode_in_use(i)) | ||
1129 | continue; | ||
1130 | printf(_("Inode %d not used, marked used in the bitmap."),i); | ||
1131 | if (ask(_("Clear"),1)) | ||
1132 | unmark_inode(i); | ||
1133 | continue; | ||
1134 | } | ||
1135 | if (!inode_in_use(i)) { | ||
1136 | printf(_("Inode %d used, marked unused in the bitmap."), | ||
1137 | i); | ||
1138 | if (ask("Set",1)) | ||
1139 | mark_inode(i); | ||
1140 | } | ||
1141 | if (Inode[i].i_nlinks != inode_count[i]) { | ||
1142 | printf(_("Inode %d (mode = %07o), i_nlinks=%d, counted=%d."), | ||
1143 | i,Inode[i].i_mode,Inode[i].i_nlinks,inode_count[i]); | ||
1144 | if (ask(_("Set i_nlinks to count"),1)) { | ||
1145 | Inode[i].i_nlinks=inode_count[i]; | ||
1146 | changed=1; | ||
1147 | } | ||
1148 | } | ||
1149 | } | ||
1150 | for (i=FIRSTZONE ; i < ZONES ; i++) { | ||
1151 | if (zone_in_use(i) == zone_count[i]) | ||
1152 | continue; | ||
1153 | if (!zone_count[i]) { | ||
1154 | if (bad_zone(i)) | ||
1155 | continue; | ||
1156 | printf(_("Zone %d: marked in use, no file uses it."),i); | ||
1157 | if (ask(_("Unmark"),1)) | ||
1158 | unmark_zone(i); | ||
1159 | continue; | ||
1160 | } | ||
1161 | printf(_("Zone %d: %sin use, counted=%d\n"), | ||
1162 | i,zone_in_use(i)?"":_("not "),zone_count[i]); | ||
1163 | } | ||
1164 | } | ||
1165 | |||
1166 | #ifdef HAVE_MINIX2 | ||
1167 | void | ||
1168 | check_counts2 (void) | ||
1169 | { | ||
1170 | int i; | ||
1171 | |||
1172 | for (i = 1; i <= INODES; i++) { | ||
1173 | if (!inode_in_use (i) && Inode2[i].i_mode && warn_mode) { | ||
1174 | printf (_("Inode %d mode not cleared."), i); | ||
1175 | if (ask (_("Clear"), 1)) { | ||
1176 | Inode2[i].i_mode = 0; | ||
1177 | changed = 1; | ||
1178 | } | ||
1179 | } | ||
1180 | if (!inode_count[i]) { | ||
1181 | if (!inode_in_use (i)) | ||
1182 | continue; | ||
1183 | printf (_("Inode %d not used, marked used in the bitmap."), i); | ||
1184 | if (ask (_("Clear"), 1)) | ||
1185 | unmark_inode (i); | ||
1186 | continue; | ||
1187 | } | ||
1188 | if (!inode_in_use (i)) { | ||
1189 | printf (_("Inode %d used, marked unused in the bitmap."), i); | ||
1190 | if (ask (_("Set"), 1)) | ||
1191 | mark_inode (i); | ||
1192 | } | ||
1193 | if (Inode2[i].i_nlinks != inode_count[i]) { | ||
1194 | printf (_("Inode %d (mode = %07o), i_nlinks=%d, counted=%d."), | ||
1195 | i, Inode2[i].i_mode, Inode2[i].i_nlinks, inode_count[i]); | ||
1196 | if (ask (_("Set i_nlinks to count"), 1)) { | ||
1197 | Inode2[i].i_nlinks = inode_count[i]; | ||
1198 | changed = 1; | ||
1199 | } | ||
1200 | } | ||
1201 | } | ||
1202 | for (i = FIRSTZONE; i < ZONES; i++) { | ||
1203 | if (zone_in_use (i) == zone_count[i]) | ||
1204 | continue; | ||
1205 | if (!zone_count[i]) { | ||
1206 | if (bad_zone (i)) | ||
1207 | continue; | ||
1208 | printf (_("Zone %d: marked in use, no file uses it."), i); | ||
1209 | if (ask (_("Unmark"), 1)) | ||
1210 | unmark_zone (i); | ||
1211 | continue; | ||
1212 | } | ||
1213 | printf (_("Zone %d: %sin use, counted=%d\n"), | ||
1214 | i, zone_in_use (i) ? "" : _("not "), zone_count[i]); | ||
1215 | } | ||
1216 | } | ||
1217 | #endif | ||
1218 | |||
1219 | void check(void) | ||
1220 | { | ||
1221 | memset(inode_count,0,(INODES + 1) * sizeof(*inode_count)); | ||
1222 | memset(zone_count,0,ZONES*sizeof(*zone_count)); | ||
1223 | check_zones(ROOT_INO); | ||
1224 | recursive_check(ROOT_INO); | ||
1225 | check_counts(); | ||
1226 | } | ||
1227 | |||
1228 | #ifdef HAVE_MINIX2 | ||
1229 | void | ||
1230 | check2 (void) | ||
1231 | { | ||
1232 | memset (inode_count, 0, (INODES + 1) * sizeof (*inode_count)); | ||
1233 | memset (zone_count, 0, ZONES * sizeof (*zone_count)); | ||
1234 | check_zones2 (ROOT_INO); | ||
1235 | recursive_check2 (ROOT_INO); | ||
1236 | check_counts2 (); | ||
1237 | } | ||
1238 | #endif | ||
1239 | |||
1240 | extern int | ||
1241 | fsck_minix_main(int argc, char ** argv) | ||
1242 | { | ||
1243 | struct termios tmp; | ||
1244 | int count; | ||
1245 | int retcode = 0; | ||
1246 | |||
1247 | setlocale(LC_ALL, ""); | ||
1248 | bindtextdomain(PACKAGE, LOCALEDIR); | ||
1249 | textdomain(PACKAGE); | ||
1250 | |||
1251 | |||
1252 | if (argc && *argv) | ||
1253 | program_name = *argv; | ||
1254 | if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) | ||
1255 | die(_("bad inode size")); | ||
1256 | #ifdef HAVE_MINIX2 | ||
1257 | if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) | ||
1258 | die(_("bad v2 inode size")); | ||
1259 | #endif | ||
1260 | while (argc-- > 1) { | ||
1261 | argv++; | ||
1262 | if (argv[0][0] != '-') { | ||
1263 | if (device_name) | ||
1264 | usage(); | ||
1265 | else | ||
1266 | device_name = argv[0]; | ||
1267 | } else while (*++argv[0]) | ||
1268 | switch (argv[0][0]) { | ||
1269 | case 'l': list=1; break; | ||
1270 | case 'a': automatic=1; repair=1; break; | ||
1271 | case 'r': automatic=0; repair=1; break; | ||
1272 | case 'v': verbose=1; break; | ||
1273 | case 's': show=1; break; | ||
1274 | case 'm': warn_mode=1; break; | ||
1275 | case 'f': force=1; break; | ||
1276 | default: usage(); | ||
1277 | } | ||
1278 | } | ||
1279 | if (!device_name) | ||
1280 | usage(); | ||
1281 | check_mount(); /* trying to check a mounted filesystem? */ | ||
1282 | if (repair && !automatic) { | ||
1283 | if (!isatty(0) || !isatty(1)) | ||
1284 | die(_("need terminal for interactive repairs")); | ||
1285 | } | ||
1286 | IN = open(device_name,repair?O_RDWR:O_RDONLY); | ||
1287 | if (IN < 0) | ||
1288 | die(_("unable to open '%s'")); | ||
1289 | for (count=0 ; count<3 ; count++) | ||
1290 | sync(); | ||
1291 | read_superblock(); | ||
1292 | |||
1293 | /* | ||
1294 | * Determine whether or not we should continue with the checking. | ||
1295 | * This is based on the status of the filesystem valid and error | ||
1296 | * flags and whether or not the -f switch was specified on the | ||
1297 | * command line. | ||
1298 | */ | ||
1299 | printf("%s, %s / %s\n", program_name, program_version, | ||
1300 | util_linux_version); | ||
1301 | if ( !(Super.s_state & MINIX_ERROR_FS) && | ||
1302 | (Super.s_state & MINIX_VALID_FS) && | ||
1303 | !force ) { | ||
1304 | if (repair) | ||
1305 | printf(_("%s is clean, no check.\n"), device_name); | ||
1306 | return retcode; | ||
1307 | } | ||
1308 | else if (force) | ||
1309 | printf(_("Forcing filesystem check on %s.\n"), device_name); | ||
1310 | else if (repair) | ||
1311 | printf(_("Filesystem on %s is dirty, needs checking.\n"),\ | ||
1312 | device_name); | ||
1313 | |||
1314 | read_tables(); | ||
1315 | |||
1316 | if (repair && !automatic) { | ||
1317 | tcgetattr(0,&termios); | ||
1318 | tmp = termios; | ||
1319 | tmp.c_lflag &= ~(ICANON|ECHO); | ||
1320 | tcsetattr(0,TCSANOW,&tmp); | ||
1321 | termios_set = 1; | ||
1322 | } | ||
1323 | |||
1324 | #if HAVE_MINIX2 | ||
1325 | if (version2) { | ||
1326 | check_root2 (); | ||
1327 | check2 (); | ||
1328 | } else | ||
1329 | #endif | ||
1330 | { | ||
1331 | check_root(); | ||
1332 | check(); | ||
1333 | } | ||
1334 | if (verbose) { | ||
1335 | int i, free; | ||
1336 | |||
1337 | for (i=1,free=0 ; i <= INODES ; i++) | ||
1338 | if (!inode_in_use(i)) | ||
1339 | free++; | ||
1340 | printf(_("\n%6ld inodes used (%ld%%)\n"),(INODES-free), | ||
1341 | 100*(INODES-free)/INODES); | ||
1342 | for (i=FIRSTZONE,free=0 ; i < ZONES ; i++) | ||
1343 | if (!zone_in_use(i)) | ||
1344 | free++; | ||
1345 | printf(_("%6ld zones used (%ld%%)\n"),(ZONES-free), | ||
1346 | 100*(ZONES-free)/ZONES); | ||
1347 | printf(_("\n%6d regular files\n" | ||
1348 | "%6d directories\n" | ||
1349 | "%6d character device files\n" | ||
1350 | "%6d block device files\n" | ||
1351 | "%6d links\n" | ||
1352 | "%6d symbolic links\n" | ||
1353 | "------\n" | ||
1354 | "%6d files\n"), | ||
1355 | regular,directory,chardev,blockdev, | ||
1356 | links-2*directory+1,symlinks,total-2*directory+1); | ||
1357 | } | ||
1358 | if (changed) { | ||
1359 | write_tables(); | ||
1360 | printf(_( "----------------------------\n" | ||
1361 | "FILE SYSTEM HAS BEEN CHANGED\n" | ||
1362 | "----------------------------\n")); | ||
1363 | for (count=0 ; count<3 ; count++) | ||
1364 | sync(); | ||
1365 | } | ||
1366 | else if ( repair ) | ||
1367 | write_super_block(); | ||
1368 | |||
1369 | if (repair && !automatic) | ||
1370 | tcsetattr(0,TCSANOW,&termios); | ||
1371 | |||
1372 | if (changed) | ||
1373 | retcode += 3; | ||
1374 | if (errors_uncorrected) | ||
1375 | retcode += 4; | ||
1376 | return retcode; | ||
1377 | } | ||
diff --git a/internal.h b/internal.h index dedb9f193..ae0148b2e 100644 --- a/internal.h +++ b/internal.h | |||
@@ -70,6 +70,8 @@ extern int dutmp_main(int argc, char** argv); | |||
70 | extern int false_main(int argc, char** argv); | 70 | extern int false_main(int argc, char** argv); |
71 | extern int fdisk_main(int argc, char** argv); | 71 | extern int fdisk_main(int argc, char** argv); |
72 | extern int fdflush_main(int argc, char **argv); | 72 | extern int fdflush_main(int argc, char **argv); |
73 | extern int fsck_minix_main(int argc, char **argv); | ||
74 | extern int mkfs_minix_main(int argc, char **argv); | ||
73 | extern int find_main(int argc, char** argv); | 75 | extern int find_main(int argc, char** argv); |
74 | extern int grep_main(int argc, char** argv); | 76 | extern int grep_main(int argc, char** argv); |
75 | extern int halt_main(int argc, char** argv); | 77 | extern int halt_main(int argc, char** argv); |
diff --git a/mkfs_minix.c b/mkfs_minix.c new file mode 100644 index 000000000..7c8af7e1d --- /dev/null +++ b/mkfs_minix.c | |||
@@ -0,0 +1,755 @@ | |||
1 | /* | ||
2 | * mkfs.c - make a linux (minix) file-system. | ||
3 | * | ||
4 | * (C) 1991 Linus Torvalds. This file may be redistributed as per | ||
5 | * the Linux copyright. | ||
6 | */ | ||
7 | |||
8 | /* | ||
9 | * DD.MM.YY | ||
10 | * | ||
11 | * 24.11.91 - Time began. Used the fsck sources to get started. | ||
12 | * | ||
13 | * 25.11.91 - Corrected some bugs. Added support for ".badblocks" | ||
14 | * The algorithm for ".badblocks" is a bit weird, but | ||
15 | * it should work. Oh, well. | ||
16 | * | ||
17 | * 25.01.92 - Added the -l option for getting the list of bad blocks | ||
18 | * out of a named file. (Dave Rivers, rivers@ponds.uucp) | ||
19 | * | ||
20 | * 28.02.92 - Added %-information when using -c. | ||
21 | * | ||
22 | * 28.02.93 - Added support for other namelengths than the original | ||
23 | * 14 characters so that I can test the new kernel routines.. | ||
24 | * | ||
25 | * 09.10.93 - Make exit status conform to that required by fsutil | ||
26 | * (Rik Faith, faith@cs.unc.edu) | ||
27 | * | ||
28 | * 31.10.93 - Added inode request feature, for backup floppies: use | ||
29 | * 32 inodes, for a news partition use more. | ||
30 | * (Scott Heavner, sdh@po.cwru.edu) | ||
31 | * | ||
32 | * 03.01.94 - Added support for file system valid flag. | ||
33 | * (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu) | ||
34 | * | ||
35 | * 30.10.94 - added support for v2 filesystem | ||
36 | * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de) | ||
37 | * | ||
38 | * 09.11.94 - Added test to prevent overwrite of mounted fs adapted | ||
39 | * from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs | ||
40 | * program. (Daniel Quinlan, quinlan@yggdrasil.com) | ||
41 | * | ||
42 | * 03.20.95 - Clear first 512 bytes of filesystem to make certain that | ||
43 | * the filesystem is not misidentified as a MS-DOS FAT filesystem. | ||
44 | * (Daniel Quinlan, quinlan@yggdrasil.com) | ||
45 | * | ||
46 | * 02.07.96 - Added small patch from Russell King to make the program a | ||
47 | * good deal more portable (janl@math.uio.no) | ||
48 | * | ||
49 | * Usage: mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks] | ||
50 | * | ||
51 | * -c for readablility checking (SLOW!) | ||
52 | * -l for getting a list of bad blocks from a file. | ||
53 | * -n for namelength (currently the kernel only uses 14 or 30) | ||
54 | * -i for number of inodes | ||
55 | * -v for v2 filesystem | ||
56 | * | ||
57 | * The device may be a block device or a image of one, but this isn't | ||
58 | * enforced (but it's not much fun on a character device :-). | ||
59 | */ | ||
60 | |||
61 | #include <stdio.h> | ||
62 | #include <time.h> | ||
63 | #include <unistd.h> | ||
64 | #include <string.h> | ||
65 | #include <signal.h> | ||
66 | #include <fcntl.h> | ||
67 | #include <ctype.h> | ||
68 | #include <stdlib.h> | ||
69 | #include <termios.h> | ||
70 | #include <sys/stat.h> | ||
71 | #include <sys/ioctl.h> | ||
72 | #include <mntent.h> | ||
73 | #include <getopt.h> | ||
74 | |||
75 | #include <linux/fs.h> | ||
76 | #include <linux/minix_fs.h> | ||
77 | |||
78 | #include "nls.h" | ||
79 | #include "../version.h" | ||
80 | |||
81 | #ifdef MINIX2_SUPER_MAGIC2 | ||
82 | #define HAVE_MINIX2 1 | ||
83 | #endif | ||
84 | |||
85 | #ifndef __GNUC__ | ||
86 | #error "needs gcc for the bitop-__asm__'s" | ||
87 | #endif | ||
88 | |||
89 | #ifndef __linux__ | ||
90 | #define volatile | ||
91 | #endif | ||
92 | |||
93 | #define MINIX_ROOT_INO 1 | ||
94 | #define MINIX_BAD_INO 2 | ||
95 | |||
96 | #define TEST_BUFFER_BLOCKS 16 | ||
97 | #define MAX_GOOD_BLOCKS 512 | ||
98 | |||
99 | #define UPPER(size,n) (((size)+((n)-1))/(n)) | ||
100 | #define INODE_SIZE (sizeof(struct minix_inode)) | ||
101 | #ifdef HAVE_MINIX2 | ||
102 | #define INODE_SIZE2 (sizeof(struct minix2_inode)) | ||
103 | #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \ | ||
104 | : MINIX_INODES_PER_BLOCK)) | ||
105 | #else | ||
106 | #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK)) | ||
107 | #endif | ||
108 | #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE) | ||
109 | |||
110 | #define BITS_PER_BLOCK (BLOCK_SIZE<<3) | ||
111 | |||
112 | static char * program_name = "mkfs"; | ||
113 | static char * device_name = NULL; | ||
114 | static int DEV = -1; | ||
115 | static long BLOCKS = 0; | ||
116 | static int check = 0; | ||
117 | static int badblocks = 0; | ||
118 | static int namelen = 30; /* default (changed to 30, per Linus's | ||
119 | suggestion, Sun Nov 21 08:05:07 1993) */ | ||
120 | static int dirsize = 32; | ||
121 | static int magic = MINIX_SUPER_MAGIC2; | ||
122 | static int version2 = 0; | ||
123 | |||
124 | static char root_block[BLOCK_SIZE] = "\0"; | ||
125 | |||
126 | static char * inode_buffer = NULL; | ||
127 | #define Inode (((struct minix_inode *) inode_buffer)-1) | ||
128 | #ifdef HAVE_MINIX2 | ||
129 | #define Inode2 (((struct minix2_inode *) inode_buffer)-1) | ||
130 | #endif | ||
131 | static char super_block_buffer[BLOCK_SIZE]; | ||
132 | static char boot_block_buffer[512]; | ||
133 | #define Super (*(struct minix_super_block *)super_block_buffer) | ||
134 | #define INODES ((unsigned long)Super.s_ninodes) | ||
135 | #ifdef HAVE_MINIX2 | ||
136 | #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones)) | ||
137 | #else | ||
138 | #define ZONES ((unsigned long)(Super.s_nzones)) | ||
139 | #endif | ||
140 | #define IMAPS ((unsigned long)Super.s_imap_blocks) | ||
141 | #define ZMAPS ((unsigned long)Super.s_zmap_blocks) | ||
142 | #define FIRSTZONE ((unsigned long)Super.s_firstdatazone) | ||
143 | #define ZONESIZE ((unsigned long)Super.s_log_zone_size) | ||
144 | #define MAXSIZE ((unsigned long)Super.s_max_size) | ||
145 | #define MAGIC (Super.s_magic) | ||
146 | #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS) | ||
147 | |||
148 | static char *inode_map; | ||
149 | static char *zone_map; | ||
150 | |||
151 | static unsigned short good_blocks_table[MAX_GOOD_BLOCKS]; | ||
152 | static int used_good_blocks = 0; | ||
153 | static unsigned long req_nr_inodes = 0; | ||
154 | |||
155 | #include "bitops.h" | ||
156 | |||
157 | #define inode_in_use(x) (bit(inode_map,(x))) | ||
158 | #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1)) | ||
159 | |||
160 | #define mark_inode(x) (setbit(inode_map,(x))) | ||
161 | #define unmark_inode(x) (clrbit(inode_map,(x))) | ||
162 | |||
163 | #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1)) | ||
164 | #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1)) | ||
165 | |||
166 | /* | ||
167 | * Volatile to let gcc know that this doesn't return. When trying | ||
168 | * to compile this under minix, volatile gives a warning, as | ||
169 | * exit() isn't defined as volatile under minix. | ||
170 | */ | ||
171 | volatile void fatal_error(const char * fmt_string,int status) | ||
172 | { | ||
173 | fprintf(stderr,fmt_string,program_name,device_name); | ||
174 | exit(status); | ||
175 | } | ||
176 | |||
177 | volatile void die(char *str) { | ||
178 | fprintf(stderr, "%s: %s\n", program_name, str); | ||
179 | exit(8); | ||
180 | } | ||
181 | |||
182 | volatile void usage() | ||
183 | { | ||
184 | fprintf(stderr, "%s (%s)\n", program_name, util_linux_version); | ||
185 | fprintf(stderr, | ||
186 | _("Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n"), | ||
187 | program_name); | ||
188 | exit(16); | ||
189 | } | ||
190 | |||
191 | /* | ||
192 | * Check to make certain that our new filesystem won't be created on | ||
193 | * an already mounted partition. Code adapted from mke2fs, Copyright | ||
194 | * (C) 1994 Theodore Ts'o. Also licensed under GPL. | ||
195 | */ | ||
196 | static void check_mount(void) | ||
197 | { | ||
198 | FILE * f; | ||
199 | struct mntent * mnt; | ||
200 | |||
201 | if ((f = setmntent (MOUNTED, "r")) == NULL) | ||
202 | return; | ||
203 | while ((mnt = getmntent (f)) != NULL) | ||
204 | if (strcmp (device_name, mnt->mnt_fsname) == 0) | ||
205 | break; | ||
206 | endmntent (f); | ||
207 | if (!mnt) | ||
208 | return; | ||
209 | |||
210 | die(_("%s is mounted; will not make a filesystem here!")); | ||
211 | } | ||
212 | |||
213 | static long valid_offset (int fd, int offset) | ||
214 | { | ||
215 | char ch; | ||
216 | |||
217 | if (lseek (fd, offset, 0) < 0) | ||
218 | return 0; | ||
219 | if (read (fd, &ch, 1) < 1) | ||
220 | return 0; | ||
221 | return 1; | ||
222 | } | ||
223 | |||
224 | static int count_blocks (int fd) | ||
225 | { | ||
226 | int high, low; | ||
227 | |||
228 | low = 0; | ||
229 | for (high = 1; valid_offset (fd, high); high *= 2) | ||
230 | low = high; | ||
231 | while (low < high - 1) | ||
232 | { | ||
233 | const int mid = (low + high) / 2; | ||
234 | |||
235 | if (valid_offset (fd, mid)) | ||
236 | low = mid; | ||
237 | else | ||
238 | high = mid; | ||
239 | } | ||
240 | valid_offset (fd, 0); | ||
241 | return (low + 1); | ||
242 | } | ||
243 | |||
244 | static int get_size(const char *file) | ||
245 | { | ||
246 | int fd; | ||
247 | long size; | ||
248 | |||
249 | fd = open(file, O_RDWR); | ||
250 | if (fd < 0) { | ||
251 | perror(file); | ||
252 | exit(1); | ||
253 | } | ||
254 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) { | ||
255 | close(fd); | ||
256 | return (size * 512); | ||
257 | } | ||
258 | |||
259 | size = count_blocks(fd); | ||
260 | close(fd); | ||
261 | return size; | ||
262 | } | ||
263 | |||
264 | void write_tables(void) | ||
265 | { | ||
266 | /* Mark the super block valid. */ | ||
267 | Super.s_state |= MINIX_VALID_FS; | ||
268 | Super.s_state &= ~MINIX_ERROR_FS; | ||
269 | |||
270 | if (lseek(DEV, 0, SEEK_SET)) | ||
271 | die(_("seek to boot block failed in write_tables")); | ||
272 | if (512 != write(DEV, boot_block_buffer, 512)) | ||
273 | die(_("unable to clear boot sector")); | ||
274 | if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET)) | ||
275 | die(_("seek failed in write_tables")); | ||
276 | if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE)) | ||
277 | die(_("unable to write super-block")); | ||
278 | if (IMAPS*BLOCK_SIZE != write(DEV,inode_map,IMAPS*BLOCK_SIZE)) | ||
279 | die(_("unable to write inode map")); | ||
280 | if (ZMAPS*BLOCK_SIZE != write(DEV,zone_map,ZMAPS*BLOCK_SIZE)) | ||
281 | die(_("unable to write zone map")); | ||
282 | if (INODE_BUFFER_SIZE != write(DEV,inode_buffer,INODE_BUFFER_SIZE)) | ||
283 | die(_("unable to write inodes")); | ||
284 | |||
285 | } | ||
286 | |||
287 | void write_block(int blk, char * buffer) | ||
288 | { | ||
289 | if (blk*BLOCK_SIZE != lseek(DEV, blk*BLOCK_SIZE, SEEK_SET)) | ||
290 | die(_("seek failed in write_block")); | ||
291 | if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE)) | ||
292 | die(_("write failed in write_block")); | ||
293 | } | ||
294 | |||
295 | int get_free_block(void) | ||
296 | { | ||
297 | int blk; | ||
298 | |||
299 | if (used_good_blocks+1 >= MAX_GOOD_BLOCKS) | ||
300 | die(_("too many bad blocks")); | ||
301 | if (used_good_blocks) | ||
302 | blk = good_blocks_table[used_good_blocks-1]+1; | ||
303 | else | ||
304 | blk = FIRSTZONE; | ||
305 | while (blk < ZONES && zone_in_use(blk)) | ||
306 | blk++; | ||
307 | if (blk >= ZONES) | ||
308 | die(_("not enough good blocks")); | ||
309 | good_blocks_table[used_good_blocks] = blk; | ||
310 | used_good_blocks++; | ||
311 | return blk; | ||
312 | } | ||
313 | |||
314 | void mark_good_blocks(void) | ||
315 | { | ||
316 | int blk; | ||
317 | |||
318 | for (blk=0 ; blk < used_good_blocks ; blk++) | ||
319 | mark_zone(good_blocks_table[blk]); | ||
320 | } | ||
321 | |||
322 | inline int next(int zone) | ||
323 | { | ||
324 | if (!zone) | ||
325 | zone = FIRSTZONE-1; | ||
326 | while (++zone < ZONES) | ||
327 | if (zone_in_use(zone)) | ||
328 | return zone; | ||
329 | return 0; | ||
330 | } | ||
331 | |||
332 | void make_bad_inode(void) | ||
333 | { | ||
334 | struct minix_inode * inode = &Inode[MINIX_BAD_INO]; | ||
335 | int i,j,zone; | ||
336 | int ind=0,dind=0; | ||
337 | unsigned short ind_block[BLOCK_SIZE>>1]; | ||
338 | unsigned short dind_block[BLOCK_SIZE>>1]; | ||
339 | |||
340 | #define NEXT_BAD (zone = next(zone)) | ||
341 | |||
342 | if (!badblocks) | ||
343 | return; | ||
344 | mark_inode(MINIX_BAD_INO); | ||
345 | inode->i_nlinks = 1; | ||
346 | inode->i_time = time(NULL); | ||
347 | inode->i_mode = S_IFREG + 0000; | ||
348 | inode->i_size = badblocks*BLOCK_SIZE; | ||
349 | zone = next(0); | ||
350 | for (i=0 ; i<7 ; i++) { | ||
351 | inode->i_zone[i] = zone; | ||
352 | if (!NEXT_BAD) | ||
353 | goto end_bad; | ||
354 | } | ||
355 | inode->i_zone[7] = ind = get_free_block(); | ||
356 | memset(ind_block,0,BLOCK_SIZE); | ||
357 | for (i=0 ; i<512 ; i++) { | ||
358 | ind_block[i] = zone; | ||
359 | if (!NEXT_BAD) | ||
360 | goto end_bad; | ||
361 | } | ||
362 | inode->i_zone[8] = dind = get_free_block(); | ||
363 | memset(dind_block,0,BLOCK_SIZE); | ||
364 | for (i=0 ; i<512 ; i++) { | ||
365 | write_block(ind,(char *) ind_block); | ||
366 | dind_block[i] = ind = get_free_block(); | ||
367 | memset(ind_block,0,BLOCK_SIZE); | ||
368 | for (j=0 ; j<512 ; j++) { | ||
369 | ind_block[j] = zone; | ||
370 | if (!NEXT_BAD) | ||
371 | goto end_bad; | ||
372 | } | ||
373 | } | ||
374 | die(_("too many bad blocks")); | ||
375 | end_bad: | ||
376 | if (ind) | ||
377 | write_block(ind, (char *) ind_block); | ||
378 | if (dind) | ||
379 | write_block(dind, (char *) dind_block); | ||
380 | } | ||
381 | |||
382 | #ifdef HAVE_MINIX2 | ||
383 | void | ||
384 | make_bad_inode2 (void) | ||
385 | { | ||
386 | struct minix2_inode *inode = &Inode2[MINIX_BAD_INO]; | ||
387 | int i, j, zone; | ||
388 | int ind = 0, dind = 0; | ||
389 | unsigned long ind_block[BLOCK_SIZE >> 2]; | ||
390 | unsigned long dind_block[BLOCK_SIZE >> 2]; | ||
391 | |||
392 | if (!badblocks) | ||
393 | return; | ||
394 | mark_inode (MINIX_BAD_INO); | ||
395 | inode->i_nlinks = 1; | ||
396 | inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL); | ||
397 | inode->i_mode = S_IFREG + 0000; | ||
398 | inode->i_size = badblocks * BLOCK_SIZE; | ||
399 | zone = next (0); | ||
400 | for (i = 0; i < 7; i++) { | ||
401 | inode->i_zone[i] = zone; | ||
402 | if (!NEXT_BAD) | ||
403 | goto end_bad; | ||
404 | } | ||
405 | inode->i_zone[7] = ind = get_free_block (); | ||
406 | memset (ind_block, 0, BLOCK_SIZE); | ||
407 | for (i = 0; i < 256; i++) { | ||
408 | ind_block[i] = zone; | ||
409 | if (!NEXT_BAD) | ||
410 | goto end_bad; | ||
411 | } | ||
412 | inode->i_zone[8] = dind = get_free_block (); | ||
413 | memset (dind_block, 0, BLOCK_SIZE); | ||
414 | for (i = 0; i < 256; i++) { | ||
415 | write_block (ind, (char *) ind_block); | ||
416 | dind_block[i] = ind = get_free_block (); | ||
417 | memset (ind_block, 0, BLOCK_SIZE); | ||
418 | for (j = 0; j < 256; j++) { | ||
419 | ind_block[j] = zone; | ||
420 | if (!NEXT_BAD) | ||
421 | goto end_bad; | ||
422 | } | ||
423 | } | ||
424 | /* Could make triple indirect block here */ | ||
425 | die (_("too many bad blocks")); | ||
426 | end_bad: | ||
427 | if (ind) | ||
428 | write_block (ind, (char *) ind_block); | ||
429 | if (dind) | ||
430 | write_block (dind, (char *) dind_block); | ||
431 | } | ||
432 | #endif | ||
433 | |||
434 | void make_root_inode(void) | ||
435 | { | ||
436 | struct minix_inode * inode = &Inode[MINIX_ROOT_INO]; | ||
437 | |||
438 | mark_inode(MINIX_ROOT_INO); | ||
439 | inode->i_zone[0] = get_free_block(); | ||
440 | inode->i_nlinks = 2; | ||
441 | inode->i_time = time(NULL); | ||
442 | if (badblocks) | ||
443 | inode->i_size = 3*dirsize; | ||
444 | else { | ||
445 | root_block[2*dirsize] = '\0'; | ||
446 | root_block[2*dirsize+1] = '\0'; | ||
447 | inode->i_size = 2*dirsize; | ||
448 | } | ||
449 | inode->i_mode = S_IFDIR + 0755; | ||
450 | inode->i_uid = getuid(); | ||
451 | if (inode->i_uid) | ||
452 | inode->i_gid = getgid(); | ||
453 | write_block(inode->i_zone[0],root_block); | ||
454 | } | ||
455 | |||
456 | #ifdef HAVE_MINIX2 | ||
457 | void | ||
458 | make_root_inode2 (void) | ||
459 | { | ||
460 | struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO]; | ||
461 | |||
462 | mark_inode (MINIX_ROOT_INO); | ||
463 | inode->i_zone[0] = get_free_block (); | ||
464 | inode->i_nlinks = 2; | ||
465 | inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL); | ||
466 | if (badblocks) | ||
467 | inode->i_size = 3 * dirsize; | ||
468 | else { | ||
469 | root_block[2 * dirsize] = '\0'; | ||
470 | root_block[2 * dirsize + 1] = '\0'; | ||
471 | inode->i_size = 2 * dirsize; | ||
472 | } | ||
473 | inode->i_mode = S_IFDIR + 0755; | ||
474 | inode->i_uid = getuid(); | ||
475 | if (inode->i_uid) | ||
476 | inode->i_gid = getgid(); | ||
477 | write_block (inode->i_zone[0], root_block); | ||
478 | } | ||
479 | #endif | ||
480 | |||
481 | void setup_tables(void) | ||
482 | { | ||
483 | int i; | ||
484 | unsigned long inodes; | ||
485 | |||
486 | memset(super_block_buffer,0,BLOCK_SIZE); | ||
487 | memset(boot_block_buffer,0,512); | ||
488 | MAGIC = magic; | ||
489 | ZONESIZE = 0; | ||
490 | MAXSIZE = version2 ? 0x7fffffff : (7+512+512*512)*1024; | ||
491 | ZONES = BLOCKS; | ||
492 | /* some magic nrs: 1 inode / 3 blocks */ | ||
493 | if ( req_nr_inodes == 0 ) | ||
494 | inodes = BLOCKS/3; | ||
495 | else | ||
496 | inodes = req_nr_inodes; | ||
497 | /* Round up inode count to fill block size */ | ||
498 | #ifdef HAVE_MINIX2 | ||
499 | if (version2) | ||
500 | inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) & | ||
501 | ~(MINIX2_INODES_PER_BLOCK - 1)); | ||
502 | else | ||
503 | #endif | ||
504 | inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) & | ||
505 | ~(MINIX_INODES_PER_BLOCK - 1)); | ||
506 | if (inodes > 65535) | ||
507 | inodes = 65535; | ||
508 | INODES = inodes; | ||
509 | IMAPS = UPPER(INODES + 1,BITS_PER_BLOCK); | ||
510 | ZMAPS = 0; | ||
511 | i=0; | ||
512 | while (ZMAPS != UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK) && i<1000) { | ||
513 | ZMAPS = UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK); | ||
514 | i++; | ||
515 | } | ||
516 | /* Real bad hack but overwise mkfs.minix can be thrown | ||
517 | * in infinite loop... | ||
518 | * try: | ||
519 | * dd if=/dev/zero of=test.fs count=10 bs=1024 | ||
520 | * /sbin/mkfs.minix -i 200 test.fs | ||
521 | * */ | ||
522 | if (i>=999) { | ||
523 | die ("unable to allocate buffers for maps"); | ||
524 | } | ||
525 | FIRSTZONE = NORM_FIRSTZONE; | ||
526 | inode_map = malloc(IMAPS * BLOCK_SIZE); | ||
527 | zone_map = malloc(ZMAPS * BLOCK_SIZE); | ||
528 | if (!inode_map || !zone_map) | ||
529 | die(_("unable to allocate buffers for maps")); | ||
530 | memset(inode_map,0xff,IMAPS * BLOCK_SIZE); | ||
531 | memset(zone_map,0xff,ZMAPS * BLOCK_SIZE); | ||
532 | for (i = FIRSTZONE ; i<ZONES ; i++) | ||
533 | unmark_zone(i); | ||
534 | for (i = MINIX_ROOT_INO ; i<=INODES ; i++) | ||
535 | unmark_inode(i); | ||
536 | inode_buffer = malloc(INODE_BUFFER_SIZE); | ||
537 | if (!inode_buffer) | ||
538 | die(_("unable to allocate buffer for inodes")); | ||
539 | memset(inode_buffer,0,INODE_BUFFER_SIZE); | ||
540 | printf(_("%ld inodes\n"),INODES); | ||
541 | printf(_("%ld blocks\n"),ZONES); | ||
542 | printf(_("Firstdatazone=%ld (%ld)\n"),FIRSTZONE,NORM_FIRSTZONE); | ||
543 | printf(_("Zonesize=%d\n"),BLOCK_SIZE<<ZONESIZE); | ||
544 | printf(_("Maxsize=%ld\n\n"),MAXSIZE); | ||
545 | } | ||
546 | |||
547 | /* | ||
548 | * Perform a test of a block; return the number of | ||
549 | * blocks readable/writeable. | ||
550 | */ | ||
551 | long do_check(char * buffer, int try, unsigned int current_block) | ||
552 | { | ||
553 | long got; | ||
554 | |||
555 | /* Seek to the correct loc. */ | ||
556 | if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) != | ||
557 | current_block * BLOCK_SIZE ) { | ||
558 | die(_("seek failed during testing of blocks")); | ||
559 | } | ||
560 | |||
561 | |||
562 | /* Try the read */ | ||
563 | got = read(DEV, buffer, try * BLOCK_SIZE); | ||
564 | if (got < 0) got = 0; | ||
565 | if (got & (BLOCK_SIZE - 1 )) { | ||
566 | printf(_("Weird values in do_check: probably bugs\n")); | ||
567 | } | ||
568 | got /= BLOCK_SIZE; | ||
569 | return got; | ||
570 | } | ||
571 | |||
572 | static unsigned int currently_testing = 0; | ||
573 | |||
574 | void alarm_intr(int alnum) | ||
575 | { | ||
576 | if (currently_testing >= ZONES) | ||
577 | return; | ||
578 | signal(SIGALRM,alarm_intr); | ||
579 | alarm(5); | ||
580 | if (!currently_testing) | ||
581 | return; | ||
582 | printf("%d ...", currently_testing); | ||
583 | fflush(stdout); | ||
584 | } | ||
585 | |||
586 | void check_blocks(void) | ||
587 | { | ||
588 | int try,got; | ||
589 | static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS]; | ||
590 | |||
591 | currently_testing=0; | ||
592 | signal(SIGALRM,alarm_intr); | ||
593 | alarm(5); | ||
594 | while (currently_testing < ZONES) { | ||
595 | if (lseek(DEV,currently_testing*BLOCK_SIZE,SEEK_SET) != | ||
596 | currently_testing*BLOCK_SIZE) | ||
597 | die(_("seek failed in check_blocks")); | ||
598 | try = TEST_BUFFER_BLOCKS; | ||
599 | if (currently_testing + try > ZONES) | ||
600 | try = ZONES-currently_testing; | ||
601 | got = do_check(buffer, try, currently_testing); | ||
602 | currently_testing += got; | ||
603 | if (got == try) | ||
604 | continue; | ||
605 | if (currently_testing < FIRSTZONE) | ||
606 | die(_("bad blocks before data-area: cannot make fs")); | ||
607 | mark_zone(currently_testing); | ||
608 | badblocks++; | ||
609 | currently_testing++; | ||
610 | } | ||
611 | if (badblocks > 1) | ||
612 | printf(_("%d bad blocks\n"), badblocks); | ||
613 | else if (badblocks == 1) | ||
614 | printf(_("one bad block\n")); | ||
615 | } | ||
616 | |||
617 | void get_list_blocks(filename) | ||
618 | char *filename; | ||
619 | |||
620 | { | ||
621 | FILE *listfile; | ||
622 | unsigned long blockno; | ||
623 | |||
624 | listfile=fopen(filename,"r"); | ||
625 | if(listfile == (FILE *)NULL) { | ||
626 | die(_("can't open file of bad blocks")); | ||
627 | } | ||
628 | while(!feof(listfile)) { | ||
629 | fscanf(listfile,"%ld\n", &blockno); | ||
630 | mark_zone(blockno); | ||
631 | badblocks++; | ||
632 | } | ||
633 | if(badblocks > 1) | ||
634 | printf(_("%d bad blocks\n"), badblocks); | ||
635 | else if (badblocks == 1) | ||
636 | printf(_("one bad block\n")); | ||
637 | } | ||
638 | |||
639 | extern int | ||
640 | mkfs_minix_main(int argc, char ** argv) | ||
641 | { | ||
642 | int i; | ||
643 | char * tmp; | ||
644 | struct stat statbuf; | ||
645 | char * listfile = NULL; | ||
646 | |||
647 | if (argc && *argv) | ||
648 | program_name = *argv; | ||
649 | if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) | ||
650 | die(_("bad inode size")); | ||
651 | #ifdef HAVE_MINIX2 | ||
652 | if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) | ||
653 | die(_("bad inode size")); | ||
654 | #endif | ||
655 | opterr = 0; | ||
656 | while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF) | ||
657 | switch (i) { | ||
658 | case 'c': | ||
659 | check=1; break; | ||
660 | case 'i': | ||
661 | req_nr_inodes = (unsigned long) atol(optarg); | ||
662 | break; | ||
663 | case 'l': | ||
664 | listfile = optarg; break; | ||
665 | case 'n': | ||
666 | i = strtoul(optarg,&tmp,0); | ||
667 | if (*tmp) | ||
668 | usage(); | ||
669 | if (i == 14) | ||
670 | magic = MINIX_SUPER_MAGIC; | ||
671 | else if (i == 30) | ||
672 | magic = MINIX_SUPER_MAGIC2; | ||
673 | else | ||
674 | usage(); | ||
675 | namelen = i; | ||
676 | dirsize = i+2; | ||
677 | break; | ||
678 | case 'v': | ||
679 | #ifdef HAVE_MINIX2 | ||
680 | version2 = 1; | ||
681 | #else | ||
682 | fatal_error(_("%s: not compiled with minix v2 support\n"),-1); | ||
683 | #endif | ||
684 | break; | ||
685 | default: | ||
686 | usage(); | ||
687 | } | ||
688 | argc -= optind; | ||
689 | argv += optind; | ||
690 | if (argc > 0 && !device_name) { | ||
691 | device_name = argv[0]; | ||
692 | argc--; | ||
693 | argv++; | ||
694 | } | ||
695 | if (argc > 0) { | ||
696 | BLOCKS = strtol(argv[0],&tmp,0); | ||
697 | if (*tmp) { | ||
698 | printf(_("strtol error: number of blocks not specified")); | ||
699 | usage(); | ||
700 | } | ||
701 | } | ||
702 | |||
703 | if (device_name && !BLOCKS) | ||
704 | BLOCKS = get_size (device_name) / 1024; | ||
705 | if (!device_name || BLOCKS<10) { | ||
706 | usage(); | ||
707 | } | ||
708 | #ifdef HAVE_MINIX2 | ||
709 | if (version2) { | ||
710 | if (namelen == 14) | ||
711 | magic = MINIX2_SUPER_MAGIC; | ||
712 | else | ||
713 | magic = MINIX2_SUPER_MAGIC2; | ||
714 | } else | ||
715 | #endif | ||
716 | if (BLOCKS > 65535) | ||
717 | BLOCKS = 65535; | ||
718 | check_mount(); /* is it already mounted? */ | ||
719 | tmp = root_block; | ||
720 | *(short *)tmp = 1; | ||
721 | strcpy(tmp+2,"."); | ||
722 | tmp += dirsize; | ||
723 | *(short *)tmp = 1; | ||
724 | strcpy(tmp+2,".."); | ||
725 | tmp += dirsize; | ||
726 | *(short *)tmp = 2; | ||
727 | strcpy(tmp+2,".badblocks"); | ||
728 | DEV = open(device_name,O_RDWR ); | ||
729 | if (DEV<0) | ||
730 | die(_("unable to open %s")); | ||
731 | if (fstat(DEV,&statbuf)<0) | ||
732 | die(_("unable to stat %s")); | ||
733 | if (!S_ISBLK(statbuf.st_mode)) | ||
734 | check=0; | ||
735 | else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) | ||
736 | die(_("will not try to make filesystem on '%s'")); | ||
737 | setup_tables(); | ||
738 | if (check) | ||
739 | check_blocks(); | ||
740 | else if (listfile) | ||
741 | get_list_blocks(listfile); | ||
742 | #ifdef HAVE_MINIX2 | ||
743 | if (version2) { | ||
744 | make_root_inode2 (); | ||
745 | make_bad_inode2 (); | ||
746 | } else | ||
747 | #endif | ||
748 | { | ||
749 | make_root_inode(); | ||
750 | make_bad_inode(); | ||
751 | } | ||
752 | mark_good_blocks(); | ||
753 | write_tables(); | ||
754 | return 0; | ||
755 | } | ||
diff --git a/util-linux/fsck_minix.c b/util-linux/fsck_minix.c new file mode 100644 index 000000000..e4b14d705 --- /dev/null +++ b/util-linux/fsck_minix.c | |||
@@ -0,0 +1,1377 @@ | |||
1 | /* | ||
2 | * fsck.c - a file system consistency checker for Linux. | ||
3 | * | ||
4 | * (C) 1991, 1992 Linus Torvalds. This file may be redistributed | ||
5 | * as per the GNU copyleft. | ||
6 | */ | ||
7 | |||
8 | /* | ||
9 | * 09.11.91 - made the first rudimetary functions | ||
10 | * | ||
11 | * 10.11.91 - updated, does checking, no repairs yet. | ||
12 | * Sent out to the mailing-list for testing. | ||
13 | * | ||
14 | * 14.11.91 - Testing seems to have gone well. Added some | ||
15 | * correction-code, and changed some functions. | ||
16 | * | ||
17 | * 15.11.91 - More correction code. Hopefully it notices most | ||
18 | * cases now, and tries to do something about them. | ||
19 | * | ||
20 | * 16.11.91 - More corrections (thanks to Mika Jalava). Most | ||
21 | * things seem to work now. Yeah, sure. | ||
22 | * | ||
23 | * | ||
24 | * 19.04.92 - Had to start over again from this old version, as a | ||
25 | * kernel bug ate my enhanced fsck in february. | ||
26 | * | ||
27 | * 28.02.93 - added support for different directory entry sizes.. | ||
28 | * | ||
29 | * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with | ||
30 | * super-block information | ||
31 | * | ||
32 | * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform | ||
33 | * to that required by fsutil | ||
34 | * | ||
35 | * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu) | ||
36 | * Added support for file system valid flag. Also | ||
37 | * added program_version variable and output of | ||
38 | * program name and version number when program | ||
39 | * is executed. | ||
40 | * | ||
41 | * 30.10.94 - added support for v2 filesystem | ||
42 | * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de) | ||
43 | * | ||
44 | * 10.12.94 - added test to prevent checking of mounted fs adapted | ||
45 | * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck | ||
46 | * program. (Daniel Quinlan, quinlan@yggdrasil.com) | ||
47 | * | ||
48 | * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such | ||
49 | * for modern libcs (janl@math.uio.no, Nicolai Langfeldt) | ||
50 | * | ||
51 | * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk | ||
52 | * (Russell King). He made them for ARM. It would seem | ||
53 | * that the ARM is powerful enough to do this in C whereas | ||
54 | * i386 and m64k must use assembly to get it fast >:-) | ||
55 | * This should make minix fsck systemindependent. | ||
56 | * (janl@math.uio.no, Nicolai Langfeldt) | ||
57 | * | ||
58 | * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler | ||
59 | * warnings. Added mc68k bitops from | ||
60 | * Joerg Dorchain <dorchain@mpi-sb.mpg.de>. | ||
61 | * | ||
62 | * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by | ||
63 | * Andreas Schwab. | ||
64 | * | ||
65 | * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org> | ||
66 | * - added Native Language Support | ||
67 | * | ||
68 | * | ||
69 | * I've had no time to add comments - hopefully the function names | ||
70 | * are comments enough. As with all file system checkers, this assumes | ||
71 | * the file system is quiescent - don't use it on a mounted device | ||
72 | * unless you can be sure nobody is writing to it (and remember that the | ||
73 | * kernel can write to it when it searches for files). | ||
74 | * | ||
75 | * Usuage: fsck [-larvsm] device | ||
76 | * -l for a listing of all the filenames | ||
77 | * -a for automatic repairs (not implemented) | ||
78 | * -r for repairs (interactive) (not implemented) | ||
79 | * -v for verbose (tells how many files) | ||
80 | * -s for super-block info | ||
81 | * -m for minix-like "mode not cleared" warnings | ||
82 | * -f force filesystem check even if filesystem marked as valid | ||
83 | * | ||
84 | * The device may be a block device or a image of one, but this isn't | ||
85 | * enforced (but it's not much fun on a character device :-). | ||
86 | */ | ||
87 | |||
88 | #include <stdio.h> | ||
89 | #include <errno.h> | ||
90 | #include <unistd.h> | ||
91 | #include <string.h> | ||
92 | #include <fcntl.h> | ||
93 | #include <ctype.h> | ||
94 | #include <stdlib.h> | ||
95 | #include <termios.h> | ||
96 | #include <mntent.h> | ||
97 | #include <sys/stat.h> | ||
98 | |||
99 | #include <linux/fs.h> | ||
100 | #include <linux/minix_fs.h> | ||
101 | #include "../version.h" | ||
102 | #include "nls.h" | ||
103 | |||
104 | #ifdef MINIX2_SUPER_MAGIC2 | ||
105 | #define HAVE_MINIX2 1 | ||
106 | #endif | ||
107 | |||
108 | #ifndef __linux__ | ||
109 | #define volatile | ||
110 | #endif | ||
111 | |||
112 | #define ROOT_INO 1 | ||
113 | |||
114 | #define UPPER(size,n) ((size+((n)-1))/(n)) | ||
115 | #define INODE_SIZE (sizeof(struct minix_inode)) | ||
116 | #ifdef HAVE_MINIX2 | ||
117 | #define INODE_SIZE2 (sizeof(struct minix2_inode)) | ||
118 | #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \ | ||
119 | : MINIX_INODES_PER_BLOCK)) | ||
120 | #else | ||
121 | #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK)) | ||
122 | #endif | ||
123 | #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE) | ||
124 | |||
125 | #define BITS_PER_BLOCK (BLOCK_SIZE<<3) | ||
126 | |||
127 | static char * program_name = "fsck.minix"; | ||
128 | static char * program_version = "1.2 - 11/11/96"; | ||
129 | static char * device_name = NULL; | ||
130 | static int IN; | ||
131 | static int repair=0, automatic=0, verbose=0, list=0, show=0, warn_mode=0, | ||
132 | force=0; | ||
133 | static int directory=0, regular=0, blockdev=0, chardev=0, links=0, | ||
134 | symlinks=0, total=0; | ||
135 | |||
136 | static int changed = 0; /* flags if the filesystem has been changed */ | ||
137 | static int errors_uncorrected = 0; /* flag if some error was not corrected */ | ||
138 | static int dirsize = 16; | ||
139 | static int namelen = 14; | ||
140 | static int version2 = 0; | ||
141 | static struct termios termios; | ||
142 | static int termios_set = 0; | ||
143 | |||
144 | /* File-name data */ | ||
145 | #define MAX_DEPTH 50 | ||
146 | static int name_depth = 0; | ||
147 | static char name_list[MAX_DEPTH][NAME_MAX+1]; | ||
148 | |||
149 | static char * inode_buffer = NULL; | ||
150 | #define Inode (((struct minix_inode *) inode_buffer)-1) | ||
151 | #define Inode2 (((struct minix2_inode *) inode_buffer)-1) | ||
152 | static char super_block_buffer[BLOCK_SIZE]; | ||
153 | #define Super (*(struct minix_super_block *)super_block_buffer) | ||
154 | #define INODES ((unsigned long)Super.s_ninodes) | ||
155 | #ifdef HAVE_MINIX2 | ||
156 | #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones)) | ||
157 | #else | ||
158 | #define ZONES ((unsigned long)(Super.s_nzones)) | ||
159 | #endif | ||
160 | #define IMAPS ((unsigned long)Super.s_imap_blocks) | ||
161 | #define ZMAPS ((unsigned long)Super.s_zmap_blocks) | ||
162 | #define FIRSTZONE ((unsigned long)Super.s_firstdatazone) | ||
163 | #define ZONESIZE ((unsigned long)Super.s_log_zone_size) | ||
164 | #define MAXSIZE ((unsigned long)Super.s_max_size) | ||
165 | #define MAGIC (Super.s_magic) | ||
166 | #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS) | ||
167 | |||
168 | static char *inode_map; | ||
169 | static char *zone_map; | ||
170 | |||
171 | static unsigned char * inode_count = NULL; | ||
172 | static unsigned char * zone_count = NULL; | ||
173 | |||
174 | void recursive_check(unsigned int ino); | ||
175 | void recursive_check2(unsigned int ino); | ||
176 | |||
177 | #include "bitops.h" | ||
178 | |||
179 | #define inode_in_use(x) (bit(inode_map,(x))) | ||
180 | #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1)) | ||
181 | |||
182 | #define mark_inode(x) (setbit(inode_map,(x)),changed=1) | ||
183 | #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1) | ||
184 | |||
185 | #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1) | ||
186 | #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1) | ||
187 | |||
188 | void leave(int) __attribute__ ((noreturn)); | ||
189 | void leave(int status) | ||
190 | { | ||
191 | if (termios_set) | ||
192 | tcsetattr(0, TCSANOW, &termios); | ||
193 | exit(status); | ||
194 | } | ||
195 | |||
196 | void usage(void) { | ||
197 | fprintf(stderr, | ||
198 | _("Usage: %s [-larvsmf] /dev/name\n"), | ||
199 | program_name); | ||
200 | leave(16); | ||
201 | } | ||
202 | |||
203 | void die(const char *str) { | ||
204 | fprintf(stderr, "%s: %s\n", program_name, str); | ||
205 | leave(8); | ||
206 | } | ||
207 | |||
208 | /* | ||
209 | * This simply goes through the file-name data and prints out the | ||
210 | * current file. | ||
211 | */ | ||
212 | void print_current_name(void) | ||
213 | { | ||
214 | int i=0; | ||
215 | |||
216 | while (i<name_depth) | ||
217 | printf("/%.*s",namelen,name_list[i++]); | ||
218 | if (i == 0) | ||
219 | printf ("/"); | ||
220 | } | ||
221 | |||
222 | int ask(const char * string, int def) | ||
223 | { | ||
224 | int c; | ||
225 | |||
226 | if (!repair) { | ||
227 | printf("\n"); | ||
228 | errors_uncorrected = 1; | ||
229 | return 0; | ||
230 | } | ||
231 | if (automatic) { | ||
232 | printf("\n"); | ||
233 | if (!def) | ||
234 | errors_uncorrected = 1; | ||
235 | return def; | ||
236 | } | ||
237 | printf(def?"%s (y/n)? ":"%s (n/y)? ",string); | ||
238 | for (;;) { | ||
239 | fflush(stdout); | ||
240 | if ((c=getchar())==EOF) { | ||
241 | if (!def) | ||
242 | errors_uncorrected = 1; | ||
243 | return def; | ||
244 | } | ||
245 | c=toupper(c); | ||
246 | if (c == 'Y') { | ||
247 | def = 1; | ||
248 | break; | ||
249 | } else if (c == 'N') { | ||
250 | def = 0; | ||
251 | break; | ||
252 | } else if (c == ' ' || c == '\n') | ||
253 | break; | ||
254 | } | ||
255 | if (def) | ||
256 | printf("y\n"); | ||
257 | else { | ||
258 | printf("n\n"); | ||
259 | errors_uncorrected = 1; | ||
260 | } | ||
261 | return def; | ||
262 | } | ||
263 | |||
264 | /* | ||
265 | * Make certain that we aren't checking a filesystem that is on a | ||
266 | * mounted partition. Code adapted from e2fsck, Copyright (C) 1993, | ||
267 | * 1994 Theodore Ts'o. Also licensed under GPL. | ||
268 | */ | ||
269 | static void check_mount(void) | ||
270 | { | ||
271 | FILE * f; | ||
272 | struct mntent * mnt; | ||
273 | int cont; | ||
274 | int fd; | ||
275 | |||
276 | if ((f = setmntent (MOUNTED, "r")) == NULL) | ||
277 | return; | ||
278 | while ((mnt = getmntent (f)) != NULL) | ||
279 | if (strcmp (device_name, mnt->mnt_fsname) == 0) | ||
280 | break; | ||
281 | endmntent (f); | ||
282 | if (!mnt) | ||
283 | return; | ||
284 | |||
285 | /* | ||
286 | * If the root is mounted read-only, then /etc/mtab is | ||
287 | * probably not correct; so we won't issue a warning based on | ||
288 | * it. | ||
289 | */ | ||
290 | fd = open(MOUNTED, O_RDWR); | ||
291 | if (fd < 0 && errno == EROFS) | ||
292 | return; | ||
293 | else | ||
294 | close(fd); | ||
295 | |||
296 | printf (_("%s is mounted. "), device_name); | ||
297 | if (isatty(0) && isatty(1)) | ||
298 | cont = ask(_("Do you really want to continue"), 0); | ||
299 | else | ||
300 | cont = 0; | ||
301 | if (!cont) { | ||
302 | printf (_("check aborted.\n")); | ||
303 | exit (0); | ||
304 | } | ||
305 | return; | ||
306 | } | ||
307 | |||
308 | /* | ||
309 | * check_zone_nr checks to see that *nr is a valid zone nr. If it | ||
310 | * isn't, it will possibly be repaired. Check_zone_nr sets *corrected | ||
311 | * if an error was corrected, and returns the zone (0 for no zone | ||
312 | * or a bad zone-number). | ||
313 | */ | ||
314 | int check_zone_nr(unsigned short * nr, int * corrected) | ||
315 | { | ||
316 | if (!*nr) | ||
317 | return 0; | ||
318 | if (*nr < FIRSTZONE) | ||
319 | printf(_("Zone nr < FIRSTZONE in file `")); | ||
320 | else if (*nr >= ZONES) | ||
321 | printf(_("Zone nr >= ZONES in file `")); | ||
322 | else | ||
323 | return *nr; | ||
324 | print_current_name(); | ||
325 | printf("'."); | ||
326 | if (ask(_("Remove block"),1)) { | ||
327 | *nr = 0; | ||
328 | *corrected = 1; | ||
329 | } | ||
330 | return 0; | ||
331 | } | ||
332 | |||
333 | #ifdef HAVE_MINIX2 | ||
334 | int check_zone_nr2 (unsigned int *nr, int *corrected) | ||
335 | { | ||
336 | if (!*nr) | ||
337 | return 0; | ||
338 | if (*nr < FIRSTZONE) | ||
339 | printf (_("Zone nr < FIRSTZONE in file `")); | ||
340 | else if (*nr >= ZONES) | ||
341 | printf (_("Zone nr >= ZONES in file `")); | ||
342 | else | ||
343 | return *nr; | ||
344 | print_current_name (); | ||
345 | printf ("'."); | ||
346 | if (ask (_("Remove block"), 1)) { | ||
347 | *nr = 0; | ||
348 | *corrected = 1; | ||
349 | } | ||
350 | return 0; | ||
351 | } | ||
352 | #endif | ||
353 | |||
354 | /* | ||
355 | * read-block reads block nr into the buffer at addr. | ||
356 | */ | ||
357 | void read_block(unsigned int nr, char * addr) | ||
358 | { | ||
359 | if (!nr) { | ||
360 | memset(addr,0,BLOCK_SIZE); | ||
361 | return; | ||
362 | } | ||
363 | if (BLOCK_SIZE*nr != lseek(IN, BLOCK_SIZE*nr, SEEK_SET)) { | ||
364 | printf(_("Read error: unable to seek to block in file '")); | ||
365 | print_current_name(); | ||
366 | printf("'\n"); | ||
367 | memset(addr,0,BLOCK_SIZE); | ||
368 | errors_uncorrected = 1; | ||
369 | } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) { | ||
370 | printf(_("Read error: bad block in file '")); | ||
371 | print_current_name(); | ||
372 | printf("'\n"); | ||
373 | memset(addr,0,BLOCK_SIZE); | ||
374 | errors_uncorrected = 1; | ||
375 | } | ||
376 | } | ||
377 | |||
378 | /* | ||
379 | * write_block writes block nr to disk. | ||
380 | */ | ||
381 | void write_block(unsigned int nr, char * addr) | ||
382 | { | ||
383 | if (!nr) | ||
384 | return; | ||
385 | if (nr < FIRSTZONE || nr >= ZONES) { | ||
386 | printf(_("Internal error: trying to write bad block\n" | ||
387 | "Write request ignored\n")); | ||
388 | errors_uncorrected = 1; | ||
389 | return; | ||
390 | } | ||
391 | if (BLOCK_SIZE*nr != lseek(IN, BLOCK_SIZE*nr, SEEK_SET)) | ||
392 | die(_("seek failed in write_block")); | ||
393 | if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) { | ||
394 | printf(_("Write error: bad block in file '")); | ||
395 | print_current_name(); | ||
396 | printf("'\n"); | ||
397 | errors_uncorrected = 1; | ||
398 | } | ||
399 | } | ||
400 | |||
401 | /* | ||
402 | * map-block calculates the absolute block nr of a block in a file. | ||
403 | * It sets 'changed' if the inode has needed changing, and re-writes | ||
404 | * any indirect blocks with errors. | ||
405 | */ | ||
406 | int map_block(struct minix_inode * inode, unsigned int blknr) | ||
407 | { | ||
408 | unsigned short ind[BLOCK_SIZE>>1]; | ||
409 | unsigned short dind[BLOCK_SIZE>>1]; | ||
410 | int blk_chg, block, result; | ||
411 | |||
412 | if (blknr<7) | ||
413 | return check_zone_nr(inode->i_zone + blknr, &changed); | ||
414 | blknr -= 7; | ||
415 | if (blknr<512) { | ||
416 | block = check_zone_nr(inode->i_zone + 7, &changed); | ||
417 | read_block(block, (char *) ind); | ||
418 | blk_chg = 0; | ||
419 | result = check_zone_nr(blknr + ind, &blk_chg); | ||
420 | if (blk_chg) | ||
421 | write_block(block, (char *) ind); | ||
422 | return result; | ||
423 | } | ||
424 | blknr -= 512; | ||
425 | block = check_zone_nr(inode->i_zone + 8, &changed); | ||
426 | read_block(block, (char *) dind); | ||
427 | blk_chg = 0; | ||
428 | result = check_zone_nr(dind + (blknr/512), &blk_chg); | ||
429 | if (blk_chg) | ||
430 | write_block(block, (char *) dind); | ||
431 | block = result; | ||
432 | read_block(block, (char *) ind); | ||
433 | blk_chg = 0; | ||
434 | result = check_zone_nr(ind + (blknr%512), &blk_chg); | ||
435 | if (blk_chg) | ||
436 | write_block(block, (char *) ind); | ||
437 | return result; | ||
438 | } | ||
439 | |||
440 | #ifdef HAVE_MINIX2 | ||
441 | int map_block2 (struct minix2_inode *inode, unsigned int blknr) | ||
442 | { | ||
443 | unsigned int ind[BLOCK_SIZE >> 2]; | ||
444 | unsigned int dind[BLOCK_SIZE >> 2]; | ||
445 | unsigned int tind[BLOCK_SIZE >> 2]; | ||
446 | int blk_chg, block, result; | ||
447 | |||
448 | if (blknr < 7) | ||
449 | return check_zone_nr2 (inode->i_zone + blknr, &changed); | ||
450 | blknr -= 7; | ||
451 | if (blknr < 256) { | ||
452 | block = check_zone_nr2 (inode->i_zone + 7, &changed); | ||
453 | read_block (block, (char *) ind); | ||
454 | blk_chg = 0; | ||
455 | result = check_zone_nr2 (blknr + ind, &blk_chg); | ||
456 | if (blk_chg) | ||
457 | write_block (block, (char *) ind); | ||
458 | return result; | ||
459 | } | ||
460 | blknr -= 256; | ||
461 | if (blknr >= 256 * 256) { | ||
462 | block = check_zone_nr2 (inode->i_zone + 8, &changed); | ||
463 | read_block (block, (char *) dind); | ||
464 | blk_chg = 0; | ||
465 | result = check_zone_nr2 (dind + blknr / 256, &blk_chg); | ||
466 | if (blk_chg) | ||
467 | write_block (block, (char *) dind); | ||
468 | block = result; | ||
469 | read_block (block, (char *) ind); | ||
470 | blk_chg = 0; | ||
471 | result = check_zone_nr2 (ind + blknr % 256, &blk_chg); | ||
472 | if (blk_chg) | ||
473 | write_block (block, (char *) ind); | ||
474 | return result; | ||
475 | } | ||
476 | blknr -= 256 * 256; | ||
477 | block = check_zone_nr2 (inode->i_zone + 9, &changed); | ||
478 | read_block (block, (char *) tind); | ||
479 | blk_chg = 0; | ||
480 | result = check_zone_nr2 (tind + blknr / (256 * 256), &blk_chg); | ||
481 | if (blk_chg) | ||
482 | write_block (block, (char *) tind); | ||
483 | block = result; | ||
484 | read_block (block, (char *) dind); | ||
485 | blk_chg = 0; | ||
486 | result = check_zone_nr2 (dind + (blknr / 256) % 256, &blk_chg); | ||
487 | if (blk_chg) | ||
488 | write_block (block, (char *) dind); | ||
489 | block = result; | ||
490 | read_block (block, (char *) ind); | ||
491 | blk_chg = 0; | ||
492 | result = check_zone_nr2 (ind + blknr % 256, &blk_chg); | ||
493 | if (blk_chg) | ||
494 | write_block (block, (char *) ind); | ||
495 | return result; | ||
496 | } | ||
497 | #endif | ||
498 | |||
499 | void write_super_block(void) | ||
500 | { | ||
501 | /* | ||
502 | * Set the state of the filesystem based on whether or not there | ||
503 | * are uncorrected errors. The filesystem valid flag is | ||
504 | * unconditionally set if we get this far. | ||
505 | */ | ||
506 | Super.s_state |= MINIX_VALID_FS; | ||
507 | if ( errors_uncorrected ) | ||
508 | Super.s_state |= MINIX_ERROR_FS; | ||
509 | else | ||
510 | Super.s_state &= ~MINIX_ERROR_FS; | ||
511 | |||
512 | if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET)) | ||
513 | die(_("seek failed in write_super_block")); | ||
514 | if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE)) | ||
515 | die(_("unable to write super-block")); | ||
516 | |||
517 | return; | ||
518 | } | ||
519 | |||
520 | void write_tables(void) | ||
521 | { | ||
522 | write_super_block(); | ||
523 | |||
524 | if (IMAPS*BLOCK_SIZE != write(IN,inode_map,IMAPS*BLOCK_SIZE)) | ||
525 | die(_("Unable to write inode map")); | ||
526 | if (ZMAPS*BLOCK_SIZE != write(IN,zone_map,ZMAPS*BLOCK_SIZE)) | ||
527 | die(_("Unable to write zone map")); | ||
528 | if (INODE_BUFFER_SIZE != write(IN,inode_buffer,INODE_BUFFER_SIZE)) | ||
529 | die(_("Unable to write inodes")); | ||
530 | } | ||
531 | |||
532 | void get_dirsize (void) | ||
533 | { | ||
534 | int block; | ||
535 | char blk[BLOCK_SIZE]; | ||
536 | int size; | ||
537 | |||
538 | #if HAVE_MINIX2 | ||
539 | if (version2) | ||
540 | block = Inode2[ROOT_INO].i_zone[0]; | ||
541 | else | ||
542 | #endif | ||
543 | block = Inode[ROOT_INO].i_zone[0]; | ||
544 | read_block (block, blk); | ||
545 | for (size = 16; size < BLOCK_SIZE; size <<= 1) { | ||
546 | if (strcmp (blk + size + 2, "..") == 0) { | ||
547 | dirsize = size; | ||
548 | namelen = size - 2; | ||
549 | return; | ||
550 | } | ||
551 | } | ||
552 | /* use defaults */ | ||
553 | } | ||
554 | |||
555 | void read_superblock(void) | ||
556 | { | ||
557 | if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET)) | ||
558 | die(_("seek failed")); | ||
559 | if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE)) | ||
560 | die(_("unable to read super block")); | ||
561 | if (MAGIC == MINIX_SUPER_MAGIC) { | ||
562 | namelen = 14; | ||
563 | dirsize = 16; | ||
564 | version2 = 0; | ||
565 | } else if (MAGIC == MINIX_SUPER_MAGIC2) { | ||
566 | namelen = 30; | ||
567 | dirsize = 32; | ||
568 | version2 = 0; | ||
569 | #ifdef HAVE_MINIX2 | ||
570 | } else if (MAGIC == MINIX2_SUPER_MAGIC) { | ||
571 | namelen = 14; | ||
572 | dirsize = 16; | ||
573 | version2 = 1; | ||
574 | } else if (MAGIC == MINIX2_SUPER_MAGIC2) { | ||
575 | namelen = 30; | ||
576 | dirsize = 32; | ||
577 | version2 = 1; | ||
578 | #endif | ||
579 | } else | ||
580 | die(_("bad magic number in super-block")); | ||
581 | if (ZONESIZE != 0 || BLOCK_SIZE != 1024) | ||
582 | die(_("Only 1k blocks/zones supported")); | ||
583 | if (IMAPS * BLOCK_SIZE * 8 < INODES + 1) | ||
584 | die(_("bad s_imap_blocks field in super-block")); | ||
585 | if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1) | ||
586 | die(_("bad s_zmap_blocks field in super-block")); | ||
587 | } | ||
588 | |||
589 | void read_tables(void) | ||
590 | { | ||
591 | inode_map = malloc(IMAPS * BLOCK_SIZE); | ||
592 | if (!inode_map) | ||
593 | die(_("Unable to allocate buffer for inode map")); | ||
594 | zone_map = malloc(ZMAPS * BLOCK_SIZE); | ||
595 | if (!inode_map) | ||
596 | die("Unable to allocate buffer for zone map"); | ||
597 | memset(inode_map,0,sizeof(inode_map)); | ||
598 | memset(zone_map,0,sizeof(zone_map)); | ||
599 | inode_buffer = malloc(INODE_BUFFER_SIZE); | ||
600 | if (!inode_buffer) | ||
601 | die(_("Unable to allocate buffer for inodes")); | ||
602 | inode_count = malloc(INODES + 1); | ||
603 | if (!inode_count) | ||
604 | die(_("Unable to allocate buffer for inode count")); | ||
605 | zone_count = malloc(ZONES); | ||
606 | if (!zone_count) | ||
607 | die(_("Unable to allocate buffer for zone count")); | ||
608 | if (IMAPS*BLOCK_SIZE != read(IN,inode_map,IMAPS*BLOCK_SIZE)) | ||
609 | die(_("Unable to read inode map")); | ||
610 | if (ZMAPS*BLOCK_SIZE != read(IN,zone_map,ZMAPS*BLOCK_SIZE)) | ||
611 | die(_("Unable to read zone map")); | ||
612 | if (INODE_BUFFER_SIZE != read(IN,inode_buffer,INODE_BUFFER_SIZE)) | ||
613 | die(_("Unable to read inodes")); | ||
614 | if (NORM_FIRSTZONE != FIRSTZONE) { | ||
615 | printf(_("Warning: Firstzone != Norm_firstzone\n")); | ||
616 | errors_uncorrected = 1; | ||
617 | } | ||
618 | get_dirsize (); | ||
619 | if (show) { | ||
620 | printf(_("%ld inodes\n"),INODES); | ||
621 | printf(_("%ld blocks\n"),ZONES); | ||
622 | printf(_("Firstdatazone=%ld (%ld)\n"),FIRSTZONE,NORM_FIRSTZONE); | ||
623 | printf(_("Zonesize=%d\n"),BLOCK_SIZE<<ZONESIZE); | ||
624 | printf(_("Maxsize=%ld\n"),MAXSIZE); | ||
625 | printf(_("Filesystem state=%d\n"), Super.s_state); | ||
626 | printf(_("namelen=%d\n\n"),namelen); | ||
627 | } | ||
628 | } | ||
629 | |||
630 | struct minix_inode * get_inode(unsigned int nr) | ||
631 | { | ||
632 | struct minix_inode * inode; | ||
633 | |||
634 | if (!nr || nr > INODES) | ||
635 | return NULL; | ||
636 | total++; | ||
637 | inode = Inode + nr; | ||
638 | if (!inode_count[nr]) { | ||
639 | if (!inode_in_use(nr)) { | ||
640 | printf(_("Inode %d marked not used, but used for file '"), | ||
641 | nr); | ||
642 | print_current_name(); | ||
643 | printf("'\n"); | ||
644 | if (repair) { | ||
645 | if (ask(_("Mark in use"),1)) | ||
646 | mark_inode(nr); | ||
647 | } else { | ||
648 | errors_uncorrected = 1; | ||
649 | } | ||
650 | } | ||
651 | if (S_ISDIR(inode->i_mode)) | ||
652 | directory++; | ||
653 | else if (S_ISREG(inode->i_mode)) | ||
654 | regular++; | ||
655 | else if (S_ISCHR(inode->i_mode)) | ||
656 | chardev++; | ||
657 | else if (S_ISBLK(inode->i_mode)) | ||
658 | blockdev++; | ||
659 | else if (S_ISLNK(inode->i_mode)) | ||
660 | symlinks++; | ||
661 | else if (S_ISSOCK(inode->i_mode)) | ||
662 | ; | ||
663 | else if (S_ISFIFO(inode->i_mode)) | ||
664 | ; | ||
665 | else { | ||
666 | print_current_name(); | ||
667 | printf(_(" has mode %05o\n"),inode->i_mode); | ||
668 | } | ||
669 | |||
670 | } else | ||
671 | links++; | ||
672 | if (!++inode_count[nr]) { | ||
673 | printf(_("Warning: inode count too big.\n")); | ||
674 | inode_count[nr]--; | ||
675 | errors_uncorrected = 1; | ||
676 | } | ||
677 | return inode; | ||
678 | } | ||
679 | |||
680 | #ifdef HAVE_MINIX2 | ||
681 | struct minix2_inode * | ||
682 | get_inode2 (unsigned int nr) | ||
683 | { | ||
684 | struct minix2_inode *inode; | ||
685 | |||
686 | if (!nr || nr > INODES) | ||
687 | return NULL; | ||
688 | total++; | ||
689 | inode = Inode2 + nr; | ||
690 | if (!inode_count[nr]) { | ||
691 | if (!inode_in_use (nr)) { | ||
692 | printf (_("Inode %d marked not used, but used for file '"), nr); | ||
693 | print_current_name (); | ||
694 | printf ("'\n"); | ||
695 | if (repair) { | ||
696 | if (ask (_("Mark in use"), 1)) | ||
697 | mark_inode (nr); | ||
698 | else | ||
699 | errors_uncorrected = 1; | ||
700 | } | ||
701 | } | ||
702 | if (S_ISDIR (inode->i_mode)) | ||
703 | directory++; | ||
704 | else if (S_ISREG (inode->i_mode)) | ||
705 | regular++; | ||
706 | else if (S_ISCHR (inode->i_mode)) | ||
707 | chardev++; | ||
708 | else if (S_ISBLK (inode->i_mode)) | ||
709 | blockdev++; | ||
710 | else if (S_ISLNK (inode->i_mode)) | ||
711 | symlinks++; | ||
712 | else if (S_ISSOCK (inode->i_mode)); | ||
713 | else if (S_ISFIFO (inode->i_mode)); | ||
714 | else { | ||
715 | print_current_name (); | ||
716 | printf (_(" has mode %05o\n"), inode->i_mode); | ||
717 | } | ||
718 | } else | ||
719 | links++; | ||
720 | if (!++inode_count[nr]) { | ||
721 | printf (_("Warning: inode count too big.\n")); | ||
722 | inode_count[nr]--; | ||
723 | errors_uncorrected = 1; | ||
724 | } | ||
725 | return inode; | ||
726 | } | ||
727 | #endif | ||
728 | |||
729 | void check_root(void) | ||
730 | { | ||
731 | struct minix_inode * inode = Inode + ROOT_INO; | ||
732 | |||
733 | if (!inode || !S_ISDIR(inode->i_mode)) | ||
734 | die(_("root inode isn't a directory")); | ||
735 | } | ||
736 | |||
737 | #ifdef HAVE_MINIX2 | ||
738 | void check_root2 (void) | ||
739 | { | ||
740 | struct minix2_inode *inode = Inode2 + ROOT_INO; | ||
741 | |||
742 | if (!inode || !S_ISDIR (inode->i_mode)) | ||
743 | die ("root inode isn't a directory"); | ||
744 | } | ||
745 | #endif | ||
746 | |||
747 | static int add_zone(unsigned short * znr, int * corrected) | ||
748 | { | ||
749 | int result; | ||
750 | int block; | ||
751 | |||
752 | result = 0; | ||
753 | block = check_zone_nr(znr, corrected); | ||
754 | if (!block) | ||
755 | return 0; | ||
756 | if (zone_count[block]) { | ||
757 | printf(_("Block has been used before. Now in file `")); | ||
758 | print_current_name(); | ||
759 | printf("'."); | ||
760 | if (ask(_("Clear"),1)) { | ||
761 | *znr = 0; | ||
762 | block = 0; | ||
763 | *corrected = 1; | ||
764 | } | ||
765 | } | ||
766 | if (!block) | ||
767 | return 0; | ||
768 | if (!zone_in_use(block)) { | ||
769 | printf(_("Block %d in file `"),block); | ||
770 | print_current_name(); | ||
771 | printf(_("' is marked not in use.")); | ||
772 | if (ask(_("Correct"),1)) | ||
773 | mark_zone(block); | ||
774 | } | ||
775 | if (!++zone_count[block]) | ||
776 | zone_count[block]--; | ||
777 | return block; | ||
778 | } | ||
779 | |||
780 | #ifdef HAVE_MINIX2 | ||
781 | static int add_zone2 (unsigned int *znr, int *corrected) | ||
782 | { | ||
783 | int result; | ||
784 | int block; | ||
785 | |||
786 | result = 0; | ||
787 | block = check_zone_nr2 (znr, corrected); | ||
788 | if (!block) | ||
789 | return 0; | ||
790 | if (zone_count[block]) { | ||
791 | printf (_("Block has been used before. Now in file `")); | ||
792 | print_current_name (); | ||
793 | printf ("'."); | ||
794 | if (ask (_("Clear"), 1)) { | ||
795 | *znr = 0; | ||
796 | block = 0; | ||
797 | *corrected = 1; | ||
798 | } | ||
799 | } | ||
800 | if (!block) | ||
801 | return 0; | ||
802 | if (!zone_in_use (block)) { | ||
803 | printf (_("Block %d in file `"), block); | ||
804 | print_current_name (); | ||
805 | printf (_("' is marked not in use.")); | ||
806 | if (ask (_("Correct"), 1)) | ||
807 | mark_zone (block); | ||
808 | } | ||
809 | if (!++zone_count[block]) | ||
810 | zone_count[block]--; | ||
811 | return block; | ||
812 | } | ||
813 | #endif | ||
814 | |||
815 | static void add_zone_ind(unsigned short * znr, int * corrected) | ||
816 | { | ||
817 | static char blk[BLOCK_SIZE]; | ||
818 | int i, chg_blk=0; | ||
819 | int block; | ||
820 | |||
821 | block = add_zone(znr, corrected); | ||
822 | if (!block) | ||
823 | return; | ||
824 | read_block(block, blk); | ||
825 | for (i=0 ; i < (BLOCK_SIZE>>1) ; i++) | ||
826 | add_zone(i + (unsigned short *) blk, &chg_blk); | ||
827 | if (chg_blk) | ||
828 | write_block(block, blk); | ||
829 | } | ||
830 | |||
831 | #ifdef HAVE_MINIX2 | ||
832 | static void | ||
833 | add_zone_ind2 (unsigned int *znr, int *corrected) | ||
834 | { | ||
835 | static char blk[BLOCK_SIZE]; | ||
836 | int i, chg_blk = 0; | ||
837 | int block; | ||
838 | |||
839 | block = add_zone2 (znr, corrected); | ||
840 | if (!block) | ||
841 | return; | ||
842 | read_block (block, blk); | ||
843 | for (i = 0; i < BLOCK_SIZE >> 2; i++) | ||
844 | add_zone2 (i + (unsigned int *) blk, &chg_blk); | ||
845 | if (chg_blk) | ||
846 | write_block (block, blk); | ||
847 | } | ||
848 | #endif | ||
849 | |||
850 | static void add_zone_dind(unsigned short * znr, int * corrected) | ||
851 | { | ||
852 | static char blk[BLOCK_SIZE]; | ||
853 | int i, blk_chg=0; | ||
854 | int block; | ||
855 | |||
856 | block = add_zone(znr, corrected); | ||
857 | if (!block) | ||
858 | return; | ||
859 | read_block(block, blk); | ||
860 | for (i=0 ; i < (BLOCK_SIZE>>1) ; i++) | ||
861 | add_zone_ind(i + (unsigned short *) blk, &blk_chg); | ||
862 | if (blk_chg) | ||
863 | write_block(block, blk); | ||
864 | } | ||
865 | |||
866 | #ifdef HAVE_MINIX2 | ||
867 | static void | ||
868 | add_zone_dind2 (unsigned int *znr, int *corrected) | ||
869 | { | ||
870 | static char blk[BLOCK_SIZE]; | ||
871 | int i, blk_chg = 0; | ||
872 | int block; | ||
873 | |||
874 | block = add_zone2 (znr, corrected); | ||
875 | if (!block) | ||
876 | return; | ||
877 | read_block (block, blk); | ||
878 | for (i = 0; i < BLOCK_SIZE >> 2; i++) | ||
879 | add_zone_ind2 (i + (unsigned int *) blk, &blk_chg); | ||
880 | if (blk_chg) | ||
881 | write_block (block, blk); | ||
882 | } | ||
883 | |||
884 | static void | ||
885 | add_zone_tind2 (unsigned int *znr, int *corrected) | ||
886 | { | ||
887 | static char blk[BLOCK_SIZE]; | ||
888 | int i, blk_chg = 0; | ||
889 | int block; | ||
890 | |||
891 | block = add_zone2 (znr, corrected); | ||
892 | if (!block) | ||
893 | return; | ||
894 | read_block (block, blk); | ||
895 | for (i = 0; i < BLOCK_SIZE >> 2; i++) | ||
896 | add_zone_dind2 (i + (unsigned int *) blk, &blk_chg); | ||
897 | if (blk_chg) | ||
898 | write_block (block, blk); | ||
899 | } | ||
900 | #endif | ||
901 | |||
902 | void check_zones(unsigned int i) | ||
903 | { | ||
904 | struct minix_inode * inode; | ||
905 | |||
906 | if (!i || i > INODES) | ||
907 | return; | ||
908 | if (inode_count[i] > 1) /* have we counted this file already? */ | ||
909 | return; | ||
910 | inode = Inode + i; | ||
911 | if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && | ||
912 | !S_ISLNK(inode->i_mode)) | ||
913 | return; | ||
914 | for (i=0 ; i<7 ; i++) | ||
915 | add_zone(i + inode->i_zone, &changed); | ||
916 | add_zone_ind(7 + inode->i_zone, &changed); | ||
917 | add_zone_dind(8 + inode->i_zone, &changed); | ||
918 | } | ||
919 | |||
920 | #ifdef HAVE_MINIX2 | ||
921 | void | ||
922 | check_zones2 (unsigned int i) | ||
923 | { | ||
924 | struct minix2_inode *inode; | ||
925 | |||
926 | if (!i || i > INODES) | ||
927 | return; | ||
928 | if (inode_count[i] > 1) /* have we counted this file already? */ | ||
929 | return; | ||
930 | inode = Inode2 + i; | ||
931 | if (!S_ISDIR (inode->i_mode) && !S_ISREG (inode->i_mode) | ||
932 | && !S_ISLNK (inode->i_mode)) | ||
933 | return; | ||
934 | for (i = 0; i < 7; i++) | ||
935 | add_zone2 (i + inode->i_zone, &changed); | ||
936 | add_zone_ind2 (7 + inode->i_zone, &changed); | ||
937 | add_zone_dind2 (8 + inode->i_zone, &changed); | ||
938 | add_zone_tind2 (9 + inode->i_zone, &changed); | ||
939 | } | ||
940 | #endif | ||
941 | |||
942 | void check_file(struct minix_inode * dir, unsigned int offset) | ||
943 | { | ||
944 | static char blk[BLOCK_SIZE]; | ||
945 | struct minix_inode * inode; | ||
946 | int ino; | ||
947 | char * name; | ||
948 | int block; | ||
949 | |||
950 | block = map_block(dir,offset/BLOCK_SIZE); | ||
951 | read_block(block, blk); | ||
952 | name = blk + (offset % BLOCK_SIZE) + 2; | ||
953 | ino = * (unsigned short *) (name-2); | ||
954 | if (ino > INODES) { | ||
955 | print_current_name(); | ||
956 | printf(_(" contains a bad inode number for file '")); | ||
957 | printf("%.*s'.",namelen,name); | ||
958 | if (ask(_(" Remove"),1)) { | ||
959 | *(unsigned short *)(name-2) = 0; | ||
960 | write_block(block, blk); | ||
961 | } | ||
962 | ino = 0; | ||
963 | } | ||
964 | if (name_depth < MAX_DEPTH) | ||
965 | strncpy (name_list[name_depth], name, namelen); | ||
966 | name_depth++; | ||
967 | inode = get_inode(ino); | ||
968 | name_depth--; | ||
969 | if (!offset) { | ||
970 | if (!inode || strcmp(".",name)) { | ||
971 | print_current_name(); | ||
972 | printf(_(": bad directory: '.' isn't first\n")); | ||
973 | errors_uncorrected = 1; | ||
974 | } else return; | ||
975 | } | ||
976 | if (offset == dirsize) { | ||
977 | if (!inode || strcmp("..",name)) { | ||
978 | print_current_name(); | ||
979 | printf(_(": bad directory: '..' isn't second\n")); | ||
980 | errors_uncorrected = 1; | ||
981 | } else return; | ||
982 | } | ||
983 | if (!inode) | ||
984 | return; | ||
985 | if (name_depth < MAX_DEPTH) | ||
986 | strncpy(name_list[name_depth],name,namelen); | ||
987 | name_depth++; | ||
988 | if (list) { | ||
989 | if (verbose) | ||
990 | printf("%6d %07o %3d ",ino,inode->i_mode,inode->i_nlinks); | ||
991 | print_current_name(); | ||
992 | if (S_ISDIR(inode->i_mode)) | ||
993 | printf(":\n"); | ||
994 | else | ||
995 | printf("\n"); | ||
996 | } | ||
997 | check_zones(ino); | ||
998 | if (inode && S_ISDIR(inode->i_mode)) | ||
999 | recursive_check(ino); | ||
1000 | name_depth--; | ||
1001 | return; | ||
1002 | } | ||
1003 | |||
1004 | #ifdef HAVE_MINIX2 | ||
1005 | void | ||
1006 | check_file2 (struct minix2_inode *dir, unsigned int offset) | ||
1007 | { | ||
1008 | static char blk[BLOCK_SIZE]; | ||
1009 | struct minix2_inode *inode; | ||
1010 | int ino; | ||
1011 | char *name; | ||
1012 | int block; | ||
1013 | |||
1014 | block = map_block2 (dir, offset / BLOCK_SIZE); | ||
1015 | read_block (block, blk); | ||
1016 | name = blk + (offset % BLOCK_SIZE) + 2; | ||
1017 | ino = *(unsigned short *) (name - 2); | ||
1018 | if (ino > INODES) { | ||
1019 | print_current_name (); | ||
1020 | printf (_(" contains a bad inode number for file '")); | ||
1021 | printf ("%.*s'.", namelen, name); | ||
1022 | if (ask (_(" Remove"), 1)) { | ||
1023 | *(unsigned short *) (name - 2) = 0; | ||
1024 | write_block (block, blk); | ||
1025 | } | ||
1026 | ino = 0; | ||
1027 | } | ||
1028 | if (name_depth < MAX_DEPTH) | ||
1029 | strncpy (name_list[name_depth], name, namelen); | ||
1030 | name_depth++; | ||
1031 | inode = get_inode2 (ino); | ||
1032 | name_depth--; | ||
1033 | if (!offset) { | ||
1034 | if (!inode || strcmp (".", name)) { | ||
1035 | print_current_name (); | ||
1036 | printf (_(": bad directory: '.' isn't first\n")); | ||
1037 | errors_uncorrected = 1; | ||
1038 | } else | ||
1039 | return; | ||
1040 | } | ||
1041 | if (offset == dirsize) { | ||
1042 | if (!inode || strcmp ("..", name)) { | ||
1043 | print_current_name (); | ||
1044 | printf (_(": bad directory: '..' isn't second\n")); | ||
1045 | errors_uncorrected = 1; | ||
1046 | } else | ||
1047 | return; | ||
1048 | } | ||
1049 | if (!inode) | ||
1050 | return; | ||
1051 | name_depth++; | ||
1052 | if (list) { | ||
1053 | if (verbose) | ||
1054 | printf ("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks); | ||
1055 | print_current_name (); | ||
1056 | if (S_ISDIR (inode->i_mode)) | ||
1057 | printf (":\n"); | ||
1058 | else | ||
1059 | printf ("\n"); | ||
1060 | } | ||
1061 | check_zones2 (ino); | ||
1062 | if (inode && S_ISDIR (inode->i_mode)) | ||
1063 | recursive_check2 (ino); | ||
1064 | name_depth--; | ||
1065 | return; | ||
1066 | } | ||
1067 | #endif | ||
1068 | |||
1069 | void recursive_check(unsigned int ino) | ||
1070 | { | ||
1071 | struct minix_inode * dir; | ||
1072 | unsigned int offset; | ||
1073 | |||
1074 | dir = Inode + ino; | ||
1075 | if (!S_ISDIR(dir->i_mode)) | ||
1076 | die(_("internal error")); | ||
1077 | if (dir->i_size < 2 * dirsize) { | ||
1078 | print_current_name(); | ||
1079 | printf(_(": bad directory: size<32")); | ||
1080 | errors_uncorrected = 1; | ||
1081 | } | ||
1082 | for (offset = 0 ; offset < dir->i_size ; offset += dirsize) | ||
1083 | check_file(dir,offset); | ||
1084 | } | ||
1085 | |||
1086 | #ifdef HAVE_MINIX2 | ||
1087 | void | ||
1088 | recursive_check2 (unsigned int ino) | ||
1089 | { | ||
1090 | struct minix2_inode *dir; | ||
1091 | unsigned int offset; | ||
1092 | |||
1093 | dir = Inode2 + ino; | ||
1094 | if (!S_ISDIR (dir->i_mode)) | ||
1095 | die ("internal error"); | ||
1096 | if (dir->i_size < 2 * dirsize) { | ||
1097 | print_current_name (); | ||
1098 | printf (_(": bad directory: size < 32")); | ||
1099 | errors_uncorrected = 1; | ||
1100 | } | ||
1101 | for (offset = 0; offset < dir->i_size; offset += dirsize) | ||
1102 | check_file2 (dir, offset); | ||
1103 | } | ||
1104 | #endif | ||
1105 | |||
1106 | int bad_zone(int i) | ||
1107 | { | ||
1108 | char buffer[1024]; | ||
1109 | |||
1110 | if (BLOCK_SIZE*i != lseek(IN, BLOCK_SIZE*i, SEEK_SET)) | ||
1111 | die(_("seek failed in bad_zone")); | ||
1112 | return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE)); | ||
1113 | } | ||
1114 | |||
1115 | void check_counts(void) | ||
1116 | { | ||
1117 | int i; | ||
1118 | |||
1119 | for (i=1 ; i <= INODES ; i++) { | ||
1120 | if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) { | ||
1121 | printf(_("Inode %d mode not cleared."),i); | ||
1122 | if (ask(_("Clear"),1)) { | ||
1123 | Inode[i].i_mode = 0; | ||
1124 | changed = 1; | ||
1125 | } | ||
1126 | } | ||
1127 | if (!inode_count[i]) { | ||
1128 | if (!inode_in_use(i)) | ||
1129 | continue; | ||
1130 | printf(_("Inode %d not used, marked used in the bitmap."),i); | ||
1131 | if (ask(_("Clear"),1)) | ||
1132 | unmark_inode(i); | ||
1133 | continue; | ||
1134 | } | ||
1135 | if (!inode_in_use(i)) { | ||
1136 | printf(_("Inode %d used, marked unused in the bitmap."), | ||
1137 | i); | ||
1138 | if (ask("Set",1)) | ||
1139 | mark_inode(i); | ||
1140 | } | ||
1141 | if (Inode[i].i_nlinks != inode_count[i]) { | ||
1142 | printf(_("Inode %d (mode = %07o), i_nlinks=%d, counted=%d."), | ||
1143 | i,Inode[i].i_mode,Inode[i].i_nlinks,inode_count[i]); | ||
1144 | if (ask(_("Set i_nlinks to count"),1)) { | ||
1145 | Inode[i].i_nlinks=inode_count[i]; | ||
1146 | changed=1; | ||
1147 | } | ||
1148 | } | ||
1149 | } | ||
1150 | for (i=FIRSTZONE ; i < ZONES ; i++) { | ||
1151 | if (zone_in_use(i) == zone_count[i]) | ||
1152 | continue; | ||
1153 | if (!zone_count[i]) { | ||
1154 | if (bad_zone(i)) | ||
1155 | continue; | ||
1156 | printf(_("Zone %d: marked in use, no file uses it."),i); | ||
1157 | if (ask(_("Unmark"),1)) | ||
1158 | unmark_zone(i); | ||
1159 | continue; | ||
1160 | } | ||
1161 | printf(_("Zone %d: %sin use, counted=%d\n"), | ||
1162 | i,zone_in_use(i)?"":_("not "),zone_count[i]); | ||
1163 | } | ||
1164 | } | ||
1165 | |||
1166 | #ifdef HAVE_MINIX2 | ||
1167 | void | ||
1168 | check_counts2 (void) | ||
1169 | { | ||
1170 | int i; | ||
1171 | |||
1172 | for (i = 1; i <= INODES; i++) { | ||
1173 | if (!inode_in_use (i) && Inode2[i].i_mode && warn_mode) { | ||
1174 | printf (_("Inode %d mode not cleared."), i); | ||
1175 | if (ask (_("Clear"), 1)) { | ||
1176 | Inode2[i].i_mode = 0; | ||
1177 | changed = 1; | ||
1178 | } | ||
1179 | } | ||
1180 | if (!inode_count[i]) { | ||
1181 | if (!inode_in_use (i)) | ||
1182 | continue; | ||
1183 | printf (_("Inode %d not used, marked used in the bitmap."), i); | ||
1184 | if (ask (_("Clear"), 1)) | ||
1185 | unmark_inode (i); | ||
1186 | continue; | ||
1187 | } | ||
1188 | if (!inode_in_use (i)) { | ||
1189 | printf (_("Inode %d used, marked unused in the bitmap."), i); | ||
1190 | if (ask (_("Set"), 1)) | ||
1191 | mark_inode (i); | ||
1192 | } | ||
1193 | if (Inode2[i].i_nlinks != inode_count[i]) { | ||
1194 | printf (_("Inode %d (mode = %07o), i_nlinks=%d, counted=%d."), | ||
1195 | i, Inode2[i].i_mode, Inode2[i].i_nlinks, inode_count[i]); | ||
1196 | if (ask (_("Set i_nlinks to count"), 1)) { | ||
1197 | Inode2[i].i_nlinks = inode_count[i]; | ||
1198 | changed = 1; | ||
1199 | } | ||
1200 | } | ||
1201 | } | ||
1202 | for (i = FIRSTZONE; i < ZONES; i++) { | ||
1203 | if (zone_in_use (i) == zone_count[i]) | ||
1204 | continue; | ||
1205 | if (!zone_count[i]) { | ||
1206 | if (bad_zone (i)) | ||
1207 | continue; | ||
1208 | printf (_("Zone %d: marked in use, no file uses it."), i); | ||
1209 | if (ask (_("Unmark"), 1)) | ||
1210 | unmark_zone (i); | ||
1211 | continue; | ||
1212 | } | ||
1213 | printf (_("Zone %d: %sin use, counted=%d\n"), | ||
1214 | i, zone_in_use (i) ? "" : _("not "), zone_count[i]); | ||
1215 | } | ||
1216 | } | ||
1217 | #endif | ||
1218 | |||
1219 | void check(void) | ||
1220 | { | ||
1221 | memset(inode_count,0,(INODES + 1) * sizeof(*inode_count)); | ||
1222 | memset(zone_count,0,ZONES*sizeof(*zone_count)); | ||
1223 | check_zones(ROOT_INO); | ||
1224 | recursive_check(ROOT_INO); | ||
1225 | check_counts(); | ||
1226 | } | ||
1227 | |||
1228 | #ifdef HAVE_MINIX2 | ||
1229 | void | ||
1230 | check2 (void) | ||
1231 | { | ||
1232 | memset (inode_count, 0, (INODES + 1) * sizeof (*inode_count)); | ||
1233 | memset (zone_count, 0, ZONES * sizeof (*zone_count)); | ||
1234 | check_zones2 (ROOT_INO); | ||
1235 | recursive_check2 (ROOT_INO); | ||
1236 | check_counts2 (); | ||
1237 | } | ||
1238 | #endif | ||
1239 | |||
1240 | extern int | ||
1241 | fsck_minix_main(int argc, char ** argv) | ||
1242 | { | ||
1243 | struct termios tmp; | ||
1244 | int count; | ||
1245 | int retcode = 0; | ||
1246 | |||
1247 | setlocale(LC_ALL, ""); | ||
1248 | bindtextdomain(PACKAGE, LOCALEDIR); | ||
1249 | textdomain(PACKAGE); | ||
1250 | |||
1251 | |||
1252 | if (argc && *argv) | ||
1253 | program_name = *argv; | ||
1254 | if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) | ||
1255 | die(_("bad inode size")); | ||
1256 | #ifdef HAVE_MINIX2 | ||
1257 | if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) | ||
1258 | die(_("bad v2 inode size")); | ||
1259 | #endif | ||
1260 | while (argc-- > 1) { | ||
1261 | argv++; | ||
1262 | if (argv[0][0] != '-') { | ||
1263 | if (device_name) | ||
1264 | usage(); | ||
1265 | else | ||
1266 | device_name = argv[0]; | ||
1267 | } else while (*++argv[0]) | ||
1268 | switch (argv[0][0]) { | ||
1269 | case 'l': list=1; break; | ||
1270 | case 'a': automatic=1; repair=1; break; | ||
1271 | case 'r': automatic=0; repair=1; break; | ||
1272 | case 'v': verbose=1; break; | ||
1273 | case 's': show=1; break; | ||
1274 | case 'm': warn_mode=1; break; | ||
1275 | case 'f': force=1; break; | ||
1276 | default: usage(); | ||
1277 | } | ||
1278 | } | ||
1279 | if (!device_name) | ||
1280 | usage(); | ||
1281 | check_mount(); /* trying to check a mounted filesystem? */ | ||
1282 | if (repair && !automatic) { | ||
1283 | if (!isatty(0) || !isatty(1)) | ||
1284 | die(_("need terminal for interactive repairs")); | ||
1285 | } | ||
1286 | IN = open(device_name,repair?O_RDWR:O_RDONLY); | ||
1287 | if (IN < 0) | ||
1288 | die(_("unable to open '%s'")); | ||
1289 | for (count=0 ; count<3 ; count++) | ||
1290 | sync(); | ||
1291 | read_superblock(); | ||
1292 | |||
1293 | /* | ||
1294 | * Determine whether or not we should continue with the checking. | ||
1295 | * This is based on the status of the filesystem valid and error | ||
1296 | * flags and whether or not the -f switch was specified on the | ||
1297 | * command line. | ||
1298 | */ | ||
1299 | printf("%s, %s / %s\n", program_name, program_version, | ||
1300 | util_linux_version); | ||
1301 | if ( !(Super.s_state & MINIX_ERROR_FS) && | ||
1302 | (Super.s_state & MINIX_VALID_FS) && | ||
1303 | !force ) { | ||
1304 | if (repair) | ||
1305 | printf(_("%s is clean, no check.\n"), device_name); | ||
1306 | return retcode; | ||
1307 | } | ||
1308 | else if (force) | ||
1309 | printf(_("Forcing filesystem check on %s.\n"), device_name); | ||
1310 | else if (repair) | ||
1311 | printf(_("Filesystem on %s is dirty, needs checking.\n"),\ | ||
1312 | device_name); | ||
1313 | |||
1314 | read_tables(); | ||
1315 | |||
1316 | if (repair && !automatic) { | ||
1317 | tcgetattr(0,&termios); | ||
1318 | tmp = termios; | ||
1319 | tmp.c_lflag &= ~(ICANON|ECHO); | ||
1320 | tcsetattr(0,TCSANOW,&tmp); | ||
1321 | termios_set = 1; | ||
1322 | } | ||
1323 | |||
1324 | #if HAVE_MINIX2 | ||
1325 | if (version2) { | ||
1326 | check_root2 (); | ||
1327 | check2 (); | ||
1328 | } else | ||
1329 | #endif | ||
1330 | { | ||
1331 | check_root(); | ||
1332 | check(); | ||
1333 | } | ||
1334 | if (verbose) { | ||
1335 | int i, free; | ||
1336 | |||
1337 | for (i=1,free=0 ; i <= INODES ; i++) | ||
1338 | if (!inode_in_use(i)) | ||
1339 | free++; | ||
1340 | printf(_("\n%6ld inodes used (%ld%%)\n"),(INODES-free), | ||
1341 | 100*(INODES-free)/INODES); | ||
1342 | for (i=FIRSTZONE,free=0 ; i < ZONES ; i++) | ||
1343 | if (!zone_in_use(i)) | ||
1344 | free++; | ||
1345 | printf(_("%6ld zones used (%ld%%)\n"),(ZONES-free), | ||
1346 | 100*(ZONES-free)/ZONES); | ||
1347 | printf(_("\n%6d regular files\n" | ||
1348 | "%6d directories\n" | ||
1349 | "%6d character device files\n" | ||
1350 | "%6d block device files\n" | ||
1351 | "%6d links\n" | ||
1352 | "%6d symbolic links\n" | ||
1353 | "------\n" | ||
1354 | "%6d files\n"), | ||
1355 | regular,directory,chardev,blockdev, | ||
1356 | links-2*directory+1,symlinks,total-2*directory+1); | ||
1357 | } | ||
1358 | if (changed) { | ||
1359 | write_tables(); | ||
1360 | printf(_( "----------------------------\n" | ||
1361 | "FILE SYSTEM HAS BEEN CHANGED\n" | ||
1362 | "----------------------------\n")); | ||
1363 | for (count=0 ; count<3 ; count++) | ||
1364 | sync(); | ||
1365 | } | ||
1366 | else if ( repair ) | ||
1367 | write_super_block(); | ||
1368 | |||
1369 | if (repair && !automatic) | ||
1370 | tcsetattr(0,TCSANOW,&termios); | ||
1371 | |||
1372 | if (changed) | ||
1373 | retcode += 3; | ||
1374 | if (errors_uncorrected) | ||
1375 | retcode += 4; | ||
1376 | return retcode; | ||
1377 | } | ||
diff --git a/util-linux/mkfs_minix.c b/util-linux/mkfs_minix.c new file mode 100644 index 000000000..7c8af7e1d --- /dev/null +++ b/util-linux/mkfs_minix.c | |||
@@ -0,0 +1,755 @@ | |||
1 | /* | ||
2 | * mkfs.c - make a linux (minix) file-system. | ||
3 | * | ||
4 | * (C) 1991 Linus Torvalds. This file may be redistributed as per | ||
5 | * the Linux copyright. | ||
6 | */ | ||
7 | |||
8 | /* | ||
9 | * DD.MM.YY | ||
10 | * | ||
11 | * 24.11.91 - Time began. Used the fsck sources to get started. | ||
12 | * | ||
13 | * 25.11.91 - Corrected some bugs. Added support for ".badblocks" | ||
14 | * The algorithm for ".badblocks" is a bit weird, but | ||
15 | * it should work. Oh, well. | ||
16 | * | ||
17 | * 25.01.92 - Added the -l option for getting the list of bad blocks | ||
18 | * out of a named file. (Dave Rivers, rivers@ponds.uucp) | ||
19 | * | ||
20 | * 28.02.92 - Added %-information when using -c. | ||
21 | * | ||
22 | * 28.02.93 - Added support for other namelengths than the original | ||
23 | * 14 characters so that I can test the new kernel routines.. | ||
24 | * | ||
25 | * 09.10.93 - Make exit status conform to that required by fsutil | ||
26 | * (Rik Faith, faith@cs.unc.edu) | ||
27 | * | ||
28 | * 31.10.93 - Added inode request feature, for backup floppies: use | ||
29 | * 32 inodes, for a news partition use more. | ||
30 | * (Scott Heavner, sdh@po.cwru.edu) | ||
31 | * | ||
32 | * 03.01.94 - Added support for file system valid flag. | ||
33 | * (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu) | ||
34 | * | ||
35 | * 30.10.94 - added support for v2 filesystem | ||
36 | * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de) | ||
37 | * | ||
38 | * 09.11.94 - Added test to prevent overwrite of mounted fs adapted | ||
39 | * from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs | ||
40 | * program. (Daniel Quinlan, quinlan@yggdrasil.com) | ||
41 | * | ||
42 | * 03.20.95 - Clear first 512 bytes of filesystem to make certain that | ||
43 | * the filesystem is not misidentified as a MS-DOS FAT filesystem. | ||
44 | * (Daniel Quinlan, quinlan@yggdrasil.com) | ||
45 | * | ||
46 | * 02.07.96 - Added small patch from Russell King to make the program a | ||
47 | * good deal more portable (janl@math.uio.no) | ||
48 | * | ||
49 | * Usage: mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks] | ||
50 | * | ||
51 | * -c for readablility checking (SLOW!) | ||
52 | * -l for getting a list of bad blocks from a file. | ||
53 | * -n for namelength (currently the kernel only uses 14 or 30) | ||
54 | * -i for number of inodes | ||
55 | * -v for v2 filesystem | ||
56 | * | ||
57 | * The device may be a block device or a image of one, but this isn't | ||
58 | * enforced (but it's not much fun on a character device :-). | ||
59 | */ | ||
60 | |||
61 | #include <stdio.h> | ||
62 | #include <time.h> | ||
63 | #include <unistd.h> | ||
64 | #include <string.h> | ||
65 | #include <signal.h> | ||
66 | #include <fcntl.h> | ||
67 | #include <ctype.h> | ||
68 | #include <stdlib.h> | ||
69 | #include <termios.h> | ||
70 | #include <sys/stat.h> | ||
71 | #include <sys/ioctl.h> | ||
72 | #include <mntent.h> | ||
73 | #include <getopt.h> | ||
74 | |||
75 | #include <linux/fs.h> | ||
76 | #include <linux/minix_fs.h> | ||
77 | |||
78 | #include "nls.h" | ||
79 | #include "../version.h" | ||
80 | |||
81 | #ifdef MINIX2_SUPER_MAGIC2 | ||
82 | #define HAVE_MINIX2 1 | ||
83 | #endif | ||
84 | |||
85 | #ifndef __GNUC__ | ||
86 | #error "needs gcc for the bitop-__asm__'s" | ||
87 | #endif | ||
88 | |||
89 | #ifndef __linux__ | ||
90 | #define volatile | ||
91 | #endif | ||
92 | |||
93 | #define MINIX_ROOT_INO 1 | ||
94 | #define MINIX_BAD_INO 2 | ||
95 | |||
96 | #define TEST_BUFFER_BLOCKS 16 | ||
97 | #define MAX_GOOD_BLOCKS 512 | ||
98 | |||
99 | #define UPPER(size,n) (((size)+((n)-1))/(n)) | ||
100 | #define INODE_SIZE (sizeof(struct minix_inode)) | ||
101 | #ifdef HAVE_MINIX2 | ||
102 | #define INODE_SIZE2 (sizeof(struct minix2_inode)) | ||
103 | #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \ | ||
104 | : MINIX_INODES_PER_BLOCK)) | ||
105 | #else | ||
106 | #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK)) | ||
107 | #endif | ||
108 | #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE) | ||
109 | |||
110 | #define BITS_PER_BLOCK (BLOCK_SIZE<<3) | ||
111 | |||
112 | static char * program_name = "mkfs"; | ||
113 | static char * device_name = NULL; | ||
114 | static int DEV = -1; | ||
115 | static long BLOCKS = 0; | ||
116 | static int check = 0; | ||
117 | static int badblocks = 0; | ||
118 | static int namelen = 30; /* default (changed to 30, per Linus's | ||
119 | suggestion, Sun Nov 21 08:05:07 1993) */ | ||
120 | static int dirsize = 32; | ||
121 | static int magic = MINIX_SUPER_MAGIC2; | ||
122 | static int version2 = 0; | ||
123 | |||
124 | static char root_block[BLOCK_SIZE] = "\0"; | ||
125 | |||
126 | static char * inode_buffer = NULL; | ||
127 | #define Inode (((struct minix_inode *) inode_buffer)-1) | ||
128 | #ifdef HAVE_MINIX2 | ||
129 | #define Inode2 (((struct minix2_inode *) inode_buffer)-1) | ||
130 | #endif | ||
131 | static char super_block_buffer[BLOCK_SIZE]; | ||
132 | static char boot_block_buffer[512]; | ||
133 | #define Super (*(struct minix_super_block *)super_block_buffer) | ||
134 | #define INODES ((unsigned long)Super.s_ninodes) | ||
135 | #ifdef HAVE_MINIX2 | ||
136 | #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones)) | ||
137 | #else | ||
138 | #define ZONES ((unsigned long)(Super.s_nzones)) | ||
139 | #endif | ||
140 | #define IMAPS ((unsigned long)Super.s_imap_blocks) | ||
141 | #define ZMAPS ((unsigned long)Super.s_zmap_blocks) | ||
142 | #define FIRSTZONE ((unsigned long)Super.s_firstdatazone) | ||
143 | #define ZONESIZE ((unsigned long)Super.s_log_zone_size) | ||
144 | #define MAXSIZE ((unsigned long)Super.s_max_size) | ||
145 | #define MAGIC (Super.s_magic) | ||
146 | #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS) | ||
147 | |||
148 | static char *inode_map; | ||
149 | static char *zone_map; | ||
150 | |||
151 | static unsigned short good_blocks_table[MAX_GOOD_BLOCKS]; | ||
152 | static int used_good_blocks = 0; | ||
153 | static unsigned long req_nr_inodes = 0; | ||
154 | |||
155 | #include "bitops.h" | ||
156 | |||
157 | #define inode_in_use(x) (bit(inode_map,(x))) | ||
158 | #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1)) | ||
159 | |||
160 | #define mark_inode(x) (setbit(inode_map,(x))) | ||
161 | #define unmark_inode(x) (clrbit(inode_map,(x))) | ||
162 | |||
163 | #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1)) | ||
164 | #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1)) | ||
165 | |||
166 | /* | ||
167 | * Volatile to let gcc know that this doesn't return. When trying | ||
168 | * to compile this under minix, volatile gives a warning, as | ||
169 | * exit() isn't defined as volatile under minix. | ||
170 | */ | ||
171 | volatile void fatal_error(const char * fmt_string,int status) | ||
172 | { | ||
173 | fprintf(stderr,fmt_string,program_name,device_name); | ||
174 | exit(status); | ||
175 | } | ||
176 | |||
177 | volatile void die(char *str) { | ||
178 | fprintf(stderr, "%s: %s\n", program_name, str); | ||
179 | exit(8); | ||
180 | } | ||
181 | |||
182 | volatile void usage() | ||
183 | { | ||
184 | fprintf(stderr, "%s (%s)\n", program_name, util_linux_version); | ||
185 | fprintf(stderr, | ||
186 | _("Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n"), | ||
187 | program_name); | ||
188 | exit(16); | ||
189 | } | ||
190 | |||
191 | /* | ||
192 | * Check to make certain that our new filesystem won't be created on | ||
193 | * an already mounted partition. Code adapted from mke2fs, Copyright | ||
194 | * (C) 1994 Theodore Ts'o. Also licensed under GPL. | ||
195 | */ | ||
196 | static void check_mount(void) | ||
197 | { | ||
198 | FILE * f; | ||
199 | struct mntent * mnt; | ||
200 | |||
201 | if ((f = setmntent (MOUNTED, "r")) == NULL) | ||
202 | return; | ||
203 | while ((mnt = getmntent (f)) != NULL) | ||
204 | if (strcmp (device_name, mnt->mnt_fsname) == 0) | ||
205 | break; | ||
206 | endmntent (f); | ||
207 | if (!mnt) | ||
208 | return; | ||
209 | |||
210 | die(_("%s is mounted; will not make a filesystem here!")); | ||
211 | } | ||
212 | |||
213 | static long valid_offset (int fd, int offset) | ||
214 | { | ||
215 | char ch; | ||
216 | |||
217 | if (lseek (fd, offset, 0) < 0) | ||
218 | return 0; | ||
219 | if (read (fd, &ch, 1) < 1) | ||
220 | return 0; | ||
221 | return 1; | ||
222 | } | ||
223 | |||
224 | static int count_blocks (int fd) | ||
225 | { | ||
226 | int high, low; | ||
227 | |||
228 | low = 0; | ||
229 | for (high = 1; valid_offset (fd, high); high *= 2) | ||
230 | low = high; | ||
231 | while (low < high - 1) | ||
232 | { | ||
233 | const int mid = (low + high) / 2; | ||
234 | |||
235 | if (valid_offset (fd, mid)) | ||
236 | low = mid; | ||
237 | else | ||
238 | high = mid; | ||
239 | } | ||
240 | valid_offset (fd, 0); | ||
241 | return (low + 1); | ||
242 | } | ||
243 | |||
244 | static int get_size(const char *file) | ||
245 | { | ||
246 | int fd; | ||
247 | long size; | ||
248 | |||
249 | fd = open(file, O_RDWR); | ||
250 | if (fd < 0) { | ||
251 | perror(file); | ||
252 | exit(1); | ||
253 | } | ||
254 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) { | ||
255 | close(fd); | ||
256 | return (size * 512); | ||
257 | } | ||
258 | |||
259 | size = count_blocks(fd); | ||
260 | close(fd); | ||
261 | return size; | ||
262 | } | ||
263 | |||
264 | void write_tables(void) | ||
265 | { | ||
266 | /* Mark the super block valid. */ | ||
267 | Super.s_state |= MINIX_VALID_FS; | ||
268 | Super.s_state &= ~MINIX_ERROR_FS; | ||
269 | |||
270 | if (lseek(DEV, 0, SEEK_SET)) | ||
271 | die(_("seek to boot block failed in write_tables")); | ||
272 | if (512 != write(DEV, boot_block_buffer, 512)) | ||
273 | die(_("unable to clear boot sector")); | ||
274 | if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET)) | ||
275 | die(_("seek failed in write_tables")); | ||
276 | if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE)) | ||
277 | die(_("unable to write super-block")); | ||
278 | if (IMAPS*BLOCK_SIZE != write(DEV,inode_map,IMAPS*BLOCK_SIZE)) | ||
279 | die(_("unable to write inode map")); | ||
280 | if (ZMAPS*BLOCK_SIZE != write(DEV,zone_map,ZMAPS*BLOCK_SIZE)) | ||
281 | die(_("unable to write zone map")); | ||
282 | if (INODE_BUFFER_SIZE != write(DEV,inode_buffer,INODE_BUFFER_SIZE)) | ||
283 | die(_("unable to write inodes")); | ||
284 | |||
285 | } | ||
286 | |||
287 | void write_block(int blk, char * buffer) | ||
288 | { | ||
289 | if (blk*BLOCK_SIZE != lseek(DEV, blk*BLOCK_SIZE, SEEK_SET)) | ||
290 | die(_("seek failed in write_block")); | ||
291 | if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE)) | ||
292 | die(_("write failed in write_block")); | ||
293 | } | ||
294 | |||
295 | int get_free_block(void) | ||
296 | { | ||
297 | int blk; | ||
298 | |||
299 | if (used_good_blocks+1 >= MAX_GOOD_BLOCKS) | ||
300 | die(_("too many bad blocks")); | ||
301 | if (used_good_blocks) | ||
302 | blk = good_blocks_table[used_good_blocks-1]+1; | ||
303 | else | ||
304 | blk = FIRSTZONE; | ||
305 | while (blk < ZONES && zone_in_use(blk)) | ||
306 | blk++; | ||
307 | if (blk >= ZONES) | ||
308 | die(_("not enough good blocks")); | ||
309 | good_blocks_table[used_good_blocks] = blk; | ||
310 | used_good_blocks++; | ||
311 | return blk; | ||
312 | } | ||
313 | |||
314 | void mark_good_blocks(void) | ||
315 | { | ||
316 | int blk; | ||
317 | |||
318 | for (blk=0 ; blk < used_good_blocks ; blk++) | ||
319 | mark_zone(good_blocks_table[blk]); | ||
320 | } | ||
321 | |||
322 | inline int next(int zone) | ||
323 | { | ||
324 | if (!zone) | ||
325 | zone = FIRSTZONE-1; | ||
326 | while (++zone < ZONES) | ||
327 | if (zone_in_use(zone)) | ||
328 | return zone; | ||
329 | return 0; | ||
330 | } | ||
331 | |||
332 | void make_bad_inode(void) | ||
333 | { | ||
334 | struct minix_inode * inode = &Inode[MINIX_BAD_INO]; | ||
335 | int i,j,zone; | ||
336 | int ind=0,dind=0; | ||
337 | unsigned short ind_block[BLOCK_SIZE>>1]; | ||
338 | unsigned short dind_block[BLOCK_SIZE>>1]; | ||
339 | |||
340 | #define NEXT_BAD (zone = next(zone)) | ||
341 | |||
342 | if (!badblocks) | ||
343 | return; | ||
344 | mark_inode(MINIX_BAD_INO); | ||
345 | inode->i_nlinks = 1; | ||
346 | inode->i_time = time(NULL); | ||
347 | inode->i_mode = S_IFREG + 0000; | ||
348 | inode->i_size = badblocks*BLOCK_SIZE; | ||
349 | zone = next(0); | ||
350 | for (i=0 ; i<7 ; i++) { | ||
351 | inode->i_zone[i] = zone; | ||
352 | if (!NEXT_BAD) | ||
353 | goto end_bad; | ||
354 | } | ||
355 | inode->i_zone[7] = ind = get_free_block(); | ||
356 | memset(ind_block,0,BLOCK_SIZE); | ||
357 | for (i=0 ; i<512 ; i++) { | ||
358 | ind_block[i] = zone; | ||
359 | if (!NEXT_BAD) | ||
360 | goto end_bad; | ||
361 | } | ||
362 | inode->i_zone[8] = dind = get_free_block(); | ||
363 | memset(dind_block,0,BLOCK_SIZE); | ||
364 | for (i=0 ; i<512 ; i++) { | ||
365 | write_block(ind,(char *) ind_block); | ||
366 | dind_block[i] = ind = get_free_block(); | ||
367 | memset(ind_block,0,BLOCK_SIZE); | ||
368 | for (j=0 ; j<512 ; j++) { | ||
369 | ind_block[j] = zone; | ||
370 | if (!NEXT_BAD) | ||
371 | goto end_bad; | ||
372 | } | ||
373 | } | ||
374 | die(_("too many bad blocks")); | ||
375 | end_bad: | ||
376 | if (ind) | ||
377 | write_block(ind, (char *) ind_block); | ||
378 | if (dind) | ||
379 | write_block(dind, (char *) dind_block); | ||
380 | } | ||
381 | |||
382 | #ifdef HAVE_MINIX2 | ||
383 | void | ||
384 | make_bad_inode2 (void) | ||
385 | { | ||
386 | struct minix2_inode *inode = &Inode2[MINIX_BAD_INO]; | ||
387 | int i, j, zone; | ||
388 | int ind = 0, dind = 0; | ||
389 | unsigned long ind_block[BLOCK_SIZE >> 2]; | ||
390 | unsigned long dind_block[BLOCK_SIZE >> 2]; | ||
391 | |||
392 | if (!badblocks) | ||
393 | return; | ||
394 | mark_inode (MINIX_BAD_INO); | ||
395 | inode->i_nlinks = 1; | ||
396 | inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL); | ||
397 | inode->i_mode = S_IFREG + 0000; | ||
398 | inode->i_size = badblocks * BLOCK_SIZE; | ||
399 | zone = next (0); | ||
400 | for (i = 0; i < 7; i++) { | ||
401 | inode->i_zone[i] = zone; | ||
402 | if (!NEXT_BAD) | ||
403 | goto end_bad; | ||
404 | } | ||
405 | inode->i_zone[7] = ind = get_free_block (); | ||
406 | memset (ind_block, 0, BLOCK_SIZE); | ||
407 | for (i = 0; i < 256; i++) { | ||
408 | ind_block[i] = zone; | ||
409 | if (!NEXT_BAD) | ||
410 | goto end_bad; | ||
411 | } | ||
412 | inode->i_zone[8] = dind = get_free_block (); | ||
413 | memset (dind_block, 0, BLOCK_SIZE); | ||
414 | for (i = 0; i < 256; i++) { | ||
415 | write_block (ind, (char *) ind_block); | ||
416 | dind_block[i] = ind = get_free_block (); | ||
417 | memset (ind_block, 0, BLOCK_SIZE); | ||
418 | for (j = 0; j < 256; j++) { | ||
419 | ind_block[j] = zone; | ||
420 | if (!NEXT_BAD) | ||
421 | goto end_bad; | ||
422 | } | ||
423 | } | ||
424 | /* Could make triple indirect block here */ | ||
425 | die (_("too many bad blocks")); | ||
426 | end_bad: | ||
427 | if (ind) | ||
428 | write_block (ind, (char *) ind_block); | ||
429 | if (dind) | ||
430 | write_block (dind, (char *) dind_block); | ||
431 | } | ||
432 | #endif | ||
433 | |||
434 | void make_root_inode(void) | ||
435 | { | ||
436 | struct minix_inode * inode = &Inode[MINIX_ROOT_INO]; | ||
437 | |||
438 | mark_inode(MINIX_ROOT_INO); | ||
439 | inode->i_zone[0] = get_free_block(); | ||
440 | inode->i_nlinks = 2; | ||
441 | inode->i_time = time(NULL); | ||
442 | if (badblocks) | ||
443 | inode->i_size = 3*dirsize; | ||
444 | else { | ||
445 | root_block[2*dirsize] = '\0'; | ||
446 | root_block[2*dirsize+1] = '\0'; | ||
447 | inode->i_size = 2*dirsize; | ||
448 | } | ||
449 | inode->i_mode = S_IFDIR + 0755; | ||
450 | inode->i_uid = getuid(); | ||
451 | if (inode->i_uid) | ||
452 | inode->i_gid = getgid(); | ||
453 | write_block(inode->i_zone[0],root_block); | ||
454 | } | ||
455 | |||
456 | #ifdef HAVE_MINIX2 | ||
457 | void | ||
458 | make_root_inode2 (void) | ||
459 | { | ||
460 | struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO]; | ||
461 | |||
462 | mark_inode (MINIX_ROOT_INO); | ||
463 | inode->i_zone[0] = get_free_block (); | ||
464 | inode->i_nlinks = 2; | ||
465 | inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL); | ||
466 | if (badblocks) | ||
467 | inode->i_size = 3 * dirsize; | ||
468 | else { | ||
469 | root_block[2 * dirsize] = '\0'; | ||
470 | root_block[2 * dirsize + 1] = '\0'; | ||
471 | inode->i_size = 2 * dirsize; | ||
472 | } | ||
473 | inode->i_mode = S_IFDIR + 0755; | ||
474 | inode->i_uid = getuid(); | ||
475 | if (inode->i_uid) | ||
476 | inode->i_gid = getgid(); | ||
477 | write_block (inode->i_zone[0], root_block); | ||
478 | } | ||
479 | #endif | ||
480 | |||
481 | void setup_tables(void) | ||
482 | { | ||
483 | int i; | ||
484 | unsigned long inodes; | ||
485 | |||
486 | memset(super_block_buffer,0,BLOCK_SIZE); | ||
487 | memset(boot_block_buffer,0,512); | ||
488 | MAGIC = magic; | ||
489 | ZONESIZE = 0; | ||
490 | MAXSIZE = version2 ? 0x7fffffff : (7+512+512*512)*1024; | ||
491 | ZONES = BLOCKS; | ||
492 | /* some magic nrs: 1 inode / 3 blocks */ | ||
493 | if ( req_nr_inodes == 0 ) | ||
494 | inodes = BLOCKS/3; | ||
495 | else | ||
496 | inodes = req_nr_inodes; | ||
497 | /* Round up inode count to fill block size */ | ||
498 | #ifdef HAVE_MINIX2 | ||
499 | if (version2) | ||
500 | inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) & | ||
501 | ~(MINIX2_INODES_PER_BLOCK - 1)); | ||
502 | else | ||
503 | #endif | ||
504 | inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) & | ||
505 | ~(MINIX_INODES_PER_BLOCK - 1)); | ||
506 | if (inodes > 65535) | ||
507 | inodes = 65535; | ||
508 | INODES = inodes; | ||
509 | IMAPS = UPPER(INODES + 1,BITS_PER_BLOCK); | ||
510 | ZMAPS = 0; | ||
511 | i=0; | ||
512 | while (ZMAPS != UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK) && i<1000) { | ||
513 | ZMAPS = UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK); | ||
514 | i++; | ||
515 | } | ||
516 | /* Real bad hack but overwise mkfs.minix can be thrown | ||
517 | * in infinite loop... | ||
518 | * try: | ||
519 | * dd if=/dev/zero of=test.fs count=10 bs=1024 | ||
520 | * /sbin/mkfs.minix -i 200 test.fs | ||
521 | * */ | ||
522 | if (i>=999) { | ||
523 | die ("unable to allocate buffers for maps"); | ||
524 | } | ||
525 | FIRSTZONE = NORM_FIRSTZONE; | ||
526 | inode_map = malloc(IMAPS * BLOCK_SIZE); | ||
527 | zone_map = malloc(ZMAPS * BLOCK_SIZE); | ||
528 | if (!inode_map || !zone_map) | ||
529 | die(_("unable to allocate buffers for maps")); | ||
530 | memset(inode_map,0xff,IMAPS * BLOCK_SIZE); | ||
531 | memset(zone_map,0xff,ZMAPS * BLOCK_SIZE); | ||
532 | for (i = FIRSTZONE ; i<ZONES ; i++) | ||
533 | unmark_zone(i); | ||
534 | for (i = MINIX_ROOT_INO ; i<=INODES ; i++) | ||
535 | unmark_inode(i); | ||
536 | inode_buffer = malloc(INODE_BUFFER_SIZE); | ||
537 | if (!inode_buffer) | ||
538 | die(_("unable to allocate buffer for inodes")); | ||
539 | memset(inode_buffer,0,INODE_BUFFER_SIZE); | ||
540 | printf(_("%ld inodes\n"),INODES); | ||
541 | printf(_("%ld blocks\n"),ZONES); | ||
542 | printf(_("Firstdatazone=%ld (%ld)\n"),FIRSTZONE,NORM_FIRSTZONE); | ||
543 | printf(_("Zonesize=%d\n"),BLOCK_SIZE<<ZONESIZE); | ||
544 | printf(_("Maxsize=%ld\n\n"),MAXSIZE); | ||
545 | } | ||
546 | |||
547 | /* | ||
548 | * Perform a test of a block; return the number of | ||
549 | * blocks readable/writeable. | ||
550 | */ | ||
551 | long do_check(char * buffer, int try, unsigned int current_block) | ||
552 | { | ||
553 | long got; | ||
554 | |||
555 | /* Seek to the correct loc. */ | ||
556 | if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) != | ||
557 | current_block * BLOCK_SIZE ) { | ||
558 | die(_("seek failed during testing of blocks")); | ||
559 | } | ||
560 | |||
561 | |||
562 | /* Try the read */ | ||
563 | got = read(DEV, buffer, try * BLOCK_SIZE); | ||
564 | if (got < 0) got = 0; | ||
565 | if (got & (BLOCK_SIZE - 1 )) { | ||
566 | printf(_("Weird values in do_check: probably bugs\n")); | ||
567 | } | ||
568 | got /= BLOCK_SIZE; | ||
569 | return got; | ||
570 | } | ||
571 | |||
572 | static unsigned int currently_testing = 0; | ||
573 | |||
574 | void alarm_intr(int alnum) | ||
575 | { | ||
576 | if (currently_testing >= ZONES) | ||
577 | return; | ||
578 | signal(SIGALRM,alarm_intr); | ||
579 | alarm(5); | ||
580 | if (!currently_testing) | ||
581 | return; | ||
582 | printf("%d ...", currently_testing); | ||
583 | fflush(stdout); | ||
584 | } | ||
585 | |||
586 | void check_blocks(void) | ||
587 | { | ||
588 | int try,got; | ||
589 | static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS]; | ||
590 | |||
591 | currently_testing=0; | ||
592 | signal(SIGALRM,alarm_intr); | ||
593 | alarm(5); | ||
594 | while (currently_testing < ZONES) { | ||
595 | if (lseek(DEV,currently_testing*BLOCK_SIZE,SEEK_SET) != | ||
596 | currently_testing*BLOCK_SIZE) | ||
597 | die(_("seek failed in check_blocks")); | ||
598 | try = TEST_BUFFER_BLOCKS; | ||
599 | if (currently_testing + try > ZONES) | ||
600 | try = ZONES-currently_testing; | ||
601 | got = do_check(buffer, try, currently_testing); | ||
602 | currently_testing += got; | ||
603 | if (got == try) | ||
604 | continue; | ||
605 | if (currently_testing < FIRSTZONE) | ||
606 | die(_("bad blocks before data-area: cannot make fs")); | ||
607 | mark_zone(currently_testing); | ||
608 | badblocks++; | ||
609 | currently_testing++; | ||
610 | } | ||
611 | if (badblocks > 1) | ||
612 | printf(_("%d bad blocks\n"), badblocks); | ||
613 | else if (badblocks == 1) | ||
614 | printf(_("one bad block\n")); | ||
615 | } | ||
616 | |||
617 | void get_list_blocks(filename) | ||
618 | char *filename; | ||
619 | |||
620 | { | ||
621 | FILE *listfile; | ||
622 | unsigned long blockno; | ||
623 | |||
624 | listfile=fopen(filename,"r"); | ||
625 | if(listfile == (FILE *)NULL) { | ||
626 | die(_("can't open file of bad blocks")); | ||
627 | } | ||
628 | while(!feof(listfile)) { | ||
629 | fscanf(listfile,"%ld\n", &blockno); | ||
630 | mark_zone(blockno); | ||
631 | badblocks++; | ||
632 | } | ||
633 | if(badblocks > 1) | ||
634 | printf(_("%d bad blocks\n"), badblocks); | ||
635 | else if (badblocks == 1) | ||
636 | printf(_("one bad block\n")); | ||
637 | } | ||
638 | |||
639 | extern int | ||
640 | mkfs_minix_main(int argc, char ** argv) | ||
641 | { | ||
642 | int i; | ||
643 | char * tmp; | ||
644 | struct stat statbuf; | ||
645 | char * listfile = NULL; | ||
646 | |||
647 | if (argc && *argv) | ||
648 | program_name = *argv; | ||
649 | if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) | ||
650 | die(_("bad inode size")); | ||
651 | #ifdef HAVE_MINIX2 | ||
652 | if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) | ||
653 | die(_("bad inode size")); | ||
654 | #endif | ||
655 | opterr = 0; | ||
656 | while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF) | ||
657 | switch (i) { | ||
658 | case 'c': | ||
659 | check=1; break; | ||
660 | case 'i': | ||
661 | req_nr_inodes = (unsigned long) atol(optarg); | ||
662 | break; | ||
663 | case 'l': | ||
664 | listfile = optarg; break; | ||
665 | case 'n': | ||
666 | i = strtoul(optarg,&tmp,0); | ||
667 | if (*tmp) | ||
668 | usage(); | ||
669 | if (i == 14) | ||
670 | magic = MINIX_SUPER_MAGIC; | ||
671 | else if (i == 30) | ||
672 | magic = MINIX_SUPER_MAGIC2; | ||
673 | else | ||
674 | usage(); | ||
675 | namelen = i; | ||
676 | dirsize = i+2; | ||
677 | break; | ||
678 | case 'v': | ||
679 | #ifdef HAVE_MINIX2 | ||
680 | version2 = 1; | ||
681 | #else | ||
682 | fatal_error(_("%s: not compiled with minix v2 support\n"),-1); | ||
683 | #endif | ||
684 | break; | ||
685 | default: | ||
686 | usage(); | ||
687 | } | ||
688 | argc -= optind; | ||
689 | argv += optind; | ||
690 | if (argc > 0 && !device_name) { | ||
691 | device_name = argv[0]; | ||
692 | argc--; | ||
693 | argv++; | ||
694 | } | ||
695 | if (argc > 0) { | ||
696 | BLOCKS = strtol(argv[0],&tmp,0); | ||
697 | if (*tmp) { | ||
698 | printf(_("strtol error: number of blocks not specified")); | ||
699 | usage(); | ||
700 | } | ||
701 | } | ||
702 | |||
703 | if (device_name && !BLOCKS) | ||
704 | BLOCKS = get_size (device_name) / 1024; | ||
705 | if (!device_name || BLOCKS<10) { | ||
706 | usage(); | ||
707 | } | ||
708 | #ifdef HAVE_MINIX2 | ||
709 | if (version2) { | ||
710 | if (namelen == 14) | ||
711 | magic = MINIX2_SUPER_MAGIC; | ||
712 | else | ||
713 | magic = MINIX2_SUPER_MAGIC2; | ||
714 | } else | ||
715 | #endif | ||
716 | if (BLOCKS > 65535) | ||
717 | BLOCKS = 65535; | ||
718 | check_mount(); /* is it already mounted? */ | ||
719 | tmp = root_block; | ||
720 | *(short *)tmp = 1; | ||
721 | strcpy(tmp+2,"."); | ||
722 | tmp += dirsize; | ||
723 | *(short *)tmp = 1; | ||
724 | strcpy(tmp+2,".."); | ||
725 | tmp += dirsize; | ||
726 | *(short *)tmp = 2; | ||
727 | strcpy(tmp+2,".badblocks"); | ||
728 | DEV = open(device_name,O_RDWR ); | ||
729 | if (DEV<0) | ||
730 | die(_("unable to open %s")); | ||
731 | if (fstat(DEV,&statbuf)<0) | ||
732 | die(_("unable to stat %s")); | ||
733 | if (!S_ISBLK(statbuf.st_mode)) | ||
734 | check=0; | ||
735 | else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) | ||
736 | die(_("will not try to make filesystem on '%s'")); | ||
737 | setup_tables(); | ||
738 | if (check) | ||
739 | check_blocks(); | ||
740 | else if (listfile) | ||
741 | get_list_blocks(listfile); | ||
742 | #ifdef HAVE_MINIX2 | ||
743 | if (version2) { | ||
744 | make_root_inode2 (); | ||
745 | make_bad_inode2 (); | ||
746 | } else | ||
747 | #endif | ||
748 | { | ||
749 | make_root_inode(); | ||
750 | make_bad_inode(); | ||
751 | } | ||
752 | mark_good_blocks(); | ||
753 | write_tables(); | ||
754 | return 0; | ||
755 | } | ||