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