1 /* mkfs - make the MINIX filesystem Authors: Tanenbaum et al. */
3 /* Authors: Andy Tanenbaum, Paul Ogilvie, Frans Meulenbroeks, Bruce Evans
5 * This program can make version 1, 2 and 3 file systems, as follows:
6 * mkfs /dev/fd0 1200 # Version 3 (default)
7 * mkfs -1 /dev/fd0 360 # Version 1
9 * Note that the version 1 and 2 file systems produced by this program are not
10 * compatible with the original version 1 and 2 file system layout.
13 #include <sys/types.h>
28 #include <minix/partition.h>
29 #include <minix/u64.h>
30 #include <sys/ioctl.h>
35 #define EXTERN /* get rid of EXTERN by making it null */
39 #define max(a,b) ((a) > (b) ? (a) : (b))
53 #define BIT_MAP_SHIFT 13
54 #define INODE_MAX ((unsigned) 65535)
55 #define SECTOR_SIZE 512
59 maybedefine O_RDONLY
4 /* O_RDONLY | BINARY_BIT */
60 maybedefine BWRITE
5 /* O_WRONLY | BINARY_BIT */
64 #define mul64u(a,b) ((a) * (b))
65 #define lseek64(a,b,c,d) lseek(a,b,c)
72 typedef uint32_t block_t
;
73 typedef uint32_t zone_t
;
79 int next_zone
, next_inode
, zone_size
, zone_shift
= 0, zoff
;
81 int inode_offset
, lct
= 0, disk
, fd
, print
= 0, file
= 0;
82 unsigned int nrinodes
;
83 int override
= 0, simple
= 0, dflag
;
84 int donttest
; /* skip test if it fits on medium */
87 uint32_t current_time
, bin_time
;
89 char *umap_array
; /* bit map tells if block read yet */
90 int umap_array_elements
= 0;
91 block_t zone_map
; /* where is zone map? (depends on # inodes) */
98 #if defined(__NBSD_LIBC) || !defined(__minix)
99 #define getline _mkfs_getline
102 int main(int argc
, char **argv
);
103 block_t
sizeup(char *device
);
104 void super(zone_t zones
, ino_t inodes
);
105 void rootdir(ino_t inode
);
106 void eat_dir(ino_t parent
);
107 void eat_file(ino_t inode
, int f
);
108 void enter_dir(ino_t parent
, char *name
, ino_t child
);
109 void incr_size(ino_t n
, size_t count
);
110 static ino_t
alloc_inode(int mode
, int usrid
, int grpid
);
111 static zone_t
alloc_zone(void);
112 void add_zone(ino_t n
, zone_t z
, size_t bytes
, uint32_t cur_time
);
113 void add_z_1(ino_t n
, zone_t z
, size_t bytes
, uint32_t cur_time
);
114 void add_z_2(ino_t n
, zone_t z
, size_t bytes
, uint32_t cur_time
);
115 void incr_link(ino_t n
);
116 void insert_bit(block_t block
, int bit
);
117 int mode_con(char *p
);
118 void getline(char line
[LINE_LEN
], char *parse
[MAX_TOKENS
]);
119 void check_mtab(char *devname
);
120 uint32_t file_time(int f
);
122 void copy(char *from
, char *to
, size_t count
);
124 int read_and_set(block_t n
);
125 void special(char *string
);
126 void get_block(block_t n
, char *buf
);
127 void get_super_block(char *buf
);
128 void put_block(block_t n
, char *buf
);
129 void cache_init(void);
131 void mx_read(int blocknr
, char *buf
);
132 void mx_write(int blocknr
, char *buf
);
133 void dexit(char *s
, int sectnum
, int err
);
135 char *alloc_block(void);
137 /*================================================================
138 * mkfs - make filesystem
139 *===============================================================*/
144 int nread
, mode
, usrid
, grpid
, ch
;
145 block_t blocks
, maxblocks
;
150 char *token
[MAX_TOKENS
], line
[LINE_LEN
];
153 /* Get two times, the current time and the mod time of the binary of
154 * mkfs itself. When the -d flag is used, the later time is put into
155 * the i_mtimes of all the files. This feature is useful when
156 * producing a set of file systems, and one wants all the times to be
157 * identical. First you set the time of the mkfs binary to what you
160 current_time
= time((time_t *) 0); /* time mkfs is being run */
161 stat(argv
[0], &statbuf
);
162 bin_time
= statbuf
.st_mtime
; /* time when mkfs binary was last modified */
164 /* Process switches. */
169 inodes_per_block
= 0;
171 while ((ch
= getopt(argc
, argv
, "12b:di:lotB:")) != EOF
)
175 inodes_per_block
= V1_INODES_PER_BLOCK
;
181 blocks
= strtoul(optarg
, (char **) NULL
, 0);
185 current_time
= bin_time
;
188 i
= strtoul(optarg
, (char **) NULL
, 0);
190 case 'l': print
= 1; break;
191 case 'o': override
= 1; break;
192 case 't': donttest
= 1; break;
193 case 'B': block_size
= atoi(optarg
); break;
197 if (argc
== optind
) usage();
199 if(fs_version
== 3) {
200 if(!block_size
) block_size
= _MAX_BLOCK_SIZE
; /* V3 default block size */
201 if(block_size
%SECTOR_SIZE
|| block_size
< _MIN_BLOCK_SIZE
) {
202 fprintf(stderr
, "block size must be multiple of sector (%d) "
203 "and at least %d bytes\n",
204 SECTOR_SIZE
, _MIN_BLOCK_SIZE
);
205 pexit("specified block size illegal");
207 if(block_size
%V2_INODE_SIZE
) {
208 fprintf(stderr
, "block size must be a multiple of inode size (%d bytes)\n",
210 pexit("specified block size illegal");
214 pexit("Can't specify a block size if FS version is <3");
216 block_size
= _STATIC_BLOCK_SIZE
; /* V1/V2 block size */
219 if(!inodes_per_block
)
220 inodes_per_block
= V2_INODES_PER_BLOCK(block_size
);
222 /* now that the block size is known, do buffer allocations where
225 zero
= alloc_block();
226 bzero(zero
, block_size
);
228 /* Determine the size of the device if not specified as -b or proto. */
229 maxblocks
= sizeup(argv
[optind
]);
230 if (argc
- optind
== 1 && blocks
== 0) {
232 /* blocks == 0 is checked later, but leads to a funny way of
233 * reporting a 0-sized device (displays usage).
236 fprintf(stderr
, "%s: zero size device.\n", progname
);
241 /* The remaining args must be 'special proto', or just 'special' if the
242 * no. of blocks has already been specified.
244 if (argc
- optind
!= 2 && (argc
- optind
!= 1 || blocks
== 0)) usage();
246 if (blocks
> maxblocks
) {
247 fprintf(stderr
, "%s: %s: number of blocks too large for device.\n",
248 progname
, argv
[optind
]);
253 check_mtab(argv
[optind
]);
255 /* Check and start processing proto. */
256 optarg
= argv
[++optind
];
257 if (optind
< argc
&& (proto
= fopen(optarg
, "r")) != NULL
) {
258 /* Prototype file is readable. */
260 getline(line
, token
); /* skip boot block info */
262 /* Read the line with the block and inode counts. */
263 getline(line
, token
);
264 blocks
= atol(token
[0]);
265 inodes
= atoi(token
[1]);
267 /* Process mode line for root directory. */
268 getline(line
, token
);
269 mode
= mode_con(token
[0]);
270 usrid
= atoi(token
[1]);
271 grpid
= atoi(token
[2]);
275 /* Maybe the prototype file is just a size. Check. */
276 blocks
= strtoul(optarg
, (char **) NULL
, 0);
277 if (blocks
== 0) pexit("Can't open prototype file");
281 uint32_t kb
= div64u(mul64u(blocks
, block_size
), 1024);
283 uint32_t kb
= ((unsigned long long) blocks
* block_size
) / 1024;
286 if (kb
>= 100000) i
= kb
/ 4;
288 /* round up to fill inode block */
289 i
+= inodes_per_block
- 1;
290 i
= i
/ inodes_per_block
* inodes_per_block
;
291 if (i
> INODE_MAX
&& fs_version
< 3) i
= INODE_MAX
;
294 if (blocks
< 5) pexit("Block count too small");
295 if (i
< 1) pexit("Inode count too small");
296 if (i
> INODE_MAX
&& fs_version
< 3) pexit("Inode count too large");
299 /* Make simple file system of the given size, using defaults. */
311 bytes
= 1 + blocks
/8;
312 if(!(umap_array
= malloc(bytes
))) {
313 fprintf(stderr
, "mkfs: can't allocate block bitmap (%u bytes).\n",
317 umap_array_elements
= bytes
;
321 special(argv
[--optind
]);
328 testb
= (short *) alloc_block();
330 /* Try writing the last block of partition or diskette. */
331 if(lseek64(fd
, mul64u(blocks
- 1, block_size
), SEEK_SET
, NULL
) < 0) {
332 pexit("couldn't seek to last block to test size (1)");
336 testb
[block_size
/2-1] = 0x1F2F;
337 if ((w
=write(fd
, (char *) testb
, block_size
)) != block_size
) {
338 if(w
< 0) perror("write");
339 printf("%d/%u\n", w
, block_size
);
340 pexit("File system is too big for minor device (write)");
342 sync(); /* flush write, so if error next read fails */
343 if(lseek64(fd
, mul64u(blocks
- 1, block_size
), SEEK_SET
, NULL
) < 0) {
344 pexit("couldn't seek to last block to test size (2)");
348 nread
= read(fd
, (char *) testb
, block_size
);
349 if (nread
!= block_size
|| testb
[0] != 0x3245 || testb
[1] != 0x11FF ||
350 testb
[block_size
/2-1] != 0x1F2F) {
351 if(nread
< 0) perror("read");
352 printf("nread = %d\n", nread
);
353 printf("testb = 0x%x 0x%x 0x%x\n", testb
[0], testb
[1], testb
[block_size
-1]);
354 pexit("File system is too big for minor device (read)");
356 lseek64(fd
, mul64u(blocks
- 1, block_size
), SEEK_SET
, NULL
);
359 if (write(fd
, (char *) testb
, block_size
) != block_size
)
360 pexit("File system is too big for minor device (write2)");
361 lseek(fd
, 0L, SEEK_SET
);
366 /* Make the file-system */
370 put_block((block_t
) 0, zero
); /* Write a null boot block. */
372 zone_shift
= 0; /* for future use */
373 zones
= nrblocks
>> zone_shift
;
375 super(zones
, inodes
);
377 root_inum
= alloc_inode(mode
, usrid
, grpid
);
379 if (simple
== 0) eat_dir(root_inum
);
381 if (print
) print_fs();
389 /*================================================================
390 * sizeup - determine device size
391 *===============================================================*/
392 block_t
sizeup(device
)
404 if ((fd
= open(device
, O_RDONLY
)) == -1) {
406 perror("sizeup open");
411 if(minix_sizeup(device
, &bytes
) < 0) {
416 d
= div64u(bytes
, block_size
);
417 rem
= rem64u(bytes
, block_size
);
419 resize
= add64u(mul64u(d
, block_size
), rem
);
420 if(cmp64(resize
, bytes
) != 0) {
422 fprintf(stderr
, "mkfs: truncating FS at %u blocks\n", d
);
425 size
= lseek(fd
, 0, SEEK_END
);
426 if (size
== (off_t
) -1) {
427 fprintf(stderr
, "Cannot get device size fd=%d\n", fd
);
430 d
= size
/ block_size
;
439 static int bitmapsize(nr_bits
, block_size
)
445 nr_blocks
= (int) (nr_bits
/ FS_BITS_PER_BLOCK(block_size
));
446 if (((uint32_t) nr_blocks
* FS_BITS_PER_BLOCK(block_size
)) < nr_bits
) ++nr_blocks
;
450 /*================================================================
451 * super - construct a superblock
452 *===============================================================*/
454 void super(zones
, inodes
)
464 struct super_block
*sup
;
469 for (cp
= buf
; cp
< &buf
[block_size
]; cp
++) *cp
= 0;
470 sup
= (struct super_block
*) buf
; /* lint - might use a union */
472 /* The assumption is that mkfs will create a clean FS. */
473 sup
->s_flags
= MFSFLAG_CLEAN
;
475 sup
->s_ninodes
= inodes
;
476 if (fs_version
== 1) {
477 sup
->s_nzones
= zones
;
478 if (sup
->s_nzones
!= zones
) pexit("too many zones");
480 sup
->s_nzones
= 0; /* not used in V2 - 0 forces errors early */
481 sup
->s_zones
= zones
;
484 #define BIGGERBLOCKS "Please try a larger block size for an FS of this size.\n"
485 sup
->s_imap_blocks
= nb
= bitmapsize((uint32_t) (1 + inodes
), block_size
);
486 if(sup
->s_imap_blocks
!= nb
) {
487 fprintf(stderr
, "mkfs: too many inode bitmap blocks.\n" BIGGERBLOCKS
);
490 sup
->s_zmap_blocks
= nb
= bitmapsize((uint32_t) zones
, block_size
);
491 if(nb
!= sup
->s_zmap_blocks
) {
492 fprintf(stderr
, "mkfs: too many block bitmap blocks.\n" BIGGERBLOCKS
);
495 inode_offset
= START_BLOCK
+ sup
->s_imap_blocks
+ sup
->s_zmap_blocks
;
496 inodeblks
= (inodes
+ inodes_per_block
- 1) / inodes_per_block
;
497 initblks
= inode_offset
+ inodeblks
;
498 sup
->s_firstdatazone_old
= nb
=
499 (initblks
+ (1 << zone_shift
) - 1) >> zone_shift
;
500 if(nb
>= zones
) pexit("bit maps too large");
501 if(nb
!= sup
->s_firstdatazone_old
) {
502 /* The field is too small to store the value. Fortunately, the value
503 * can be computed from other fields. We set the on-disk field to zero
504 * to indicate that it must not be used. Eventually, we can always set
505 * the on-disk field to zero, and stop using it.
507 sup
->s_firstdatazone_old
= 0;
509 sup
->s_firstdatazone
= nb
;
510 zoff
= sup
->s_firstdatazone
- 1;
511 sup
->s_log_zone_size
= zone_shift
;
512 if (fs_version
== 1) {
513 sup
->s_magic
= SUPER_MAGIC
; /* identify super blocks */
514 v1sq
= (zone_t
) V1_INDIRECTS
* V1_INDIRECTS
;
515 zo
= V1_NR_DZONES
+ (int) V1_INDIRECTS
+ v1sq
;
516 sup
->s_max_size
= zo
* block_size
;
518 v2sq
= (zone_t
) V2_INDIRECTS(block_size
) * V2_INDIRECTS(block_size
);
519 zo
= V2_NR_DZONES
+ (zone_t
) V2_INDIRECTS(block_size
) + v2sq
;
520 if(fs_version
== 2) {
521 sup
->s_magic
= SUPER_V2
;/* identify super blocks */
522 sup
->s_max_size
= zo
* block_size
;
524 sup
->s_magic
= SUPER_V3
;
525 sup
->s_block_size
= block_size
;
526 sup
->s_disk_version
= 0;
527 #define MAX_MAX_SIZE (INT_MAX)
528 if(MAX_MAX_SIZE
/block_size
< zo
) {
529 sup
->s_max_size
= (int32_t) MAX_MAX_SIZE
;
532 sup
->s_max_size
= zo
* block_size
;
537 zone_size
= 1 << zone_shift
; /* nr of blocks per zone */
539 if (lseek(fd
, (off_t
) _STATIC_BLOCK_SIZE
, SEEK_SET
) == (off_t
) -1) {
540 pexit("super() couldn't seek");
542 if (write(fd
, buf
, _STATIC_BLOCK_SIZE
) != _STATIC_BLOCK_SIZE
) {
543 pexit("super() couldn't write");
546 /* Clear maps and inodes. */
547 for (i
= START_BLOCK
; i
< initblks
; i
++) put_block((block_t
) i
, zero
);
549 next_zone
= sup
->s_firstdatazone
;
552 zone_map
= INODE_MAP
+ sup
->s_imap_blocks
;
554 insert_bit(zone_map
, 0); /* bit zero must always be allocated */
555 insert_bit((block_t
) INODE_MAP
, 0); /* inode zero not used but
556 * must be allocated */
562 /*================================================================
563 * rootdir - install the root directory
564 *===============================================================*/
571 add_zone(inode
, z
, 2 * sizeof(struct direct
), current_time
);
572 enter_dir(inode
, ".", inode
);
573 enter_dir(inode
, "..", inode
);
579 /*================================================================
580 * eat_dir - recursively install directory
581 *===============================================================*/
585 /* Read prototype lines and set up directory. Recurse if need be. */
586 char *token
[MAX_TOKENS
], *p
;
588 int mode
, usrid
, grpid
, maj
, min
, f
;
594 getline(line
, token
);
596 if (*p
== '$') return;
599 usrid
= atoi(token
[2]);
600 grpid
= atoi(token
[3]);
601 if (grpid
& 0200) fprintf(stderr
, "A.S.Tanenbaum\n");
602 n
= alloc_inode(mode
, usrid
, grpid
);
604 /* Enter name in directory and update directory's size. */
605 enter_dir(parent
, token
[0], n
);
606 incr_size(parent
, sizeof(struct direct
));
608 /* Check to see if file is directory or special. */
611 /* This is a directory. */
612 z
= alloc_zone(); /* zone for new directory */
613 add_zone(n
, z
, 2 * sizeof(struct direct
), current_time
);
614 enter_dir(n
, ".", n
);
615 enter_dir(n
, "..", parent
);
619 } else if (*p
== 'b' || *p
== 'c') {
621 maj
= atoi(token
[4]);
622 min
= atoi(token
[5]);
624 if (token
[6]) size
= atoi(token
[6]);
625 size
= block_size
* size
;
626 add_zone(n
, (zone_t
) (makedev(maj
,min
)), size
, current_time
);
628 /* Regular file. Go read it. */
629 if ((f
= open(token
[4], O_RDONLY
)) < 0) {
630 fprintf(stderr
, "%s: Can't open %s: %s\n",
631 progname
, token
[4], strerror(errno
));
640 /*================================================================
641 * eat_file - copy file to MINIX
642 *===============================================================*/
643 /* Zonesize >= blocksize */
644 void eat_file(inode
, f
)
656 for (i
= 0, j
= 0; i
< zone_size
; i
++, j
+= ct
) {
657 for (k
= 0; k
< block_size
; k
++) buf
[k
] = 0;
658 if ((ct
= read(f
, buf
, block_size
)) > 0) {
659 if (i
== 0) z
= alloc_zone();
660 put_block((z
<< zone_shift
) + i
, buf
);
663 timeval
= (dflag
? current_time
: file_time(f
));
664 if (ct
) add_zone(inode
, z
, (size_t) j
, timeval
);
665 } while (ct
== block_size
);
672 /*================================================================
673 * directory & inode management assist group
674 *===============================================================*/
675 void enter_dir(parent
, name
, child
)
679 /* Enter child in parent directory */
680 /* Works for dir > 1 block and zone > block */
681 unsigned int i
, j
, k
, l
, off
;
685 struct direct
*dir_entry
;
686 d1_inode ino1
[V1_INODES_PER_BLOCK
];
690 b
= ((parent
- 1) / inodes_per_block
) + inode_offset
;
691 off
= (parent
- 1) % inodes_per_block
;
693 if(!(dir_entry
= malloc(NR_DIR_ENTRIES(block_size
) * sizeof(*dir_entry
))))
694 pexit("couldn't allocate directory entry");
696 if(!(ino2
= malloc(V2_INODES_PER_BLOCK(block_size
) * sizeof(*ino2
))))
697 pexit("couldn't allocate block of inodes entry");
699 if (fs_version
== 1) {
700 get_block(b
, (char *) ino1
);
701 nr_dzones
= V1_NR_DZONES
;
703 get_block(b
, (char *) ino2
);
704 nr_dzones
= V2_NR_DZONES
;
706 for (k
= 0; k
< nr_dzones
; k
++) {
707 if (fs_version
== 1) {
708 z
= ino1
[off
].d1_zone
[k
];
711 ino1
[off
].d1_zone
[k
] = z
;
714 z
= ino2
[off
].d2_zone
[k
];
717 ino2
[off
].d2_zone
[k
] = z
;
720 for (l
= 0; l
< zone_size
; l
++) {
721 get_block((z
<< zone_shift
) + l
, (char *) dir_entry
);
722 for (i
= 0; i
< NR_DIR_ENTRIES(block_size
); i
++) {
723 if (dir_entry
[i
].mfs_d_ino
== 0) {
724 dir_entry
[i
].mfs_d_ino
= child
;
726 p2
= dir_entry
[i
].mfs_d_name
;
727 j
= sizeof(dir_entry
[i
].mfs_d_name
);
733 put_block((z
<< zone_shift
) + l
, (char *) dir_entry
);
734 if (fs_version
== 1) {
735 put_block(b
, (char *) ino1
);
737 put_block(b
, (char *) ino2
);
747 printf("Directory-inode %lu beyond direct blocks. Could not enter %s\n",
753 void add_zone(ino_t n
, zone_t z
, size_t bytes
, uint32_t cur_time
)
755 if (fs_version
== 1) {
756 add_z_1(n
, z
, bytes
, cur_time
);
758 add_z_2(n
, z
, bytes
, cur_time
);
762 void add_z_1(ino_t n
, zone_t z
, size_t bytes
, uint32_t cur_time
)
764 /* Add zone z to inode n. The file has grown by 'bytes' bytes. */
769 uint16_t blk
[V1_INDIRECTS
];
771 d1_inode inode
[V1_INODES_PER_BLOCK
];
773 b
= ((n
- 1) / V1_INODES_PER_BLOCK
) + inode_offset
;
774 off
= (n
- 1) % V1_INODES_PER_BLOCK
;
775 get_block(b
, (char *) inode
);
778 p
->d1_mtime
= cur_time
;
779 for (i
= 0; i
< V1_NR_DZONES
; i
++)
780 if (p
->d1_zone
[i
] == 0) {
781 p
->d1_zone
[i
] = (uint16_t) z
;
782 put_block(b
, (char *) inode
);
785 put_block(b
, (char *) inode
);
787 /* File has grown beyond a small file. */
788 if (p
->d1_zone
[V1_NR_DZONES
] == 0)
789 p
->d1_zone
[V1_NR_DZONES
] = (uint16_t) alloc_zone();
790 indir
= p
->d1_zone
[V1_NR_DZONES
];
791 put_block(b
, (char *) inode
);
792 b
= indir
<< zone_shift
;
793 get_block(b
, (char *) blk
);
794 for (i
= 0; i
< V1_INDIRECTS
; i
++)
796 blk
[i
] = (uint16_t) z
;
797 put_block(b
, (char *) blk
);
800 pexit("File has grown beyond single indirect");
803 void add_z_2(ino_t n
, zone_t z
, size_t bytes
, uint32_t cur_time
)
805 /* Add zone z to inode n. The file has grown by 'bytes' bytes. */
814 if(!(blk
= malloc(V2_INDIRECTS(block_size
)*sizeof(*blk
))))
815 pexit("Couldn't allocate indirect block");
817 if(!(inode
= malloc(V2_INODES_PER_BLOCK(block_size
)*sizeof(*inode
))))
818 pexit("Couldn't allocate block of inodes");
820 b
= ((n
- 1) / V2_INODES_PER_BLOCK(block_size
)) + inode_offset
;
821 off
= (n
- 1) % V2_INODES_PER_BLOCK(block_size
);
822 get_block(b
, (char *) inode
);
825 p
->d2_mtime
= cur_time
;
826 for (i
= 0; i
< V2_NR_DZONES
; i
++)
827 if (p
->d2_zone
[i
] == 0) {
829 put_block(b
, (char *) inode
);
834 put_block(b
, (char *) inode
);
836 /* File has grown beyond a small file. */
837 if (p
->d2_zone
[V2_NR_DZONES
] == 0) p
->d2_zone
[V2_NR_DZONES
] = alloc_zone();
838 indir
= p
->d2_zone
[V2_NR_DZONES
];
839 put_block(b
, (char *) inode
);
840 b
= indir
<< zone_shift
;
841 get_block(b
, (char *) blk
);
842 for (i
= 0; i
< V2_INDIRECTS(block_size
); i
++)
845 put_block(b
, (char *) blk
);
850 pexit("File has grown beyond single indirect");
857 /* Increment the link count to inode n */
859 static int enter
= 0;
864 b
= ((n
- 1) / inodes_per_block
) + inode_offset
;
865 off
= (n
- 1) % inodes_per_block
;
866 if (fs_version
== 1) {
867 d1_inode inode1
[V1_INODES_PER_BLOCK
];
869 get_block(b
, (char *) inode1
);
870 inode1
[off
].d1_nlinks
++;
871 put_block(b
, (char *) inode1
);
873 static d2_inode
*inode2
= NULL
;
876 n
= sizeof(*inode2
) * V2_INODES_PER_BLOCK(block_size
);
877 if(!inode2
&& !(inode2
= malloc(n
)))
878 pexit("couldn't allocate a block of inodes");
880 get_block(b
, (char *) inode2
);
881 inode2
[off
].d2_nlinks
++;
882 put_block(b
, (char *) inode2
);
888 void incr_size(n
, count
)
892 /* Increment the file-size in inode n */
896 b
= ((n
- 1) / inodes_per_block
) + inode_offset
;
897 off
= (n
- 1) % inodes_per_block
;
898 if (fs_version
== 1) {
899 d1_inode inode1
[V1_INODES_PER_BLOCK
];
901 get_block(b
, (char *) inode1
);
902 inode1
[off
].d1_size
+= count
;
903 put_block(b
, (char *) inode1
);
906 if(!(inode2
= malloc(V2_INODES_PER_BLOCK(block_size
) * sizeof(*inode2
))))
907 pexit("couldn't allocate a block of inodes");
909 get_block(b
, (char *) inode2
);
910 inode2
[off
].d2_size
+= count
;
911 put_block(b
, (char *) inode2
);
917 /*================================================================
918 * allocation assist group
919 *===============================================================*/
920 static ino_t
alloc_inode(mode
, usrid
, grpid
)
921 int mode
, usrid
, grpid
;
928 if (num
> nrinodes
) {
929 fprintf(stderr
, "have %d inodoes\n", nrinodes
);
930 pexit("File system does not have enough inodes");
932 b
= ((num
- 1) / inodes_per_block
) + inode_offset
;
933 off
= (num
- 1) % inodes_per_block
;
934 if (fs_version
== 1) {
935 d1_inode inode1
[V1_INODES_PER_BLOCK
];
937 get_block(b
, (char *) inode1
);
938 inode1
[off
].d1_mode
= mode
;
939 inode1
[off
].d1_uid
= usrid
;
940 inode1
[off
].d1_gid
= grpid
;
941 put_block(b
, (char *) inode1
);
945 if(!(inode2
= malloc(V2_INODES_PER_BLOCK(block_size
) * sizeof(*inode2
))))
946 pexit("couldn't allocate a block of inodes");
948 get_block(b
, (char *) inode2
);
949 inode2
[off
].d2_mode
= mode
;
950 inode2
[off
].d2_uid
= usrid
;
951 inode2
[off
].d2_gid
= grpid
;
952 put_block(b
, (char *) inode2
);
957 /* Set the bit in the bit map. */
958 /* DEBUG FIXME. This assumes the bit is in the first inode map block. */
959 insert_bit((block_t
) INODE_MAP
, (int) num
);
964 static zone_t
alloc_zone()
966 /* Allocate a new zone */
967 /* Works for zone > block */
974 if ((b
+ zone_size
) > nrblocks
)
975 pexit("File system not big enough for all the files");
976 for (i
= 0; i
< zone_size
; i
++)
977 put_block(b
+ i
, zero
); /* give an empty zone */
978 /* DEBUG FIXME. This assumes the bit is in the first zone map block. */
979 insert_bit(zone_map
, (int) (z
- zoff
)); /* lint, NOT OK because
980 * z hasn't been broken
987 void insert_bit(block
, bit
)
991 /* Insert 'count' bits in the bitmap */
1000 buf
= (bitchunk_t
*) alloc_block();
1002 buf
= (uint32_t *) alloc_block();
1005 get_block(block
, (char *) buf
);
1006 #if defined(__minix)
1007 w
= bit
/ (8 * sizeof(bitchunk_t
));
1008 s
= bit
% (8 * sizeof(bitchunk_t
));
1010 w
= bit
/ (8 * sizeof(uint32_t));
1011 s
= bit
% (8 * sizeof(uint32_t));
1014 put_block(block
, (char *) buf
);
1020 /*================================================================
1021 * proto-file processing assist group
1022 *===============================================================*/
1026 /* Convert string to mode */
1027 int o1
, o2
, o3
, mode
;
1036 mode
= (o1
<< 6) | (o2
<< 3) | o3
;
1037 if (c1
== 'd') mode
|= S_IFDIR
;
1038 if (c1
== 'b') mode
|= S_IFBLK
;
1039 if (c1
== 'c') mode
|= S_IFCHR
;
1040 if (c1
== '-') mode
|= S_IFREG
;
1041 if (c2
== 'u') mode
|= S_ISUID
;
1042 if (c3
== 'g') mode
|= S_ISGID
;
1046 void getline(line
, parse
)
1047 char *parse
[MAX_TOKENS
];
1048 char line
[LINE_LEN
];
1050 /* Read a line and break it up in tokens */
1055 for (k
= 0; k
< MAX_TOKENS
; k
++) parse
[k
] = 0;
1056 for (k
= 0; k
< LINE_LEN
; k
++) line
[k
] = 0;
1061 if (++k
> LINE_LEN
) pexit("Line too long");
1063 if (d
== EOF
) pexit("Unexpected end-of-file");
1065 if (*p
== '\n') lct
++;
1066 if (*p
== ' ' || *p
== '\t') *p
= 0;
1080 if (c
== '\n') return;
1081 if (c
== 0) continue;
1085 } while (c
!= 0 && c
!= '\n');
1090 /*================================================================
1092 *===============================================================*/
1093 void check_mtab(device
)
1094 char *device
; /* /dev/hd1 or whatever */
1096 /* Check to see if the special file named in s is mounted. */
1097 #if defined(__minix)
1100 char special
[PATH_MAX
+ 1], mounted_on
[PATH_MAX
+ 1], version
[10], rw_flag
[10];
1102 r
= stat(device
, &sb
);
1105 if (errno
== ENOENT
)
1106 return; /* Does not exist, and therefore not mounted. */
1107 fprintf(stderr
, "%s: stat %s failed: %s\n",
1108 progname
, device
, strerror(errno
));
1111 if (!S_ISBLK(sb
.st_mode
))
1113 /* Not a block device and therefore not mounted. */
1117 if (load_mtab("mkfs") < 0) return;
1119 n
= get_mtab_entry(special
, mounted_on
, version
, rw_flag
);
1121 if (strcmp(device
, special
) == 0) {
1122 /* Can't mkfs on top of a mounted file system. */
1123 fprintf(stderr
, "%s: %s is mounted on %s\n",
1124 progname
, device
, mounted_on
);
1128 #elif defined(__linux__)
1129 /* XXX: this code is copyright Theodore T'so and distributed under the GPLv2. Rewrite.
1133 dev_t file_dev
=0, file_rdev
=0;
1137 char *mtab_file
= "/proc/mounts";
1139 if ((f
= setmntent (mtab_file
, "r")) == NULL
)
1142 if (stat(device
, &st_buf
) == 0) {
1143 if (S_ISBLK(st_buf
.st_mode
)) {
1144 file_rdev
= st_buf
.st_rdev
;
1146 file_dev
= st_buf
.st_dev
;
1147 file_ino
= st_buf
.st_ino
;
1151 while ((mnt
= getmntent (f
)) != NULL
) {
1152 if (strcmp(device
, mnt
->mnt_fsname
) == 0)
1154 if (stat(mnt
->mnt_fsname
, &st_buf
) == 0) {
1155 if (S_ISBLK(st_buf
.st_mode
)) {
1156 if (file_rdev
&& (file_rdev
== st_buf
.st_rdev
))
1159 if (file_dev
&& ((file_dev
== st_buf
.st_dev
) &&
1160 (file_ino
== st_buf
.st_ino
)))
1168 * Do an extra check to see if this is the root device. We
1169 * can't trust /etc/mtab, and /proc/mounts will only list
1170 * /dev/root for the root filesystem. Argh. Instead we
1171 * check if the given device has the same major/minor number
1172 * as the device that the root directory is on.
1174 if (file_rdev
&& stat("/", &st_buf
) == 0) {
1175 if (st_buf
.st_dev
== file_rdev
) {
1181 /* Validate the entry in case /etc/mtab is out of date */
1183 * We need to be paranoid, because some broken distributions
1184 * (read: Slackware) don't initialize /etc/mtab before checking
1185 * all of the non-root filesystems on the disk.
1187 if (stat(mnt
->mnt_dir
, &st_buf
) < 0) {
1188 if (errno
== ENOENT
) {
1193 if (file_rdev
&& (st_buf
.st_dev
!= file_rdev
)) {
1197 fprintf(stderr
, "Device %s is mounted, exiting\n", device
);
1201 * Check to see if we're referring to the root filesystem.
1202 * If so, do a manual check to see if we can open /etc/mtab
1203 * read/write, since if the root is mounted read/only, the
1204 * contents of /etc/mtab may not be accurate.
1206 if (!strcmp(mnt
->mnt_dir
, "/")) {
1208 fprintf(stderr
, "Device %s is mounted as root file system!\n",
1216 if ((stat(device
, &st_buf
) != 0) ||
1217 !S_ISBLK(st_buf
.st_mode
))
1219 fd
= open(device
, O_RDONLY
| O_EXCL
);
1221 if (errno
== EBUSY
) {
1222 fprintf(stderr
, "Device %s is used by the system\n", device
);
1232 fprintf(stderr
, "Error while checking if device %s is mounted\n", device
);
1238 uint32_t file_time(f
)
1242 struct stat statbuf
;
1244 return(statbuf
.st_mtime
);
1245 #else /* fstat not supported by DOS */
1254 fprintf(stderr
, "%s: %s\n", progname
, s
);
1256 fprintf(stderr
, "Line %d being processed when error detected.\n", lct
);
1262 void copy(from
, to
, count
)
1266 while (count
--) *to
++ = *from
++;
1273 if(!(buf
= malloc(block_size
))) {
1274 pexit("couldn't allocate filesystem buffer");
1276 bzero(buf
, block_size
);
1285 d1_inode inode1
[V1_INODES_PER_BLOCK
];
1287 unsigned short *usbuf
;
1291 if(!(inode2
= malloc(V2_INODES_PER_BLOCK(block_size
) * sizeof(*inode2
))))
1292 pexit("couldn't allocate a block of inodes");
1294 if(!(dir
= malloc(NR_DIR_ENTRIES(block_size
)*sizeof(*dir
))))
1295 pexit("malloc of directory entry failed");
1297 usbuf
= (unsigned short *) alloc_block();
1299 get_super_block((char *) usbuf
);
1300 printf("\nSuperblock: ");
1301 for (i
= 0; i
< 8; i
++) printf("%06o ", usbuf
[i
]);
1302 get_block((block_t
) 2, (char *) usbuf
);
1303 printf("...\nInode map: ");
1304 for (i
= 0; i
< 9; i
++) printf("%06o ", usbuf
[i
]);
1305 get_block((block_t
) 3, (char *) usbuf
);
1306 printf("...\nZone map: ");
1307 for (i
= 0; i
< 9; i
++) printf("%06o ", usbuf
[i
]);
1314 for (b
= inode_offset
; k
< nrinodes
; b
++) {
1315 if (fs_version
== 1) {
1316 get_block(b
, (char *) inode1
);
1318 get_block(b
, (char *) inode2
);
1320 for (i
= 0; i
< inodes_per_block
; i
++) {
1321 k
= inodes_per_block
* (int) (b
- inode_offset
) + i
+ 1;
1323 if (k
> nrinodes
) break;
1324 if (fs_version
== 1) {
1325 if (inode1
[i
].d1_mode
!= 0) {
1326 printf("Inode %2lu: mode=", k
);
1327 printf("%06o", inode1
[i
].d1_mode
);
1328 printf(" uid=%2d gid=%2d size=",
1329 inode1
[i
].d1_uid
, inode1
[i
].d1_gid
);
1330 printf("%6d", inode1
[i
].d1_size
);
1331 printf(" zone[0]=%d\n", inode1
[i
].d1_zone
[0]);
1333 if ((inode1
[i
].d1_mode
& S_IFMT
) == S_IFDIR
) {
1334 /* This is a directory */
1335 get_block(inode1
[i
].d1_zone
[0], (char *) dir
);
1336 for (j
= 0; j
< NR_DIR_ENTRIES(block_size
); j
++)
1337 if (dir
[j
].mfs_d_ino
)
1338 printf("\tInode %2u: %s\n", dir
[j
].mfs_d_ino
, dir
[j
].mfs_d_name
);
1341 if (inode2
[i
].d2_mode
!= 0) {
1342 printf("Inode %2lu: mode=", k
);
1343 printf("%06o", inode2
[i
].d2_mode
);
1344 printf(" uid=%2d gid=%2d size=",
1345 inode2
[i
].d2_uid
, inode2
[i
].d2_gid
);
1346 printf("%6d", inode2
[i
].d2_size
);
1347 printf(" zone[0]=%u\n", inode2
[i
].d2_zone
[0]);
1349 if ((inode2
[i
].d2_mode
& S_IFMT
) == S_IFDIR
) {
1350 /* This is a directory */
1351 get_block(inode2
[i
].d2_zone
[0], (char *) dir
);
1352 for (j
= 0; j
< NR_DIR_ENTRIES(block_size
); j
++)
1353 if (dir
[j
].mfs_d_ino
)
1354 printf("\tInode %2u: %s\n", dir
[j
].mfs_d_ino
, dir
[j
].mfs_d_name
);
1360 printf("%d inodes used. %d zones used.\n", next_inode
- 1, next_zone
);
1369 /* The first time a block is read, it returns all 0s, unless there has
1370 * been a write. This routine checks to see if a block has been accessed.
1376 if(w
>= umap_array_elements
) {
1377 pexit("umap array too small - this can't happen");
1381 r
= (umap_array
[w
] & mask
? 1 : 0);
1382 umap_array
[w
] |= mask
;
1389 "Usage: %s [-12dlot] [-b blocks] [-i inodes] [-B blocksize] special [proto]\n",
1394 /*================================================================
1395 * get_block & put_block for MS-DOS
1396 *===============================================================*/
1400 * These are the get_block and put_block routines
1401 * when compiling & running mkfs.c under MS-DOS.
1403 * It requires the (asembler) routines absread & abswrite
1404 * from the file diskio.asm. Since these routines just do
1405 * as they are told (read & write the sector specified),
1406 * a local cache is used to minimize the i/o-overhead for
1407 * frequently used blocks.
1409 * The global variable "file" determines whether the output
1410 * is to a disk-device or to a binary file.
1414 #define PH_SECTSIZE 512 /* size of a physical disk-sector */
1417 char *derrtab
[14] = {
1419 "disk is read-only",
1424 "internal error: bad request structure length",
1426 "unknown media type",
1428 "printer out of paper (?)",
1434 #define CACHE_SIZE 20 /* 20 block-buffers */
1438 char blockbuf
[BLOCK_SIZE
];
1442 } cache
[CACHE_SIZE
];
1445 void special(string
)
1449 if (string
[1] == ':' && string
[2] == 0) {
1450 /* Format: d: or d:fname */
1451 disk
= (string
[0] & ~32) - 'A';
1452 if (disk
> 1 && !override
) /* safety precaution */
1453 pexit("Bad drive specifier for special");
1456 if ((fd
= creat(string
, BWRITE
)) == 0)
1457 pexit("Can't open special file");
1461 void get_block(n
, buf
)
1465 /* Get a block to the user */
1466 struct cache
*bp
, *fp
;
1468 /* First access returns a zero block */
1469 if (read_and_set(n
) == 0) {
1470 copy(zero
, buf
, block_size
);
1474 /* Look for block in cache */
1476 for (bp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++) {
1477 if (bp
->blocknum
== n
) {
1478 copy(bp
, buf
, block_size
);
1483 /* Remember clean block */
1486 if (fp
->usecnt
> bp
->usecnt
) fp
= bp
;
1491 /* Block not in cache, get it */
1493 /* No clean buf, flush one */
1494 for (bp
= cache
, fp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++)
1495 if (fp
->usecnt
> bp
->usecnt
) fp
= bp
;
1496 mx_write(fp
->blocknum
, fp
);
1502 copy(fp
, buf
, block_size
);
1505 void put_block(n
, buf
)
1509 /* Accept block from user */
1510 struct cache
*fp
, *bp
;
1512 (void) read_and_set(n
);
1514 /* Look for block in cache */
1516 for (bp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++) {
1517 if (bp
->blocknum
== n
) {
1518 copy(buf
, bp
, block_size
);
1523 /* Remember clean block */
1526 if (fp
->usecnt
> bp
->usecnt
) fp
= bp
;
1531 /* Block not in cache */
1533 /* No clean buf, flush one */
1534 for (bp
= cache
, fp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++)
1535 if (fp
->usecnt
> bp
->usecnt
) fp
= bp
;
1536 mx_write(fp
->blocknum
, fp
);
1541 copy(buf
, fp
, block_size
);
1547 for (bp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++) bp
->blocknum
= -1;
1552 /* Flush all dirty blocks to disk */
1555 for (bp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++)
1557 mx_write(bp
->blocknum
, bp
);
1562 /*==================================================================
1563 * hard read & write etc.
1564 *=================================================================*/
1565 #define MAX_RETRIES 5
1568 void mx_read(blocknr
, buf
)
1573 /* Read the requested MINIX-block in core */
1574 char (*bp
)[PH_SECTSIZE
];
1575 int sectnum
, retries
, err
;
1578 lseek(fd
, (off_t
) blocknr
* block_size
, 0);
1579 if (read(fd
, buf
, block_size
) != block_size
)
1580 pexit("mx_read: error reading file");
1582 sectnum
= blocknr
* (block_size
/ PH_SECTSIZE
);
1583 for (bp
= buf
; bp
< &buf
[block_size
]; bp
++) {
1584 retries
= MAX_RETRIES
;
1586 err
= absread(disk
, sectnum
, bp
);
1587 while (err
&& --retries
);
1592 dexit("mx_read", sectnum
, err
);
1598 void mx_write(blocknr
, buf
)
1602 /* Write the MINIX-block to disk */
1603 char (*bp
)[PH_SECTSIZE
];
1604 int retries
, sectnum
, err
;
1607 lseek(fd
, blocknr
* block_size
, 0);
1608 if (write(fd
, buf
, block_size
) != block_size
) {
1609 pexit("mx_write: error writing file");
1612 sectnum
= blocknr
* (block_size
/ PH_SECTSIZE
);
1613 for (bp
= buf
; bp
< &buf
[block_size
]; bp
++) {
1614 retries
= MAX_RETRIES
;
1616 err
= abswrite(disk
, sectnum
, bp
);
1617 } while (err
&& --retries
);
1622 dexit("mx_write", sectnum
, err
);
1629 void dexit(s
, sectnum
, err
)
1633 printf("Error: %s, sector: %d, code: %d, meaning: %s\n",
1634 s
, sectnum
, err
, derrtab
[err
]);
1640 /*================================================================
1641 * get_block & put_block for UNIX
1642 *===============================================================*/
1645 void special(string
)
1648 fd
= creat(string
, 0777);
1650 fd
= open(string
, O_RDWR
);
1651 if (fd
< 0) pexit("Can't open special file");
1656 void get_block(n
, buf
)
1664 /* First access returns a zero block */
1665 if (read_and_set(n
) == 0) {
1666 copy(zero
, buf
, block_size
);
1669 lseek64(fd
, mul64u(n
, block_size
), SEEK_SET
, NULL
);
1670 k
= read(fd
, buf
, block_size
);
1671 if (k
!= block_size
) {
1672 pexit("get_block couldn't read");
1676 void get_super_block(buf
)
1683 if(lseek(fd
, (off_t
) SUPER_BLOCK_BYTES
, SEEK_SET
) < 0) {
1685 pexit("seek failed");
1687 k
= read(fd
, buf
, _STATIC_BLOCK_SIZE
);
1688 if (k
!= _STATIC_BLOCK_SIZE
) {
1689 pexit("get_super_block couldn't read");
1693 void put_block(n
, buf
)
1697 /* Write a block. */
1699 (void) read_and_set(n
);
1701 /* XXX - check other lseeks too. */
1702 if (lseek64(fd
, mul64u(n
, block_size
), SEEK_SET
, NULL
) == (off_t
) -1) {
1703 pexit("put_block couldn't seek");
1705 if (write(fd
, buf
, block_size
) != block_size
) {
1706 pexit("put_block couldn't write");
1711 /* Dummy routines to keep source file clean from #ifdefs */