]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/mkfs.minix.c
Imported from util-linux-2.10f tarball.
[thirdparty/util-linux.git] / disk-utils / mkfs.minix.c
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
80 #ifdef MINIX2_SUPER_MAGIC2
81 #define HAVE_MINIX2 1
82 #endif
83
84 #ifndef __GNUC__
85 #error "needs gcc for the bitop-__asm__'s"
86 #endif
87
88 #ifndef __linux__
89 #define volatile
90 #endif
91
92 #define MINIX_ROOT_INO 1
93 #define MINIX_BAD_INO 2
94
95 #define TEST_BUFFER_BLOCKS 16
96 #define MAX_GOOD_BLOCKS 512
97
98 #define UPPER(size,n) ((size+((n)-1))/(n))
99 #define INODE_SIZE (sizeof(struct minix_inode))
100 #ifdef HAVE_MINIX2
101 #define INODE_SIZE2 (sizeof(struct minix2_inode))
102 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
103 : MINIX_INODES_PER_BLOCK))
104 #else
105 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
106 #endif
107 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
108
109 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
110
111 static char * program_name = "mkfs";
112 static char * device_name = NULL;
113 static int DEV = -1;
114 static long BLOCKS = 0;
115 static int check = 0;
116 static int badblocks = 0;
117 static int namelen = 30; /* default (changed to 30, per Linus's
118 suggestion, Sun Nov 21 08:05:07 1993) */
119 static int dirsize = 32;
120 static int magic = MINIX_SUPER_MAGIC2;
121 static int version2 = 0;
122
123 static char root_block[BLOCK_SIZE] = "\0";
124
125 static char * inode_buffer = NULL;
126 #define Inode (((struct minix_inode *) inode_buffer)-1)
127 #ifdef HAVE_MINIX2
128 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
129 #endif
130 static char super_block_buffer[BLOCK_SIZE];
131 static char boot_block_buffer[512];
132 #define Super (*(struct minix_super_block *)super_block_buffer)
133 #define INODES ((unsigned long)Super.s_ninodes)
134 #ifdef HAVE_MINIX2
135 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
136 #else
137 #define ZONES ((unsigned long)(Super.s_nzones))
138 #endif
139 #define IMAPS ((unsigned long)Super.s_imap_blocks)
140 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
141 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
142 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
143 #define MAXSIZE ((unsigned long)Super.s_max_size)
144 #define MAGIC (Super.s_magic)
145 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
146
147 static char *inode_map;
148 static char *zone_map;
149
150 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
151 static int used_good_blocks = 0;
152 static unsigned long req_nr_inodes = 0;
153
154 #include "bitops.h"
155
156 #define inode_in_use(x) (bit(inode_map,(x)))
157 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
158
159 #define mark_inode(x) (setbit(inode_map,(x)))
160 #define unmark_inode(x) (clrbit(inode_map,(x)))
161
162 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
163 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
164
165 /*
166 * Volatile to let gcc know that this doesn't return. When trying
167 * to compile this under minix, volatile gives a warning, as
168 * exit() isn't defined as volatile under minix.
169 */
170 volatile void fatal_error(const char * fmt_string,int status)
171 {
172 fprintf(stderr,fmt_string,program_name,device_name);
173 exit(status);
174 }
175
176 volatile void die(char *str) {
177 fprintf(stderr, "%s: %s\n", program_name, str);
178 exit(8);
179 }
180
181 volatile void usage()
182 {
183 fprintf(stderr, "%s (%s)\n", program_name, util_linux_version);
184 fprintf(stderr,
185 _("Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n"),
186 program_name);
187 exit(16);
188 }
189
190 /*
191 * Check to make certain that our new filesystem won't be created on
192 * an already mounted partition. Code adapted from mke2fs, Copyright
193 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
194 */
195 static void check_mount(void)
196 {
197 FILE * f;
198 struct mntent * mnt;
199
200 if ((f = setmntent (MOUNTED, "r")) == NULL)
201 return;
202 while ((mnt = getmntent (f)) != NULL)
203 if (strcmp (device_name, mnt->mnt_fsname) == 0)
204 break;
205 endmntent (f);
206 if (!mnt)
207 return;
208
209 die(_("%s is mounted; will not make a filesystem here!"));
210 }
211
212 static long valid_offset (int fd, int offset)
213 {
214 char ch;
215
216 if (lseek (fd, offset, 0) < 0)
217 return 0;
218 if (read (fd, &ch, 1) < 1)
219 return 0;
220 return 1;
221 }
222
223 static int count_blocks (int fd)
224 {
225 int high, low;
226
227 low = 0;
228 for (high = 1; valid_offset (fd, high); high *= 2)
229 low = high;
230 while (low < high - 1)
231 {
232 const int mid = (low + high) / 2;
233
234 if (valid_offset (fd, mid))
235 low = mid;
236 else
237 high = mid;
238 }
239 valid_offset (fd, 0);
240 return (low + 1);
241 }
242
243 static int get_size(const char *file)
244 {
245 int fd;
246 long size;
247
248 fd = open(file, O_RDWR);
249 if (fd < 0) {
250 perror(file);
251 exit(1);
252 }
253 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
254 close(fd);
255 return (size * 512);
256 }
257
258 size = count_blocks(fd);
259 close(fd);
260 return size;
261 }
262
263 void write_tables(void)
264 {
265 /* Mark the super block valid. */
266 Super.s_state |= MINIX_VALID_FS;
267 Super.s_state &= ~MINIX_ERROR_FS;
268
269 if (lseek(DEV, 0, SEEK_SET))
270 die(_("seek to boot block failed in write_tables"));
271 if (512 != write(DEV, boot_block_buffer, 512))
272 die(_("unable to clear boot sector"));
273 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
274 die(_("seek failed in write_tables"));
275 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
276 die(_("unable to write super-block"));
277 if (IMAPS*BLOCK_SIZE != write(DEV,inode_map,IMAPS*BLOCK_SIZE))
278 die(_("unable to write inode map"));
279 if (ZMAPS*BLOCK_SIZE != write(DEV,zone_map,ZMAPS*BLOCK_SIZE))
280 die(_("unable to write zone map"));
281 if (INODE_BUFFER_SIZE != write(DEV,inode_buffer,INODE_BUFFER_SIZE))
282 die(_("unable to write inodes"));
283
284 }
285
286 void write_block(int blk, char * buffer)
287 {
288 if (blk*BLOCK_SIZE != lseek(DEV, blk*BLOCK_SIZE, SEEK_SET))
289 die(_("seek failed in write_block"));
290 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
291 die(_("write failed in write_block"));
292 }
293
294 int get_free_block(void)
295 {
296 int blk;
297
298 if (used_good_blocks+1 >= MAX_GOOD_BLOCKS)
299 die(_("too many bad blocks"));
300 if (used_good_blocks)
301 blk = good_blocks_table[used_good_blocks-1]+1;
302 else
303 blk = FIRSTZONE;
304 while (blk < ZONES && zone_in_use(blk))
305 blk++;
306 if (blk >= ZONES)
307 die(_("not enough good blocks"));
308 good_blocks_table[used_good_blocks] = blk;
309 used_good_blocks++;
310 return blk;
311 }
312
313 void mark_good_blocks(void)
314 {
315 int blk;
316
317 for (blk=0 ; blk < used_good_blocks ; blk++)
318 mark_zone(good_blocks_table[blk]);
319 }
320
321 inline int next(int zone)
322 {
323 if (!zone)
324 zone = FIRSTZONE-1;
325 while (++zone < ZONES)
326 if (zone_in_use(zone))
327 return zone;
328 return 0;
329 }
330
331 void make_bad_inode(void)
332 {
333 struct minix_inode * inode = &Inode[MINIX_BAD_INO];
334 int i,j,zone;
335 int ind=0,dind=0;
336 unsigned short ind_block[BLOCK_SIZE>>1];
337 unsigned short dind_block[BLOCK_SIZE>>1];
338
339 #define NEXT_BAD (zone = next(zone))
340
341 if (!badblocks)
342 return;
343 mark_inode(MINIX_BAD_INO);
344 inode->i_nlinks = 1;
345 inode->i_time = time(NULL);
346 inode->i_mode = S_IFREG + 0000;
347 inode->i_size = badblocks*BLOCK_SIZE;
348 zone = next(0);
349 for (i=0 ; i<7 ; i++) {
350 inode->i_zone[i] = zone;
351 if (!NEXT_BAD)
352 goto end_bad;
353 }
354 inode->i_zone[7] = ind = get_free_block();
355 memset(ind_block,0,BLOCK_SIZE);
356 for (i=0 ; i<512 ; i++) {
357 ind_block[i] = zone;
358 if (!NEXT_BAD)
359 goto end_bad;
360 }
361 inode->i_zone[8] = dind = get_free_block();
362 memset(dind_block,0,BLOCK_SIZE);
363 for (i=0 ; i<512 ; i++) {
364 write_block(ind,(char *) ind_block);
365 dind_block[i] = ind = get_free_block();
366 memset(ind_block,0,BLOCK_SIZE);
367 for (j=0 ; j<512 ; j++) {
368 ind_block[j] = zone;
369 if (!NEXT_BAD)
370 goto end_bad;
371 }
372 }
373 die(_("too many bad blocks"));
374 end_bad:
375 if (ind)
376 write_block(ind, (char *) ind_block);
377 if (dind)
378 write_block(dind, (char *) dind_block);
379 }
380
381 #ifdef HAVE_MINIX2
382 void
383 make_bad_inode2 (void)
384 {
385 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
386 int i, j, zone;
387 int ind = 0, dind = 0;
388 unsigned long ind_block[BLOCK_SIZE >> 2];
389 unsigned long dind_block[BLOCK_SIZE >> 2];
390
391 if (!badblocks)
392 return;
393 mark_inode (MINIX_BAD_INO);
394 inode->i_nlinks = 1;
395 inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL);
396 inode->i_mode = S_IFREG + 0000;
397 inode->i_size = badblocks * BLOCK_SIZE;
398 zone = next (0);
399 for (i = 0; i < 7; i++) {
400 inode->i_zone[i] = zone;
401 if (!NEXT_BAD)
402 goto end_bad;
403 }
404 inode->i_zone[7] = ind = get_free_block ();
405 memset (ind_block, 0, BLOCK_SIZE);
406 for (i = 0; i < 256; i++) {
407 ind_block[i] = zone;
408 if (!NEXT_BAD)
409 goto end_bad;
410 }
411 inode->i_zone[8] = dind = get_free_block ();
412 memset (dind_block, 0, BLOCK_SIZE);
413 for (i = 0; i < 256; i++) {
414 write_block (ind, (char *) ind_block);
415 dind_block[i] = ind = get_free_block ();
416 memset (ind_block, 0, BLOCK_SIZE);
417 for (j = 0; j < 256; j++) {
418 ind_block[j] = zone;
419 if (!NEXT_BAD)
420 goto end_bad;
421 }
422 }
423 /* Could make triple indirect block here */
424 die (_("too many bad blocks"));
425 end_bad:
426 if (ind)
427 write_block (ind, (char *) ind_block);
428 if (dind)
429 write_block (dind, (char *) dind_block);
430 }
431 #endif
432
433 void make_root_inode(void)
434 {
435 struct minix_inode * inode = &Inode[MINIX_ROOT_INO];
436
437 mark_inode(MINIX_ROOT_INO);
438 inode->i_zone[0] = get_free_block();
439 inode->i_nlinks = 2;
440 inode->i_time = time(NULL);
441 if (badblocks)
442 inode->i_size = 3*dirsize;
443 else {
444 root_block[2*dirsize] = '\0';
445 root_block[2*dirsize+1] = '\0';
446 inode->i_size = 2*dirsize;
447 }
448 inode->i_mode = S_IFDIR + 0755;
449 inode->i_uid = getuid();
450 if (inode->i_uid)
451 inode->i_gid = getgid();
452 write_block(inode->i_zone[0],root_block);
453 }
454
455 #ifdef HAVE_MINIX2
456 void
457 make_root_inode2 (void)
458 {
459 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
460
461 mark_inode (MINIX_ROOT_INO);
462 inode->i_zone[0] = get_free_block ();
463 inode->i_nlinks = 2;
464 inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL);
465 if (badblocks)
466 inode->i_size = 3 * dirsize;
467 else {
468 root_block[2 * dirsize] = '\0';
469 root_block[2 * dirsize + 1] = '\0';
470 inode->i_size = 2 * dirsize;
471 }
472 inode->i_mode = S_IFDIR + 0755;
473 inode->i_uid = getuid();
474 if (inode->i_uid)
475 inode->i_gid = getgid();
476 write_block (inode->i_zone[0], root_block);
477 }
478 #endif
479
480 void setup_tables(void)
481 {
482 int i;
483 unsigned long inodes;
484
485 memset(super_block_buffer,0,BLOCK_SIZE);
486 memset(boot_block_buffer,0,512);
487 MAGIC = magic;
488 ZONESIZE = 0;
489 MAXSIZE = version2 ? 0x7fffffff : (7+512+512*512)*1024;
490 ZONES = BLOCKS;
491
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 = UPPER(BLOCKS - (1+IMAPS+INODE_BLOCKS), BITS_PER_BLOCK+1);
511 /* The old code here
512 * ZMAPS = 0;
513 * while (ZMAPS != UPPER(BLOCKS - NORM_FIRSTZONE + 1,BITS_PER_BLOCK))
514 * ZMAPS = UPPER(BLOCKS - NORM_FIRSTZONE + 1,BITS_PER_BLOCK);
515 * was no good, since it may loop. - aeb
516 */
517 FIRSTZONE = NORM_FIRSTZONE;
518 inode_map = malloc(IMAPS * BLOCK_SIZE);
519 zone_map = malloc(ZMAPS * BLOCK_SIZE);
520 if (!inode_map || !zone_map)
521 die(_("unable to allocate buffers for maps"));
522 memset(inode_map,0xff,IMAPS * BLOCK_SIZE);
523 memset(zone_map,0xff,ZMAPS * BLOCK_SIZE);
524 for (i = FIRSTZONE ; i<ZONES ; i++)
525 unmark_zone(i);
526 for (i = MINIX_ROOT_INO ; i<=INODES ; i++)
527 unmark_inode(i);
528 inode_buffer = malloc(INODE_BUFFER_SIZE);
529 if (!inode_buffer)
530 die(_("unable to allocate buffer for inodes"));
531 memset(inode_buffer,0,INODE_BUFFER_SIZE);
532 printf(_("%ld inodes\n"),INODES);
533 printf(_("%ld blocks\n"),ZONES);
534 printf(_("Firstdatazone=%ld (%ld)\n"),FIRSTZONE,NORM_FIRSTZONE);
535 printf(_("Zonesize=%d\n"),BLOCK_SIZE<<ZONESIZE);
536 printf(_("Maxsize=%ld\n\n"),MAXSIZE);
537 }
538
539 /*
540 * Perform a test of a block; return the number of
541 * blocks readable/writeable.
542 */
543 long do_check(char * buffer, int try, unsigned int current_block)
544 {
545 long got;
546
547 /* Seek to the correct loc. */
548 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
549 current_block * BLOCK_SIZE ) {
550 die(_("seek failed during testing of blocks"));
551 }
552
553
554 /* Try the read */
555 got = read(DEV, buffer, try * BLOCK_SIZE);
556 if (got < 0) got = 0;
557 if (got & (BLOCK_SIZE - 1 )) {
558 printf(_("Weird values in do_check: probably bugs\n"));
559 }
560 got /= BLOCK_SIZE;
561 return got;
562 }
563
564 static unsigned int currently_testing = 0;
565
566 void alarm_intr(int alnum)
567 {
568 if (currently_testing >= ZONES)
569 return;
570 signal(SIGALRM,alarm_intr);
571 alarm(5);
572 if (!currently_testing)
573 return;
574 printf("%d ...", currently_testing);
575 fflush(stdout);
576 }
577
578 void check_blocks(void)
579 {
580 int try,got;
581 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
582
583 currently_testing=0;
584 signal(SIGALRM,alarm_intr);
585 alarm(5);
586 while (currently_testing < ZONES) {
587 if (lseek(DEV,currently_testing*BLOCK_SIZE,SEEK_SET) !=
588 currently_testing*BLOCK_SIZE)
589 die(_("seek failed in check_blocks"));
590 try = TEST_BUFFER_BLOCKS;
591 if (currently_testing + try > ZONES)
592 try = ZONES-currently_testing;
593 got = do_check(buffer, try, currently_testing);
594 currently_testing += got;
595 if (got == try)
596 continue;
597 if (currently_testing < FIRSTZONE)
598 die(_("bad blocks before data-area: cannot make fs"));
599 mark_zone(currently_testing);
600 badblocks++;
601 currently_testing++;
602 }
603 if (badblocks > 1)
604 printf(_("%d bad blocks\n"), badblocks);
605 else if (badblocks == 1)
606 printf(_("one bad block\n"));
607 }
608
609 void get_list_blocks(filename)
610 char *filename;
611
612 {
613 FILE *listfile;
614 unsigned long blockno;
615
616 listfile=fopen(filename,"r");
617 if(listfile == (FILE *)NULL) {
618 die(_("can't open file of bad blocks"));
619 }
620 while(!feof(listfile)) {
621 fscanf(listfile,"%ld\n", &blockno);
622 mark_zone(blockno);
623 badblocks++;
624 }
625 if(badblocks > 1)
626 printf(_("%d bad blocks\n"), badblocks);
627 else if (badblocks == 1)
628 printf(_("one bad block\n"));
629 }
630
631 int main(int argc, char ** argv)
632 {
633 int i;
634 char * tmp;
635 struct stat statbuf;
636 char * listfile = NULL;
637 char * p;
638
639 if (argc && *argv)
640 program_name = *argv;
641 if ((p = strrchr(program_name, '/')) != NULL)
642 program_name = p+1;
643
644 setlocale(LC_ALL, "");
645 bindtextdomain(PACKAGE, LOCALEDIR);
646 textdomain(PACKAGE);
647
648 if (argc == 2 &&
649 (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
650 printf(_("%s from %s\n"), program_name, util_linux_version);
651 exit(0);
652 }
653
654 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
655 die(_("bad inode size"));
656 #ifdef HAVE_MINIX2
657 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
658 die(_("bad inode size"));
659 #endif
660 opterr = 0;
661 while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF)
662 switch (i) {
663 case 'c':
664 check=1; break;
665 case 'i':
666 req_nr_inodes = (unsigned long) atol(optarg);
667 break;
668 case 'l':
669 listfile = optarg; break;
670 case 'n':
671 i = strtoul(optarg,&tmp,0);
672 if (*tmp)
673 usage();
674 if (i == 14)
675 magic = MINIX_SUPER_MAGIC;
676 else if (i == 30)
677 magic = MINIX_SUPER_MAGIC2;
678 else
679 usage();
680 namelen = i;
681 dirsize = i+2;
682 break;
683 case 'v':
684 #ifdef HAVE_MINIX2
685 version2 = 1;
686 #else
687 fatal_error(_("%s: not compiled with minix v2 support\n"),-1);
688 #endif
689 break;
690 default:
691 usage();
692 }
693 argc -= optind;
694 argv += optind;
695 if (argc > 0 && !device_name) {
696 device_name = argv[0];
697 argc--;
698 argv++;
699 }
700 if (argc > 0) {
701 BLOCKS = strtol(argv[0],&tmp,0);
702 if (*tmp) {
703 printf(_("strtol error: number of blocks not specified"));
704 usage();
705 }
706 }
707
708 if (device_name && !BLOCKS)
709 BLOCKS = get_size (device_name) / 1024;
710 if (!device_name || BLOCKS<10) {
711 usage();
712 }
713 #ifdef HAVE_MINIX2
714 if (version2) {
715 if (namelen == 14)
716 magic = MINIX2_SUPER_MAGIC;
717 else
718 magic = MINIX2_SUPER_MAGIC2;
719 } else
720 #endif
721 if (BLOCKS > 65535)
722 BLOCKS = 65535;
723 check_mount(); /* is it already mounted? */
724 tmp = root_block;
725 *(short *)tmp = 1;
726 strcpy(tmp+2,".");
727 tmp += dirsize;
728 *(short *)tmp = 1;
729 strcpy(tmp+2,"..");
730 tmp += dirsize;
731 *(short *)tmp = 2;
732 strcpy(tmp+2,".badblocks");
733 DEV = open(device_name,O_RDWR );
734 if (DEV<0)
735 die(_("unable to open %s"));
736 if (fstat(DEV,&statbuf)<0)
737 die(_("unable to stat %s"));
738 if (!S_ISBLK(statbuf.st_mode))
739 check=0;
740 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
741 die(_("will not try to make filesystem on '%s'"));
742 setup_tables();
743 if (check)
744 check_blocks();
745 else if (listfile)
746 get_list_blocks(listfile);
747 #ifdef HAVE_MINIX2
748 if (version2) {
749 make_root_inode2 ();
750 make_bad_inode2 ();
751 } else
752 #endif
753 {
754 make_root_inode();
755 make_bad_inode();
756 }
757 mark_good_blocks();
758 write_tables();
759 return 0;
760 }