]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/mkfs.minix.c
2 * mkfs.c - make a linux (minix) file-system.
4 * (C) 1991 Linus Torvalds. This file may be redistributed as per
11 * 24.11.91 - Time began. Used the fsck sources to get started.
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.
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)
20 * 28.02.92 - Added %-information when using -c.
22 * 28.02.93 - Added support for other namelengths than the original
23 * 14 characters so that I can test the new kernel routines..
25 * 09.10.93 - Make exit status conform to that required by fsutil
26 * (Rik Faith, faith@cs.unc.edu)
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)
32 * 03.01.94 - Added support for file system valid flag.
33 * (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu)
35 * 30.10.94 - added support for v2 filesystem
36 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
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)
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)
46 * 02.07.96 - Added small patch from Russell King to make the program a
47 * good deal more portable (janl@math.uio.no)
49 * Usage: mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks]
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
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 :-).
71 #include <sys/ioctl.h>
76 #include <linux/minix_fs.h>
80 #ifdef MINIX2_SUPER_MAGIC2
85 #error "needs gcc for the bitop-__asm__'s"
92 #define MINIX_ROOT_INO 1
93 #define MINIX_BAD_INO 2
95 #define TEST_BUFFER_BLOCKS 16
96 #define MAX_GOOD_BLOCKS 512
98 #define UPPER(size,n) ((size+((n)-1))/(n))
99 #define INODE_SIZE (sizeof(struct minix_inode))
101 #define INODE_SIZE2 (sizeof(struct minix2_inode))
102 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
103 : MINIX_INODES_PER_BLOCK))
105 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
107 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
109 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
111 static char * program_name
= "mkfs";
112 static char * device_name
= NULL
;
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;
123 static char root_block
[BLOCK_SIZE
] = "\0";
125 static char * inode_buffer
= NULL
;
126 #define Inode (((struct minix_inode *) inode_buffer)-1)
128 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
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)
135 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
137 #define ZONES ((unsigned long)(Super.s_nzones))
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)
147 static char *inode_map
;
148 static char *zone_map
;
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;
156 #define inode_in_use(x) (bit(inode_map,(x)))
157 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
159 #define mark_inode(x) (setbit(inode_map,(x)))
160 #define unmark_inode(x) (clrbit(inode_map,(x)))
162 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
163 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
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.
170 volatile void fatal_error(const char * fmt_string
,int status
)
172 fprintf(stderr
,fmt_string
,program_name
,device_name
);
176 volatile void die(char *str
) {
177 fprintf(stderr
, "%s: %s\n", program_name
, str
);
181 volatile void usage()
183 fprintf(stderr
, "%s (%s)\n", program_name
, util_linux_version
);
185 _("Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n"),
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.
195 static void check_mount(void)
200 if ((f
= setmntent (MOUNTED
, "r")) == NULL
)
202 while ((mnt
= getmntent (f
)) != NULL
)
203 if (strcmp (device_name
, mnt
->mnt_fsname
) == 0)
209 die(_("%s is mounted; will not make a filesystem here!"));
212 static long valid_offset (int fd
, int offset
)
216 if (lseek (fd
, offset
, 0) < 0)
218 if (read (fd
, &ch
, 1) < 1)
223 static int count_blocks (int fd
)
228 for (high
= 1; valid_offset (fd
, high
); high
*= 2)
230 while (low
< high
- 1)
232 const int mid
= (low
+ high
) / 2;
234 if (valid_offset (fd
, mid
))
239 valid_offset (fd
, 0);
243 static int get_size(const char *file
)
248 fd
= open(file
, O_RDWR
);
253 if (ioctl(fd
, BLKGETSIZE
, &size
) >= 0) {
258 size
= count_blocks(fd
);
263 void write_tables(void)
265 /* Mark the super block valid. */
266 Super
.s_state
|= MINIX_VALID_FS
;
267 Super
.s_state
&= ~MINIX_ERROR_FS
;
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"));
286 void write_block(int blk
, char * buffer
)
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"));
294 int get_free_block(void)
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;
304 while (blk
< ZONES
&& zone_in_use(blk
))
307 die(_("not enough good blocks"));
308 good_blocks_table
[used_good_blocks
] = blk
;
313 void mark_good_blocks(void)
317 for (blk
=0 ; blk
< used_good_blocks
; blk
++)
318 mark_zone(good_blocks_table
[blk
]);
321 inline int next(int zone
)
325 while (++zone
< ZONES
)
326 if (zone_in_use(zone
))
331 void make_bad_inode(void)
333 struct minix_inode
* inode
= &Inode
[MINIX_BAD_INO
];
336 unsigned short ind_block
[BLOCK_SIZE
>>1];
337 unsigned short dind_block
[BLOCK_SIZE
>>1];
339 #define NEXT_BAD (zone = next(zone))
343 mark_inode(MINIX_BAD_INO
);
345 inode
->i_time
= time(NULL
);
346 inode
->i_mode
= S_IFREG
+ 0000;
347 inode
->i_size
= badblocks
*BLOCK_SIZE
;
349 for (i
=0 ; i
<7 ; i
++) {
350 inode
->i_zone
[i
] = zone
;
354 inode
->i_zone
[7] = ind
= get_free_block();
355 memset(ind_block
,0,BLOCK_SIZE
);
356 for (i
=0 ; i
<512 ; i
++) {
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
++) {
373 die(_("too many bad blocks"));
376 write_block(ind
, (char *) ind_block
);
378 write_block(dind
, (char *) dind_block
);
383 make_bad_inode2 (void)
385 struct minix2_inode
*inode
= &Inode2
[MINIX_BAD_INO
];
387 int ind
= 0, dind
= 0;
388 unsigned long ind_block
[BLOCK_SIZE
>> 2];
389 unsigned long dind_block
[BLOCK_SIZE
>> 2];
393 mark_inode (MINIX_BAD_INO
);
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
;
399 for (i
= 0; i
< 7; i
++) {
400 inode
->i_zone
[i
] = zone
;
404 inode
->i_zone
[7] = ind
= get_free_block ();
405 memset (ind_block
, 0, BLOCK_SIZE
);
406 for (i
= 0; i
< 256; i
++) {
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
++) {
423 /* Could make triple indirect block here */
424 die (_("too many bad blocks"));
427 write_block (ind
, (char *) ind_block
);
429 write_block (dind
, (char *) dind_block
);
433 void make_root_inode(void)
435 struct minix_inode
* inode
= &Inode
[MINIX_ROOT_INO
];
437 mark_inode(MINIX_ROOT_INO
);
438 inode
->i_zone
[0] = get_free_block();
440 inode
->i_time
= time(NULL
);
442 inode
->i_size
= 3*dirsize
;
444 root_block
[2*dirsize
] = '\0';
445 root_block
[2*dirsize
+1] = '\0';
446 inode
->i_size
= 2*dirsize
;
448 inode
->i_mode
= S_IFDIR
+ 0755;
449 inode
->i_uid
= getuid();
451 inode
->i_gid
= getgid();
452 write_block(inode
->i_zone
[0],root_block
);
457 make_root_inode2 (void)
459 struct minix2_inode
*inode
= &Inode2
[MINIX_ROOT_INO
];
461 mark_inode (MINIX_ROOT_INO
);
462 inode
->i_zone
[0] = get_free_block ();
464 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= time (NULL
);
466 inode
->i_size
= 3 * dirsize
;
468 root_block
[2 * dirsize
] = '\0';
469 root_block
[2 * dirsize
+ 1] = '\0';
470 inode
->i_size
= 2 * dirsize
;
472 inode
->i_mode
= S_IFDIR
+ 0755;
473 inode
->i_uid
= getuid();
475 inode
->i_gid
= getgid();
476 write_block (inode
->i_zone
[0], root_block
);
480 void setup_tables(void)
483 unsigned long inodes
;
485 memset(super_block_buffer
,0,BLOCK_SIZE
);
486 memset(boot_block_buffer
,0,512);
489 MAXSIZE
= version2
? 0x7fffffff : (7+512+512*512)*1024;
492 /* some magic nrs: 1 inode / 3 blocks */
493 if ( req_nr_inodes
== 0 )
496 inodes
= req_nr_inodes
;
497 /* Round up inode count to fill block size */
500 inodes
= ((inodes
+ MINIX2_INODES_PER_BLOCK
- 1) &
501 ~(MINIX2_INODES_PER_BLOCK
- 1));
504 inodes
= ((inodes
+ MINIX_INODES_PER_BLOCK
- 1) &
505 ~(MINIX_INODES_PER_BLOCK
- 1));
509 IMAPS
= UPPER(INODES
+ 1,BITS_PER_BLOCK
);
510 ZMAPS
= UPPER(BLOCKS
- (1+IMAPS
+INODE_BLOCKS
), BITS_PER_BLOCK
+1);
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
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
++)
526 for (i
= MINIX_ROOT_INO
; i
<=INODES
; i
++)
528 inode_buffer
= malloc(INODE_BUFFER_SIZE
);
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
);
540 * Perform a test of a block; return the number of
541 * blocks readable/writeable.
543 long do_check(char * buffer
, int try, unsigned int current_block
)
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"));
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"));
564 static unsigned int currently_testing
= 0;
566 void alarm_intr(int alnum
)
568 if (currently_testing
>= ZONES
)
570 signal(SIGALRM
,alarm_intr
);
572 if (!currently_testing
)
574 printf("%d ...", currently_testing
);
578 void check_blocks(void)
581 static char buffer
[BLOCK_SIZE
* TEST_BUFFER_BLOCKS
];
584 signal(SIGALRM
,alarm_intr
);
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
;
597 if (currently_testing
< FIRSTZONE
)
598 die(_("bad blocks before data-area: cannot make fs"));
599 mark_zone(currently_testing
);
604 printf(_("%d bad blocks\n"), badblocks
);
605 else if (badblocks
== 1)
606 printf(_("one bad block\n"));
609 void get_list_blocks(filename
)
614 unsigned long blockno
;
616 listfile
=fopen(filename
,"r");
617 if(listfile
== (FILE *)NULL
) {
618 die(_("can't open file of bad blocks"));
620 while(!feof(listfile
)) {
621 fscanf(listfile
,"%ld\n", &blockno
);
626 printf(_("%d bad blocks\n"), badblocks
);
627 else if (badblocks
== 1)
628 printf(_("one bad block\n"));
631 int main(int argc
, char ** argv
)
636 char * listfile
= NULL
;
640 program_name
= *argv
;
641 if ((p
= strrchr(program_name
, '/')) != NULL
)
644 setlocale(LC_ALL
, "");
645 bindtextdomain(PACKAGE
, LOCALEDIR
);
649 (!strcmp(argv
[1], "-V") || !strcmp(argv
[1], "--version"))) {
650 printf(_("%s from %s\n"), program_name
, util_linux_version
);
654 if (INODE_SIZE
* MINIX_INODES_PER_BLOCK
!= BLOCK_SIZE
)
655 die(_("bad inode size"));
657 if (INODE_SIZE2
* MINIX2_INODES_PER_BLOCK
!= BLOCK_SIZE
)
658 die(_("bad inode size"));
661 while ((i
= getopt(argc
, argv
, "ci:l:n:v")) != EOF
)
666 req_nr_inodes
= (unsigned long) atol(optarg
);
669 listfile
= optarg
; break;
671 i
= strtoul(optarg
,&tmp
,0);
675 magic
= MINIX_SUPER_MAGIC
;
677 magic
= MINIX_SUPER_MAGIC2
;
687 fatal_error(_("%s: not compiled with minix v2 support\n"),-1);
695 if (argc
> 0 && !device_name
) {
696 device_name
= argv
[0];
701 BLOCKS
= strtol(argv
[0],&tmp
,0);
703 printf(_("strtol error: number of blocks not specified"));
708 if (device_name
&& !BLOCKS
)
709 BLOCKS
= get_size (device_name
) / 1024;
710 if (!device_name
|| BLOCKS
<10) {
716 magic
= MINIX2_SUPER_MAGIC
;
718 magic
= MINIX2_SUPER_MAGIC2
;
723 check_mount(); /* is it already mounted? */
732 strcpy(tmp
+2,".badblocks");
733 DEV
= open(device_name
,O_RDWR
);
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
))
740 else if (statbuf
.st_rdev
== 0x0300 || statbuf
.st_rdev
== 0x0340)
741 die(_("will not try to make filesystem on '%s'"));
746 get_list_blocks(listfile
);