9b40faac666e4e18a9d80eb48a251ccb4fcd039e
[oweals/busybox.git] / util-linux / mkfs_minix.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * mkfs.c - make a linux (minix) file-system.
4  *
5  * (C) 1991 Linus Torvalds. This file may be redistributed as per
6  * the Linux copyright.
7  */
8
9 /*
10  * DD.MM.YY
11  *
12  * 24.11.91  -  Time began. Used the fsck sources to get started.
13  *
14  * 25.11.91  -  Corrected some bugs. Added support for ".badblocks"
15  *              The algorithm for ".badblocks" is a bit weird, but
16  *              it should work. Oh, well.
17  *
18  * 25.01.92  -  Added the -l option for getting the list of bad blocks
19  *              out of a named file. (Dave Rivers, rivers@ponds.uucp)
20  *
21  * 28.02.92  -  Added %-information when using -c.
22  *
23  * 28.02.93  -  Added support for other namelengths than the original
24  *              14 characters so that I can test the new kernel routines..
25  *
26  * 09.10.93  -  Make exit status conform to that required by fsutil
27  *              (Rik Faith, faith@cs.unc.edu)
28  *
29  * 31.10.93  -  Added inode request feature, for backup floppies: use
30  *              32 inodes, for a news partition use more.
31  *              (Scott Heavner, sdh@po.cwru.edu)
32  *
33  * 03.01.94  -  Added support for file system valid flag.
34  *              (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu)
35  *
36  * 30.10.94 - added support for v2 filesystem
37  *            (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
38  * 
39  * 09.11.94  -  Added test to prevent overwrite of mounted fs adapted
40  *              from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs
41  *              program.  (Daniel Quinlan, quinlan@yggdrasil.com)
42  *
43  * 03.20.95  -  Clear first 512 bytes of filesystem to make certain that
44  *              the filesystem is not misidentified as a MS-DOS FAT filesystem.
45  *              (Daniel Quinlan, quinlan@yggdrasil.com)
46  *
47  * 02.07.96  -  Added small patch from Russell King to make the program a
48  *              good deal more portable (janl@math.uio.no)
49  *
50  * Usage:  mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks]
51  *
52  *      -c for readablility checking (SLOW!)
53  *      -l for getting a list of bad blocks from a file.
54  *      -n for namelength (currently the kernel only uses 14 or 30)
55  *      -i for number of inodes
56  *      -v for v2 filesystem
57  *
58  * The device may be a block device or a image of one, but this isn't
59  * enforced (but it's not much fun on a character device :-). 
60  *
61  * Modified for BusyBox by Erik Andersen <andersen@debian.org> --
62  *      removed getopt based parser and added a hand rolled one.
63  */
64
65 #include "busybox.h"
66 #include <stdio.h>
67 #include <time.h>
68 #include <unistd.h>
69 #include <string.h>
70 #include <signal.h>
71 #include <fcntl.h>
72 #include <ctype.h>
73 #include <stdlib.h>
74 #include <termios.h>
75 #include <sys/ioctl.h>
76 #include <sys/param.h>
77 #include <mntent.h>
78
79
80 typedef unsigned char u8;
81 typedef unsigned short u16;
82 typedef unsigned int u32;
83
84
85 #define MINIX_ROOT_INO 1
86 #define MINIX_LINK_MAX  250
87 #define MINIX2_LINK_MAX 65530
88
89 #define MINIX_I_MAP_SLOTS       8
90 #define MINIX_Z_MAP_SLOTS       64
91 #define MINIX_SUPER_MAGIC       0x137F          /* original minix fs */
92 #define MINIX_SUPER_MAGIC2      0x138F          /* minix fs, 30 char names */
93 #define MINIX2_SUPER_MAGIC      0x2468          /* minix V2 fs */
94 #define MINIX2_SUPER_MAGIC2     0x2478          /* minix V2 fs, 30 char names */
95 #define MINIX_VALID_FS          0x0001          /* Clean fs. */
96 #define MINIX_ERROR_FS          0x0002          /* fs has errors. */
97
98 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
99 #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
100
101 #define MINIX_V1                0x0001          /* original minix fs */
102 #define MINIX_V2                0x0002          /* minix V2 fs */
103
104 #define INODE_VERSION(inode)    inode->i_sb->u.minix_sb.s_version
105
106 /*
107  * This is the original minix inode layout on disk.
108  * Note the 8-bit gid and atime and ctime.
109  */
110 struct minix_inode {
111         u16 i_mode;
112         u16 i_uid;
113         u32 i_size;
114         u32 i_time;
115         u8  i_gid;
116         u8  i_nlinks;
117         u16 i_zone[9];
118 };
119
120 /*
121  * The new minix inode has all the time entries, as well as
122  * long block numbers and a third indirect block (7+1+1+1
123  * instead of 7+1+1). Also, some previously 8-bit values are
124  * now 16-bit. The inode is now 64 bytes instead of 32.
125  */
126 struct minix2_inode {
127         u16 i_mode;
128         u16 i_nlinks;
129         u16 i_uid;
130         u16 i_gid;
131         u32 i_size;
132         u32 i_atime;
133         u32 i_mtime;
134         u32 i_ctime;
135         u32 i_zone[10];
136 };
137
138 /*
139  * minix super-block data on disk
140  */
141 struct minix_super_block {
142         u16 s_ninodes;
143         u16 s_nzones;
144         u16 s_imap_blocks;
145         u16 s_zmap_blocks;
146         u16 s_firstdatazone;
147         u16 s_log_zone_size;
148         u32 s_max_size;
149         u16 s_magic;
150         u16 s_state;
151         u32 s_zones;
152 };
153
154 struct minix_dir_entry {
155         u16 inode;
156         char name[0];
157 };
158
159 #define BLOCK_SIZE_BITS 10
160 #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
161
162 #define NAME_MAX         255   /* # chars in a file name */
163
164 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
165
166 #define MINIX_VALID_FS               0x0001          /* Clean fs. */
167 #define MINIX_ERROR_FS               0x0002          /* fs has errors. */
168
169 #define MINIX_SUPER_MAGIC    0x137F          /* original minix fs */
170 #define MINIX_SUPER_MAGIC2   0x138F          /* minix fs, 30 char names */
171
172 #ifndef BLKGETSIZE
173 #define BLKGETSIZE _IO(0x12,96)    /* return device size */
174 #endif
175
176
177 #ifndef __linux__
178 #define volatile
179 #endif
180
181 #define MINIX_ROOT_INO 1
182 #define MINIX_BAD_INO 2
183
184 #define TEST_BUFFER_BLOCKS 16
185 #define MAX_GOOD_BLOCKS 512
186
187 #define UPPER(size,n) (((size)+((n)-1))/(n))
188 #define INODE_SIZE (sizeof(struct minix_inode))
189 #ifdef BB_FEATURE_MINIX2
190 #define INODE_SIZE2 (sizeof(struct minix2_inode))
191 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
192                                     : MINIX_INODES_PER_BLOCK))
193 #else
194 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
195 #endif
196 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
197
198 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
199
200 static char *device_name = NULL;
201 static int DEV = -1;
202 static long BLOCKS = 0;
203 static int check = 0;
204 static int badblocks = 0;
205 static int namelen = 30;                /* default (changed to 30, per Linus's
206
207                                                                    suggestion, Sun Nov 21 08:05:07 1993) */
208 static int dirsize = 32;
209 static int magic = MINIX_SUPER_MAGIC2;
210 static int version2 = 0;
211
212 static char root_block[BLOCK_SIZE] = "\0";
213
214 static char *inode_buffer = NULL;
215
216 #define Inode (((struct minix_inode *) inode_buffer)-1)
217 #ifdef BB_FEATURE_MINIX2
218 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
219 #endif
220 static char super_block_buffer[BLOCK_SIZE];
221 static char boot_block_buffer[512];
222
223 #define Super (*(struct minix_super_block *)super_block_buffer)
224 #define INODES ((unsigned long)Super.s_ninodes)
225 #ifdef BB_FEATURE_MINIX2
226 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
227 #else
228 #define ZONES ((unsigned long)(Super.s_nzones))
229 #endif
230 #define IMAPS ((unsigned long)Super.s_imap_blocks)
231 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
232 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
233 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
234 #define MAXSIZE ((unsigned long)Super.s_max_size)
235 #define MAGIC (Super.s_magic)
236 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
237
238 static char *inode_map;
239 static char *zone_map;
240
241 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
242 static int used_good_blocks = 0;
243 static unsigned long req_nr_inodes = 0;
244
245 static inline int bit(char * a,unsigned int i)
246 {
247           return (a[i >> 3] & (1<<(i & 7))) != 0;
248 }
249 #define inode_in_use(x) (bit(inode_map,(x)))
250 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
251
252 #define mark_inode(x) (setbit(inode_map,(x)))
253 #define unmark_inode(x) (clrbit(inode_map,(x)))
254
255 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
256 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
257
258 static void show_usage() __attribute__ ((noreturn)); 
259
260 static void show_usage()
261 {
262         usage(mkfs_minix_usage);
263 }
264
265 /*
266  * Check to make certain that our new filesystem won't be created on
267  * an already mounted partition.  Code adapted from mke2fs, Copyright
268  * (C) 1994 Theodore Ts'o.  Also licensed under GPL.
269  */
270 static void check_mount(void)
271 {
272         FILE *f;
273         struct mntent *mnt;
274
275         if ((f = setmntent(MOUNTED, "r")) == NULL)
276                 return;
277         while ((mnt = getmntent(f)) != NULL)
278                 if (strcmp(device_name, mnt->mnt_fsname) == 0)
279                         break;
280         endmntent(f);
281         if (!mnt)
282                 return;
283
284         error_msg_and_die("%s is mounted; will not make a filesystem here!", device_name);
285 }
286
287 static long valid_offset(int fd, int offset)
288 {
289         char ch;
290
291         if (lseek(fd, offset, 0) < 0)
292                 return 0;
293         if (read(fd, &ch, 1) < 1)
294                 return 0;
295         return 1;
296 }
297
298 static int count_blocks(int fd)
299 {
300         int high, low;
301
302         low = 0;
303         for (high = 1; valid_offset(fd, high); high *= 2)
304                 low = high;
305         while (low < high - 1) {
306                 const int mid = (low + high) / 2;
307
308                 if (valid_offset(fd, mid))
309                         low = mid;
310                 else
311                         high = mid;
312         }
313         valid_offset(fd, 0);
314         return (low + 1);
315 }
316
317 static int get_size(const char *file)
318 {
319         int fd;
320         long size;
321
322         if ((fd = open(file, O_RDWR)) < 0)
323                 perror_msg_and_die("%s", file);
324         if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
325                 close(fd);
326                 return (size * 512);
327         }
328
329         size = count_blocks(fd);
330         close(fd);
331         return size;
332 }
333
334 static void write_tables(void)
335 {
336         /* Mark the super block valid. */
337         Super.s_state |= MINIX_VALID_FS;
338         Super.s_state &= ~MINIX_ERROR_FS;
339
340         if (lseek(DEV, 0, SEEK_SET))
341                 error_msg_and_die("seek to boot block failed in write_tables");
342         if (512 != write(DEV, boot_block_buffer, 512))
343                 error_msg_and_die("unable to clear boot sector");
344         if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
345                 error_msg_and_die("seek failed in write_tables");
346         if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
347                 error_msg_and_die("unable to write super-block");
348         if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
349                 error_msg_and_die("unable to write inode map");
350         if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
351                 error_msg_and_die("unable to write zone map");
352         if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
353                 error_msg_and_die("unable to write inodes");
354
355 }
356
357 static void write_block(int blk, char *buffer)
358 {
359         if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
360                 error_msg_and_die("seek failed in write_block");
361         if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
362                 error_msg_and_die("write failed in write_block");
363 }
364
365 static int get_free_block(void)
366 {
367         int blk;
368
369         if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
370                 error_msg_and_die("too many bad blocks");
371         if (used_good_blocks)
372                 blk = good_blocks_table[used_good_blocks - 1] + 1;
373         else
374                 blk = FIRSTZONE;
375         while (blk < ZONES && zone_in_use(blk))
376                 blk++;
377         if (blk >= ZONES)
378                 error_msg_and_die("not enough good blocks");
379         good_blocks_table[used_good_blocks] = blk;
380         used_good_blocks++;
381         return blk;
382 }
383
384 static void mark_good_blocks(void)
385 {
386         int blk;
387
388         for (blk = 0; blk < used_good_blocks; blk++)
389                 mark_zone(good_blocks_table[blk]);
390 }
391
392 inline int next(int zone)
393 {
394         if (!zone)
395                 zone = FIRSTZONE - 1;
396         while (++zone < ZONES)
397                 if (zone_in_use(zone))
398                         return zone;
399         return 0;
400 }
401
402 static void make_bad_inode(void)
403 {
404         struct minix_inode *inode = &Inode[MINIX_BAD_INO];
405         int i, j, zone;
406         int ind = 0, dind = 0;
407         unsigned short ind_block[BLOCK_SIZE >> 1];
408         unsigned short dind_block[BLOCK_SIZE >> 1];
409
410 #define NEXT_BAD (zone = next(zone))
411
412         if (!badblocks)
413                 return;
414         mark_inode(MINIX_BAD_INO);
415         inode->i_nlinks = 1;
416         inode->i_time = time(NULL);
417         inode->i_mode = S_IFREG + 0000;
418         inode->i_size = badblocks * BLOCK_SIZE;
419         zone = next(0);
420         for (i = 0; i < 7; i++) {
421                 inode->i_zone[i] = zone;
422                 if (!NEXT_BAD)
423                         goto end_bad;
424         }
425         inode->i_zone[7] = ind = get_free_block();
426         memset(ind_block, 0, BLOCK_SIZE);
427         for (i = 0; i < 512; i++) {
428                 ind_block[i] = zone;
429                 if (!NEXT_BAD)
430                         goto end_bad;
431         }
432         inode->i_zone[8] = dind = get_free_block();
433         memset(dind_block, 0, BLOCK_SIZE);
434         for (i = 0; i < 512; i++) {
435                 write_block(ind, (char *) ind_block);
436                 dind_block[i] = ind = get_free_block();
437                 memset(ind_block, 0, BLOCK_SIZE);
438                 for (j = 0; j < 512; j++) {
439                         ind_block[j] = zone;
440                         if (!NEXT_BAD)
441                                 goto end_bad;
442                 }
443         }
444         error_msg_and_die("too many bad blocks");
445   end_bad:
446         if (ind)
447                 write_block(ind, (char *) ind_block);
448         if (dind)
449                 write_block(dind, (char *) dind_block);
450 }
451
452 #ifdef BB_FEATURE_MINIX2
453 static void make_bad_inode2(void)
454 {
455         struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
456         int i, j, zone;
457         int ind = 0, dind = 0;
458         unsigned long ind_block[BLOCK_SIZE >> 2];
459         unsigned long dind_block[BLOCK_SIZE >> 2];
460
461         if (!badblocks)
462                 return;
463         mark_inode(MINIX_BAD_INO);
464         inode->i_nlinks = 1;
465         inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
466         inode->i_mode = S_IFREG + 0000;
467         inode->i_size = badblocks * BLOCK_SIZE;
468         zone = next(0);
469         for (i = 0; i < 7; i++) {
470                 inode->i_zone[i] = zone;
471                 if (!NEXT_BAD)
472                         goto end_bad;
473         }
474         inode->i_zone[7] = ind = get_free_block();
475         memset(ind_block, 0, BLOCK_SIZE);
476         for (i = 0; i < 256; i++) {
477                 ind_block[i] = zone;
478                 if (!NEXT_BAD)
479                         goto end_bad;
480         }
481         inode->i_zone[8] = dind = get_free_block();
482         memset(dind_block, 0, BLOCK_SIZE);
483         for (i = 0; i < 256; i++) {
484                 write_block(ind, (char *) ind_block);
485                 dind_block[i] = ind = get_free_block();
486                 memset(ind_block, 0, BLOCK_SIZE);
487                 for (j = 0; j < 256; j++) {
488                         ind_block[j] = zone;
489                         if (!NEXT_BAD)
490                                 goto end_bad;
491                 }
492         }
493         /* Could make triple indirect block here */
494         error_msg_and_die("too many bad blocks");
495   end_bad:
496         if (ind)
497                 write_block(ind, (char *) ind_block);
498         if (dind)
499                 write_block(dind, (char *) dind_block);
500 }
501 #endif
502
503 static void make_root_inode(void)
504 {
505         struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
506
507         mark_inode(MINIX_ROOT_INO);
508         inode->i_zone[0] = get_free_block();
509         inode->i_nlinks = 2;
510         inode->i_time = time(NULL);
511         if (badblocks)
512                 inode->i_size = 3 * dirsize;
513         else {
514                 root_block[2 * dirsize] = '\0';
515                 root_block[2 * dirsize + 1] = '\0';
516                 inode->i_size = 2 * dirsize;
517         }
518         inode->i_mode = S_IFDIR + 0755;
519         inode->i_uid = getuid();
520         if (inode->i_uid)
521                 inode->i_gid = getgid();
522         write_block(inode->i_zone[0], root_block);
523 }
524
525 #ifdef BB_FEATURE_MINIX2
526 static void make_root_inode2(void)
527 {
528         struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
529
530         mark_inode(MINIX_ROOT_INO);
531         inode->i_zone[0] = get_free_block();
532         inode->i_nlinks = 2;
533         inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
534         if (badblocks)
535                 inode->i_size = 3 * dirsize;
536         else {
537                 root_block[2 * dirsize] = '\0';
538                 root_block[2 * dirsize + 1] = '\0';
539                 inode->i_size = 2 * dirsize;
540         }
541         inode->i_mode = S_IFDIR + 0755;
542         inode->i_uid = getuid();
543         if (inode->i_uid)
544                 inode->i_gid = getgid();
545         write_block(inode->i_zone[0], root_block);
546 }
547 #endif
548
549 static void setup_tables(void)
550 {
551         int i;
552         unsigned long inodes;
553
554         memset(super_block_buffer, 0, BLOCK_SIZE);
555         memset(boot_block_buffer, 0, 512);
556         MAGIC = magic;
557         ZONESIZE = 0;
558         MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
559         ZONES = BLOCKS;
560 /* some magic nrs: 1 inode / 3 blocks */
561         if (req_nr_inodes == 0)
562                 inodes = BLOCKS / 3;
563         else
564                 inodes = req_nr_inodes;
565         /* Round up inode count to fill block size */
566 #ifdef BB_FEATURE_MINIX2
567         if (version2)
568                 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
569                                   ~(MINIX2_INODES_PER_BLOCK - 1));
570         else
571 #endif
572                 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
573                                   ~(MINIX_INODES_PER_BLOCK - 1));
574         if (inodes > 65535)
575                 inodes = 65535;
576         INODES = inodes;
577         IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
578         ZMAPS = 0;
579         i = 0;
580         while (ZMAPS !=
581                    UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
582                                  BITS_PER_BLOCK) && i < 1000) {
583                 ZMAPS =
584                         UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
585                                   BITS_PER_BLOCK);
586                 i++;
587         }
588         /* Real bad hack but overwise mkfs.minix can be thrown
589          * in infinite loop...
590          * try:
591          * dd if=/dev/zero of=test.fs count=10 bs=1024
592          * /sbin/mkfs.minix -i 200 test.fs
593          * */
594         if (i >= 999) {
595                 error_msg_and_die("unable to allocate buffers for maps");
596         }
597         FIRSTZONE = NORM_FIRSTZONE;
598         inode_map = xmalloc(IMAPS * BLOCK_SIZE);
599         zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
600         memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
601         memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
602         for (i = FIRSTZONE; i < ZONES; i++)
603                 unmark_zone(i);
604         for (i = MINIX_ROOT_INO; i <= INODES; i++)
605                 unmark_inode(i);
606         inode_buffer = xmalloc(INODE_BUFFER_SIZE);
607         memset(inode_buffer, 0, INODE_BUFFER_SIZE);
608         printf("%ld inodes\n", INODES);
609         printf("%ld blocks\n", ZONES);
610         printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
611         printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
612         printf("Maxsize=%ld\n\n", MAXSIZE);
613 }
614
615 /*
616  * Perform a test of a block; return the number of
617  * blocks readable/writeable.
618  */
619 long do_check(char *buffer, int try, unsigned int current_block)
620 {
621         long got;
622
623         /* Seek to the correct loc. */
624         if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
625                 current_block * BLOCK_SIZE) {
626                 error_msg_and_die("seek failed during testing of blocks");
627         }
628
629
630         /* Try the read */
631         got = read(DEV, buffer, try * BLOCK_SIZE);
632         if (got < 0)
633                 got = 0;
634         if (got & (BLOCK_SIZE - 1)) {
635                 printf("Weird values in do_check: probably bugs\n");
636         }
637         got /= BLOCK_SIZE;
638         return got;
639 }
640
641 static unsigned int currently_testing = 0;
642
643 static void alarm_intr(int alnum)
644 {
645         if (currently_testing >= ZONES)
646                 return;
647         signal(SIGALRM, alarm_intr);
648         alarm(5);
649         if (!currently_testing)
650                 return;
651         printf("%d ...", currently_testing);
652         fflush(stdout);
653 }
654
655 static void check_blocks(void)
656 {
657         int try, got;
658         static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
659
660         currently_testing = 0;
661         signal(SIGALRM, alarm_intr);
662         alarm(5);
663         while (currently_testing < ZONES) {
664                 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
665                         currently_testing * BLOCK_SIZE)
666                         error_msg_and_die("seek failed in check_blocks");
667                 try = TEST_BUFFER_BLOCKS;
668                 if (currently_testing + try > ZONES)
669                         try = ZONES - currently_testing;
670                 got = do_check(buffer, try, currently_testing);
671                 currently_testing += got;
672                 if (got == try)
673                         continue;
674                 if (currently_testing < FIRSTZONE)
675                         error_msg_and_die("bad blocks before data-area: cannot make fs");
676                 mark_zone(currently_testing);
677                 badblocks++;
678                 currently_testing++;
679         }
680         if (badblocks > 1)
681                 printf("%d bad blocks\n", badblocks);
682         else if (badblocks == 1)
683                 printf("one bad block\n");
684 }
685
686 static void get_list_blocks(filename)
687 char *filename;
688
689 {
690         FILE *listfile;
691         unsigned long blockno;
692
693         listfile = fopen(filename, "r");
694         if (listfile == (FILE *) NULL) {
695                 error_msg_and_die("can't open file of bad blocks");
696         }
697         while (!feof(listfile)) {
698                 fscanf(listfile, "%ld\n", &blockno);
699                 mark_zone(blockno);
700                 badblocks++;
701         }
702         if (badblocks > 1)
703                 printf("%d bad blocks\n", badblocks);
704         else if (badblocks == 1)
705                 printf("one bad block\n");
706 }
707
708 extern int mkfs_minix_main(int argc, char **argv)
709 {
710         int i=1;
711         char *tmp;
712         struct stat statbuf;
713         char *listfile = NULL;
714         int stopIt=FALSE;
715
716         if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
717                 error_msg_and_die("bad inode size");
718 #ifdef BB_FEATURE_MINIX2
719         if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
720                 error_msg_and_die("bad inode size");
721 #endif
722         
723         /* Parse options */
724         argv++;
725         while (--argc >= 0 && *argv && **argv) {
726                 if (**argv == '-') {
727                         stopIt=FALSE;
728                         while (i > 0 && *++(*argv) && stopIt==FALSE) {
729                                 switch (**argv) {
730                                         case 'c':
731                                                 check = 1;
732                                                 break;
733                                         case 'i':
734                                                 {
735                                                         char *cp=NULL;
736                                                         if (*(*argv+1) != 0) {
737                                                                 cp = ++(*argv);
738                                                         } else {
739                                                                 if (--argc == 0) {
740                                                                         goto goodbye;
741                                                                 }
742                                                                 cp = *(++argv);
743                                                         }
744                                                         req_nr_inodes = strtoul(cp, &tmp, 0);
745                                                         if (*tmp)
746                                                                 show_usage();
747                                                         stopIt=TRUE;
748                                                         break;
749                                                 }
750                                         case 'l':
751                                                 if (--argc == 0) {
752                                                         goto goodbye;
753                                                 }
754                                                 listfile = *(++argv);
755                                                 break;
756                                         case 'n':
757                                                 {
758                                                         char *cp=NULL;
759
760                                                         if (*(*argv+1) != 0) {
761                                                                 cp = ++(*argv);
762                                                         } else {
763                                                                 if (--argc == 0) {
764                                                                         goto goodbye;
765                                                                 }
766                                                                 cp = *(++argv);
767                                                         }
768                                                         i = strtoul(cp, &tmp, 0);
769                                                         if (*tmp)
770                                                                 show_usage();
771                                                         if (i == 14)
772                                                                 magic = MINIX_SUPER_MAGIC;
773                                                         else if (i == 30)
774                                                                 magic = MINIX_SUPER_MAGIC2;
775                                                         else 
776                                                                 show_usage();
777                                                         namelen = i;
778                                                         dirsize = i + 2;
779                                                         stopIt=TRUE;
780                                                         break;
781                                                 }
782                                         case 'v':
783 #ifdef BB_FEATURE_MINIX2
784                                                 version2 = 1;
785 #else
786                                                 error_msg("%s: not compiled with minix v2 support",
787                                                                 device_name);
788                                                 exit(-1);
789 #endif
790                                                 break;
791                                         case '-':
792                                         case 'h':
793                                         default:
794 goodbye:
795                                                 show_usage();
796                                 }
797                         }
798                 } else {
799                         if (device_name == NULL)
800                                 device_name = *argv;
801                         else if (BLOCKS == 0)
802                                 BLOCKS = strtol(*argv, &tmp, 0);
803                         else {
804                                 goto goodbye;
805                         }
806                 }
807                 argv++;
808         }
809
810         if (device_name && !BLOCKS)
811                 BLOCKS = get_size(device_name) / 1024;
812         if (!device_name || BLOCKS < 10) {
813                 show_usage();
814         }
815 #ifdef BB_FEATURE_MINIX2
816         if (version2) {
817                 if (namelen == 14)
818                         magic = MINIX2_SUPER_MAGIC;
819                 else
820                         magic = MINIX2_SUPER_MAGIC2;
821         } else
822 #endif
823         if (BLOCKS > 65535)
824                 BLOCKS = 65535;
825         check_mount();                          /* is it already mounted? */
826         tmp = root_block;
827         *(short *) tmp = 1;
828         strcpy(tmp + 2, ".");
829         tmp += dirsize;
830         *(short *) tmp = 1;
831         strcpy(tmp + 2, "..");
832         tmp += dirsize;
833         *(short *) tmp = 2;
834         strcpy(tmp + 2, ".badblocks");
835         DEV = open(device_name, O_RDWR);
836         if (DEV < 0)
837                 error_msg_and_die("unable to open %s", device_name);
838         if (fstat(DEV, &statbuf) < 0)
839                 error_msg_and_die("unable to stat %s", device_name);
840         if (!S_ISBLK(statbuf.st_mode))
841                 check = 0;
842         else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
843                 error_msg_and_die("will not try to make filesystem on '%s'", device_name);
844         setup_tables();
845         if (check)
846                 check_blocks();
847         else if (listfile)
848                 get_list_blocks(listfile);
849 #ifdef BB_FEATURE_MINIX2
850         if (version2) {
851                 make_root_inode2();
852                 make_bad_inode2();
853         } else
854 #endif
855         {
856                 make_root_inode();
857                 make_bad_inode();
858         }
859         mark_good_blocks();
860         write_tables();
861         return( 0);
862
863 }