1 /* mkfs - make the MINIX filesystem Authors: Tanenbaum et al. */
3 /* Authors: Andy Tanenbaum, Paul Ogilvie, Frans Meulenbroeks, Bruce Evans */
5 #if HAVE_NBTOOL_CONFIG_H
6 #include "nbtool_config.h"
10 #include <sys/types.h>
14 #include <minix/minlib.h>
15 #include <minix/partition.h>
16 #include <minix/u64.h>
17 #include <sys/ioctl.h>
18 #elif defined(__linux__)
35 /* Definition of the file system layout: */
41 #define INODE_MAP START_BLOCK
42 /* inode zone indexes pointing to single and double indirect zones */
43 #define S_INDIRECT_IDX (NR_DZONES)
44 #define D_INDIRECT_IDX (NR_DZONES+1)
49 /* XXX why do we not use 0 / SU_ID ? */
54 #define mul64u(a,b) ((uint64_t)(a) * (b))
57 /* some Minix specific types that do not conflict with Posix */
59 typedef uint32_t block_t
; /* block number */
62 typedef uint32_t zone_t
; /* zone number */
65 typedef uint32_t bit_t
; /* bit number in a bit map */
68 typedef uint32_t bitchunk_t
; /* collection of bits in a bitmap */
72 ino_t inocount
; /* amount of inodes */
73 zone_t zonecount
; /* amount of zones */
74 block_t blockcount
; /* amount of blocks */
81 int zone_per_block
, zone_shift
= 0;
82 zone_t next_zone
, zoff
, nr_indirzones
;
83 int inodes_per_block
, indir_per_block
, indir_per_zone
;
84 unsigned int zone_size
;
85 ino_t nrinodes
, inode_offset
, next_inode
;
86 int lct
= 0, fd
, print
= 0;
87 int simple
= 0, dflag
= 0, verbose
= 0;
88 int donttest
; /* skip test if it fits on medium */
90 uint64_t fs_offset_bytes
, fs_offset_blocks
, written_fs_size
= 0;
94 unsigned char *umap_array
; /* bit map tells if block read yet */
95 size_t umap_array_elements
;
96 block_t zone_map
; /* where is zone map? (depends on # inodes) */
97 #ifndef MFS_STATIC_BLOCK_SIZE
100 #define block_size MFS_STATIC_BLOCK_SIZE
105 int main(int argc
, char **argv
);
106 void detect_fs_size(struct fs_size
* fssize
);
107 void sizeup_dir(struct fs_size
* fssize
);
108 block_t
sizeup(char *device
);
109 static int bitmapsize(bit_t nr_bits
, size_t blk_size
);
110 void super(zone_t zones
, ino_t inodes
);
111 void rootdir(ino_t inode
);
112 void enter_symlink(ino_t inode
, char *link
);
113 int dir_try_enter(zone_t z
, ino_t child
, char const *name
);
114 void eat_dir(ino_t parent
);
115 void eat_file(ino_t inode
, int f
);
116 void enter_dir(ino_t parent
, char const *name
, ino_t child
);
117 void add_zone(ino_t n
, zone_t z
, size_t bytes
, time_t cur_time
);
118 void incr_link(ino_t n
);
119 void incr_size(ino_t n
, size_t count
);
120 static ino_t
alloc_inode(int mode
, int usrid
, int grpid
);
121 static zone_t
alloc_zone(void);
122 void insert_bit(block_t block
, bit_t bit
);
123 int mode_con(char *p
);
124 void get_line(char line
[LINE_LEN
], char *parse
[MAX_TOKENS
]);
125 void check_mtab(const char *devname
);
126 time_t file_time(int f
);
127 __dead
void pexit(char const *s
, ...) __printflike(1,2);
128 void *alloc_block(void);
130 int read_and_set(block_t n
);
131 void special(char *string
, int insertmode
);
132 __dead
void usage(void);
133 void get_block(block_t n
, void *buf
);
134 void get_super_block(void *buf
);
135 void put_block(block_t n
, void *buf
);
136 static uint64_t mkfs_seek(uint64_t pos
, int whence
);
137 static ssize_t
mkfs_write(void * buf
, size_t count
);
139 /*================================================================
140 * mkfs - make filesystem
141 *===============================================================*/
143 main(int argc
, char *argv
[])
145 int nread
, mode
, usrid
, grpid
, ch
, extra_space_percent
, Tflag
= 0;
146 block_t blocks
, maxblocks
, bblocks
;
147 ino_t inodes
, root_inum
;
148 char *token
[MAX_TOKENS
], line
[LINE_LEN
], *sfx
;
149 struct fs_size fssize
;
154 /* Process switches. */
158 #ifndef MFS_STATIC_BLOCK_SIZE
162 extra_space_percent
= 0;
163 while ((ch
= getopt(argc
, argv
, "B:b:di:ltvx:z:I:T:")) != EOF
)
165 #ifndef MFS_STATIC_BLOCK_SIZE
167 block_size
= strtoul(optarg
, &sfx
, 0);
169 case 'b': case 'B': /* bytes; NetBSD-compatible */
172 case 'k': block_size
*=1024; break;
173 case 's': block_size
*=SECTOR_SIZE
; break;
179 if (block_size
!= strtoul(optarg
, (char **) NULL
, 0))
180 errx(4, "block size must be exactly %d bytes",
181 MFS_STATIC_BLOCK_SIZE
);
183 (void)sfx
; /* shut up warnings about unused variable...*/
186 fs_offset_bytes
= strtoul(optarg
, (char **) NULL
, 0);
190 blocks
= bblocks
= strtoul(optarg
, (char **) NULL
, 0);
194 current_time
= strtoul(optarg
, (char **) NULL
, 0);
200 inodes
= strtoul(optarg
, (char **) NULL
, 0);
202 case 'l': print
= 1; break;
203 case 't': donttest
= 1; break;
204 case 'v': ++verbose
; break;
205 case 'x': extra_space_percent
= atoi(optarg
); break;
206 case 'z': zone_shift
= atoi(optarg
); break;
210 if (argc
== optind
) usage();
212 /* Get the current time, set it to the mod time of the binary of
213 * mkfs itself when the -d flag is used. The 'current' time is put into
214 * the i_mtimes of all the files. This -d feature is useful when
215 * producing a set of file systems, and one wants all the times to be
216 * identical. First you set the time of the mkfs binary to what you
221 errx(1, "-T and -d both specify a time and so are mutually exclusive");
224 if (stat(progname
, &statbuf
)) {
225 err(1, "stat of itself");
227 current_time
= statbuf
.st_mtime
;
229 current_time
= time((time_t *) 0); /* time mkfs is being run */
232 /* Percentage of extra size must be nonnegative.
233 * It can legitimately be bigger than 100 but has to make some sort of sense.
235 if(extra_space_percent
< 0 || extra_space_percent
> 2000) usage();
237 #ifdef DEFAULT_BLOCK_SIZE
238 if(!block_size
) block_size
= DEFAULT_BLOCK_SIZE
;
240 if (block_size
% SECTOR_SIZE
)
241 errx(4, "block size must be multiple of sector (%d bytes)", SECTOR_SIZE
);
242 #ifdef MIN_BLOCK_SIZE
243 if (block_size
< MIN_BLOCK_SIZE
)
244 errx(4, "block size must be at least %d bytes", MIN_BLOCK_SIZE
);
246 #ifdef MAX_BLOCK_SIZE
247 if (block_size
> MAX_BLOCK_SIZE
)
248 errx(4, "block size must be at most %d bytes", MAX_BLOCK_SIZE
);
250 if(block_size
%INODE_SIZE
)
251 errx(4, "block size must be a multiple of inode size (%d bytes)", INODE_SIZE
);
253 if(zone_shift
< 0 || zone_shift
> 14)
254 errx(4, "zone_shift must be a small non-negative integer");
255 zone_per_block
= 1 << zone_shift
; /* nr of blocks per zone */
257 inodes_per_block
= INODES_PER_BLOCK(block_size
);
258 indir_per_block
= INDIRECTS(block_size
);
259 indir_per_zone
= INDIRECTS(block_size
) << zone_shift
;
260 /* number of file zones we can address directly and with a single indirect*/
261 nr_indirzones
= NR_DZONES
+ indir_per_zone
;
262 zone_size
= block_size
<< zone_shift
;
263 /* Checks for an overflow: only with very big block size */
265 errx(4, "Zones are too big for this program; smaller -B or -z, please!");
267 /* now that the block size is known, do buffer allocations where
270 zero
= alloc_block();
272 fs_offset_blocks
= roundup(fs_offset_bytes
, block_size
) / block_size
;
274 /* Determine the size of the device if not specified as -b or proto. */
275 maxblocks
= sizeup(argv
[optind
]);
276 if (bblocks
!= 0 && bblocks
+ fs_offset_blocks
> maxblocks
&& !insertmode
) {
277 errx(4, "Given size -b %d exeeds device capacity(%d)\n", bblocks
, maxblocks
);
280 if (argc
- optind
== 1 && bblocks
== 0) {
282 /* blocks == 0 is checked later, but leads to a funny way of
283 * reporting a 0-sized device (displays usage).
286 errx(1, "zero size device.");
290 /* The remaining args must be 'special proto', or just 'special' if the
291 * no. of blocks has already been specified.
293 if (argc
- optind
!= 2 && (argc
- optind
!= 1 || blocks
== 0)) usage();
295 if (maxblocks
&& blocks
> maxblocks
&& !insertmode
) {
296 errx(1, "%s: number of blocks too large for device.", argv
[optind
]);
300 check_mtab(argv
[optind
]);
302 /* Check and start processing proto. */
303 optarg
= argv
[++optind
];
304 if (optind
< argc
&& (proto
= fopen(optarg
, "r")) != NULL
) {
305 /* Prototype file is readable. */
307 get_line(line
, token
); /* skip boot block info */
309 /* Read the line with the block and inode counts. */
310 get_line(line
, token
);
312 blocks
= strtol(token
[0], (char **) NULL
, 10);
314 if(bblocks
< strtol(token
[0], (char **) NULL
, 10)) {
315 errx(1, "%s: number of blocks given as parameter(%d)"
316 " is too small for given proto file(%d).",
317 argv
[optind
], bblocks
,
318 strtol(token
[0], (char **) NULL
, 10));
321 inodes
= strtol(token
[1], (char **) NULL
, 10);
323 /* Process mode line for root directory. */
324 get_line(line
, token
);
325 mode
= mode_con(token
[0]);
326 usrid
= atoi(token
[1]);
327 grpid
= atoi(token
[2]);
329 if(blocks
<= 0 && inodes
<= 0){
330 detect_fs_size(&fssize
);
331 blocks
= fssize
.blockcount
;
332 inodes
= fssize
.inocount
;
333 blocks
+= blocks
*extra_space_percent
/100;
334 inodes
+= inodes
*extra_space_percent
/100;
335 /* XXX is it OK to write on stdout? Use warn() instead? Also consider using verbose */
336 fprintf(stderr
, "dynamically sized filesystem: %u blocks, %u inodes\n",
337 (unsigned int) blocks
, (unsigned int) inodes
);
342 /* Maybe the prototype file is just a size. Check. */
343 blocks
= strtoul(optarg
, (char **) NULL
, 0);
344 if (blocks
== 0) errx(2, "Can't open prototype file");
347 /* Make simple file system of the given size, using defaults. */
355 long long kb
= ((unsigned long long)blocks
*block_size
) / 1024;
358 if (kb
>= 100000) inodes
= kb
/ 4;
359 if (kb
>= 1000000) inodes
= kb
/ 6;
360 if (kb
>= 10000000) inodes
= kb
/ 8;
361 if (kb
>= 100000000) inodes
= kb
/ 10;
362 if (kb
>= 1000000000) inodes
= kb
/ 12;
363 /* XXX check overflow: with very large number of blocks, this results in insanely large number of inodes */
364 /* XXX check underflow (if/when ino_t is signed), else the message below will look strange */
366 /* round up to fill inode block */
367 inodes
+= inodes_per_block
- 1;
368 inodes
= inodes
/ inodes_per_block
* inodes_per_block
;
371 if (blocks
< 5) errx(1, "Block count too small");
372 if (inodes
< 1) errx(1, "Inode count too small");
377 umap_array_elements
= 1 + blocks
/8;
378 if(!(umap_array
= malloc(umap_array_elements
)))
379 err(1, "can't allocate block bitmap (%u bytes).",
380 (unsigned)umap_array_elements
);
383 special(argv
[--optind
], insertmode
);
389 testb
= alloc_block();
391 /* Try writing the last block of partition or diskette. */
392 mkfs_seek(mul64u(blocks
- 1, block_size
), SEEK_SET
);
395 testb
[block_size
/2-1] = 0x1F2F;
396 w
=mkfs_write(testb
, block_size
);
397 sync(); /* flush write, so if error next read fails */
398 mkfs_seek(mul64u(blocks
- 1, block_size
), SEEK_SET
);
401 testb
[block_size
/2-1] = 0;
402 nread
= read(fd
, testb
, block_size
);
403 if (nread
!= block_size
|| testb
[0] != 0x3245 || testb
[1] != 0x11FF ||
404 testb
[block_size
/2-1] != 0x1F2F) {
405 warn("nread = %d\n", nread
);
406 warnx("testb = 0x%x 0x%x 0x%x\n",
407 testb
[0], testb
[1], testb
[block_size
-1]);
408 errx(1, "File system is too big for minor device (read)");
410 mkfs_seek(mul64u(blocks
- 1, block_size
), SEEK_SET
);
413 testb
[block_size
/2-1] = 0;
414 mkfs_write(testb
, block_size
);
415 mkfs_seek(0L, SEEK_SET
);
419 /* Make the file-system */
421 put_block(BOOT_BLOCK
, zero
); /* Write a null boot block. */
422 put_block(BOOT_BLOCK
+1, zero
); /* Write another null block. */
424 super(nrblocks
>> zone_shift
, inodes
);
426 root_inum
= alloc_inode(mode
, usrid
, grpid
);
428 if (simple
== 0) eat_dir(root_inum
);
430 if (print
) print_fs();
431 else if (verbose
> 1) {
433 fprintf(stderr
, "%d inodes used. %u zones (%u blocks) used.\n",
434 (int)next_inode
-1, next_zone
, next_zone
*zone_per_block
);
436 fprintf(stderr
, "%d inodes used. %u zones used.\n",
437 (int)next_inode
-1, next_zone
);
440 if(insertmode
) printf("%ld\n", written_fs_size
);
447 /*================================================================
448 * detect_fs_size - determine image size dynamically
449 *===============================================================*/
451 detect_fs_size(struct fs_size
* fssize
)
454 off_t point
= ftell(proto
);
458 fssize
->inocount
= 1; /* root directory node */
459 fssize
->zonecount
= 0;
460 fssize
->blockcount
= 0;
464 initb
= bitmapsize(1 + fssize
->inocount
, block_size
);
465 initb
+= bitmapsize(fssize
->zonecount
, block_size
);
466 initb
+= START_BLOCK
;
467 initb
+= (fssize
->inocount
+ inodes_per_block
- 1) / inodes_per_block
;
468 initz
= (initb
+ zone_per_block
- 1) >> zone_shift
;
470 fssize
->blockcount
= initb
+ fssize
->zonecount
;
472 fseek(proto
, point
, SEEK_SET
);
476 sizeup_dir(struct fs_size
* fssize
)
478 char *token
[MAX_TOKENS
], *p
;
483 zone_t dir_zones
= 0, fzones
, indirects
;
486 get_line(line
, token
);
489 dir_zones
= (dir_entries
/ (NR_DIR_ENTRIES(block_size
) * zone_per_block
));
490 if(dir_entries
% (NR_DIR_ENTRIES(block_size
) * zone_per_block
))
492 if(dir_zones
> NR_DZONES
)
493 dir_zones
++; /* Max single indir */
494 fssize
->zonecount
+= dir_zones
;
504 } else if (*p
== 'b' || *p
== 'c') {
506 } else if (*p
== 's') {
507 fssize
->zonecount
++; /* Symlink contents is always stored a block */
509 if ((f
= fopen(token
[4], "rb")) == NULL
) {
510 warn("dynamic sizing: can't open %s", token
[4]);
511 } else if (fseek(f
, 0, SEEK_END
) < 0) {
512 pexit("dynamic size detection failed: seek to end of %s",
514 } else if ( (size
= ftell(f
)) == (off_t
)(-1)) {
515 pexit("dynamic size detection failed: can't tell size of %s",
519 fzones
= roundup(size
, zone_size
) / zone_size
;
521 /* XXX overflow? fzones is u32, size is potentially 64-bit */
522 if (fzones
> NR_DZONES
)
523 indirects
++; /* single indirect needed */
524 if (fzones
> nr_indirzones
) {
525 /* Each further group of 'indir_per_zone'
526 * needs one supplementary indirect zone:
528 indirects
+= roundup(fzones
- nr_indirzones
,
529 indir_per_zone
) / indir_per_zone
;
530 indirects
++; /* + double indirect needed!*/
532 fssize
->zonecount
+= fzones
+ indirects
;
538 /*================================================================
539 * sizeup - determine device size
540 *===============================================================*/
542 sizeup(char * device
)
553 if ((fd
= open(device
, O_RDONLY
)) == -1) {
555 perror("sizeup open");
560 if(minix_sizeup(device
, &bytes
) < 0) {
565 d
= div64u(bytes
, block_size
);
566 rem
= rem64u(bytes
, block_size
);
568 resize
= mul64u(d
, block_size
) + rem
;
569 if(cmp64(resize
, bytes
) != 0) {
570 /* Assume block_t is unsigned */
572 fprintf(stderr
, "%s: truncating FS at %lu blocks\n",
573 progname
, (unsigned long)d
);
576 size
= mkfs_seek(0, SEEK_END
);
577 /* Assume block_t is unsigned */
578 if (size
/ block_size
> (block_t
)(-1ul)) {
580 fprintf(stderr
, "%s: truncating FS at %lu blocks\n",
581 progname
, (unsigned long)d
);
583 d
= size
/ block_size
;
593 bitmapsize(bit_t nr_bits
, size_t blk_size
)
597 nr_blocks
= nr_bits
/ FS_BITS_PER_BLOCK(blk_size
);
598 if (nr_blocks
* FS_BITS_PER_BLOCK(blk_size
) < nr_bits
)
603 /*================================================================
604 * super - construct a superblock
605 *===============================================================*/
608 super(zone_t zones
, ino_t inodes
)
610 block_t inodeblks
, initblks
, i
;
612 long long ind_per_zone
, zo
;
614 struct super_block
*sup
;
616 sup
= buf
= alloc_block();
619 /* The assumption is that mkfs will create a clean FS. */
620 sup
->s_flags
= MFSFLAG_CLEAN
;
623 sup
->s_ninodes
= inodes
;
624 /* Check for overflow; cannot happen on V3 file systems */
625 if(inodes
!= sup
->s_ninodes
)
626 errx(1, "Too much inodes for that version of Minix FS.");
627 sup
->s_nzones
= 0; /* not used in V2 - 0 forces errors early */
628 sup
->s_zones
= zones
;
629 /* Check for overflow; can only happen on V1 file systems */
630 if(zones
!= sup
->s_zones
)
631 errx(1, "Too much zones (blocks) for that version of Minix FS.");
633 #ifndef MFS_STATIC_BLOCK_SIZE
634 #define BIGGERBLOCKS "Please try a larger block size for an FS of this size."
636 #define BIGGERBLOCKS "Please use MinixFS V3 for an FS of this size."
638 sup
->s_imap_blocks
= nb
= bitmapsize(1 + inodes
, block_size
);
639 /* Checks for an overflow: nb is uint32_t while s_imap_blocks is of type
641 if(sup
->s_imap_blocks
!= nb
) {
642 errx(1, "too many inode bitmap blocks.\n" BIGGERBLOCKS
);
644 sup
->s_zmap_blocks
= nb
= bitmapsize(zones
, block_size
);
645 /* Idem here check for overflow */
646 if(nb
!= sup
->s_zmap_blocks
) {
647 errx(1, "too many block bitmap blocks.\n" BIGGERBLOCKS
);
649 inode_offset
= START_BLOCK
+ sup
->s_imap_blocks
+ sup
->s_zmap_blocks
;
650 inodeblks
= (inodes
+ inodes_per_block
- 1) / inodes_per_block
;
651 initblks
= inode_offset
+ inodeblks
;
652 sup
->s_firstdatazone_old
= nb
=
653 (initblks
+ (1 << zone_shift
) - 1) >> zone_shift
;
654 if(nb
>= zones
) errx(1, "bit maps too large");
655 if(nb
!= sup
->s_firstdatazone_old
) {
656 /* The field is too small to store the value. Fortunately, the value
657 * can be computed from other fields. We set the on-disk field to zero
658 * to indicate that it must not be used. Eventually, we can always set
659 * the on-disk field to zero, and stop using it.
661 sup
->s_firstdatazone_old
= 0;
663 sup
->s_firstdatazone
= nb
;
664 zoff
= sup
->s_firstdatazone
- 1;
665 sup
->s_log_zone_size
= zone_shift
;
666 sup
->s_magic
= SUPER_MAGIC
;
667 #ifdef MFS_SUPER_BLOCK_SIZE
668 sup
->s_block_size
= block_size
;
669 /* Check for overflow */
670 if(block_size
!= sup
->MFS_SUPER_BLOCK_SIZE
)
671 errx(1, "block_size too large.");
672 sup
->s_disk_version
= 0;
675 ind_per_zone
= (long long) indir_per_zone
;
676 zo
= NR_DZONES
+ ind_per_zone
+ ind_per_zone
*ind_per_zone
;
678 #define MAX_MAX_SIZE (INT32_MAX)
680 if(MAX_MAX_SIZE
/block_size
< zo
) {
681 sup
->s_max_size
= MAX_MAX_SIZE
;
684 sup
->s_max_size
= zo
* block_size
;
688 fprintf(stderr
, "Super block values:\n"
689 "\tnumber of inodes\t%12d\n"
690 "\tnumber of zones \t%12d\n"
691 "\tinode bit map blocks\t%12d\n"
692 "\tzone bit map blocks\t%12d\n"
693 "\tfirst data zone \t%12d\n"
694 "\tblocks per zone shift\t%12d\n"
695 "\tmaximum file size\t%12d\n"
696 "\tmagic number\t\t%#12X\n",
697 sup
->s_ninodes
, sup
->s_zones
,
698 sup
->s_imap_blocks
, sup
->s_zmap_blocks
, sup
->s_firstdatazone
,
699 sup
->s_log_zone_size
, sup
->s_max_size
, sup
->s_magic
);
700 #ifdef MFS_SUPER_BLOCK_SIZE
701 fprintf(stderr
, "\tblock size\t\t%12d\n", sup
->s_block_size
);
705 mkfs_seek((off_t
) SUPER_BLOCK_BYTES
, SEEK_SET
);
706 mkfs_write(buf
, SUPER_BLOCK_BYTES
);
708 /* Clear maps and inodes. */
709 for (i
= START_BLOCK
; i
< initblks
; i
++) put_block((block_t
) i
, zero
);
711 next_zone
= sup
->s_firstdatazone
;
714 zone_map
= INODE_MAP
+ sup
->s_imap_blocks
;
716 insert_bit(zone_map
, 0); /* bit zero must always be allocated */
717 insert_bit((block_t
) INODE_MAP
, 0); /* inode zero not used but
718 * must be allocated */
724 /*================================================================
725 * rootdir - install the root directory
726 *===============================================================*/
733 add_zone(inode
, z
, 2 * sizeof(struct direct
), current_time
);
734 enter_dir(inode
, ".", inode
);
735 enter_dir(inode
, "..", inode
);
741 enter_symlink(ino_t inode
, char *lnk
)
750 if (len
>= block_size
)
751 pexit("symlink too long, max length is %u", (unsigned)block_size
- 1);
753 put_block((z
<< zone_shift
), buf
);
755 add_zone(inode
, z
, len
, current_time
);
761 /*================================================================
762 * eat_dir - recursively install directory
763 *===============================================================*/
765 eat_dir(ino_t parent
)
767 /* Read prototype lines and set up directory. Recurse if need be. */
768 char *token
[MAX_TOKENS
], *p
;
770 int mode
, usrid
, grpid
, maj
, min
, f
;
776 get_line(line
, token
);
778 if (*p
== '$') return;
781 usrid
= atoi(token
[2]);
782 grpid
= atoi(token
[3]);
783 n
= alloc_inode(mode
, usrid
, grpid
);
785 /* Enter name in directory and update directory's size. */
786 enter_dir(parent
, token
[0], n
);
787 incr_size(parent
, sizeof(struct direct
));
789 /* Check to see if file is directory or special. */
792 /* This is a directory. */
793 z
= alloc_zone(); /* zone for new directory */
794 add_zone(n
, z
, 2 * sizeof(struct direct
), current_time
);
795 enter_dir(n
, ".", n
);
796 enter_dir(n
, "..", parent
);
800 } else if (*p
== 'b' || *p
== 'c') {
802 maj
= atoi(token
[4]);
803 min
= atoi(token
[5]);
805 if (token
[6]) size
= atoi(token
[6]);
806 size
= block_size
* size
;
807 add_zone(n
, (zone_t
) (makedev(maj
,min
)), size
, current_time
);
808 } else if (*p
== 's') {
809 enter_symlink(n
, token
[4]);
811 /* Regular file. Go read it. */
812 if ((f
= open(token
[4], O_RDONLY
)) < 0) {
813 fprintf(stderr
, "%s: Can't open %s: %s\n",
814 progname
, token
[4], strerror(errno
));
823 /*================================================================
824 * eat_file - copy file to MINIX
825 *===============================================================*/
826 /* Zonesize >= blocksize */
828 eat_file(ino_t inode
, int f
)
838 for (i
= 0, j
= 0; i
< zone_per_block
; i
++, j
+= ct
) {
839 memset(buf
, 0, block_size
);
840 if ((ct
= read(f
, buf
, block_size
)) > 0) {
841 if (i
== 0) z
= alloc_zone();
842 put_block((z
<< zone_shift
) + i
, buf
);
845 timeval
= (dflag
? current_time
: file_time(f
));
846 if (ct
) add_zone(inode
, z
, (size_t) j
, timeval
);
847 } while (ct
== block_size
);
853 dir_try_enter(zone_t z
, ino_t child
, char const *name
)
855 struct direct
*dir_entry
= alloc_block();
861 for (l
= 0; l
< zone_per_block
; l
++, b
++) {
862 get_block(b
, dir_entry
);
864 for (i
= 0; i
< NR_DIR_ENTRIES(block_size
); i
++)
865 if (!dir_entry
[i
].d_ino
)
868 if(i
< NR_DIR_ENTRIES(block_size
)) {
870 dir_entry
[i
].d_ino
= child
;
871 assert(sizeof(dir_entry
[i
].d_name
) == MFS_DIRSIZ
);
872 if (verbose
&& strlen(name
) > MFS_DIRSIZ
)
873 fprintf(stderr
, "File name %s is too long, truncated\n", name
);
874 strncpy(dir_entry
[i
].d_name
, name
, MFS_DIRSIZ
);
875 put_block(b
, dir_entry
);
885 /*================================================================
886 * directory & inode management assist group
887 *===============================================================*/
889 enter_dir(ino_t parent
, char const *name
, ino_t child
)
891 /* Enter child in parent directory */
892 /* Works for dir > 1 block and zone > block */
898 struct inode
*inoblock
= alloc_block();
899 zone_t
*indirblock
= alloc_block();
901 assert(!(block_size
% sizeof(struct direct
)));
903 /* Obtain the inode structure */
904 b
= ((parent
- 1) / inodes_per_block
) + inode_offset
;
905 off
= (parent
- 1) % inodes_per_block
;
906 get_block(b
, inoblock
);
907 ino
= inoblock
+ off
;
909 for (k
= 0; k
< NR_DZONES
; k
++) {
916 if(dir_try_enter(z
, child
, __UNCONST(name
))) {
917 put_block(b
, inoblock
);
924 /* no space in directory using just direct blocks; try indirect */
925 if (ino
->i_zone
[S_INDIRECT_IDX
] == 0)
926 ino
->i_zone
[S_INDIRECT_IDX
] = alloc_zone();
928 indir
= ino
->i_zone
[S_INDIRECT_IDX
] << zone_shift
;
929 --indir
; /* Compensate for ++indir below */
930 for(k
= 0; k
< (indir_per_zone
); k
++) {
931 if (k
% indir_per_block
== 0)
932 get_block(++indir
, indirblock
);
933 z
= indirblock
[k
% indir_per_block
];
935 z
= indirblock
[k
% indir_per_block
] = alloc_zone();
936 put_block(indir
, indirblock
);
938 if(dir_try_enter(z
, child
, __UNCONST(name
))) {
939 put_block(b
, inoblock
);
946 pexit("Directory-inode %u beyond single indirect blocks. Could not enter %s",
947 (unsigned)parent
, name
);
952 add_zone(ino_t n
, zone_t z
, size_t bytes
, time_t mtime
)
954 /* Add zone z to inode n. The file has grown by 'bytes' bytes. */
958 zone_t indir
, dindir
;
959 struct inode
*p
, *inode
;
962 assert(inodes_per_block
*sizeof(*inode
) == block_size
);
963 if(!(inode
= alloc_block()))
964 err(1, "Couldn't allocate block of inodes");
966 b
= ((n
- 1) / inodes_per_block
) + inode_offset
;
967 off
= (n
- 1) % inodes_per_block
;
972 #ifndef MFS_INODE_ONLY_MTIME /* V1 file systems did not have them... */
973 p
->i_atime
= p
->i_ctime
= current_time
;
975 for (i
= 0; i
< NR_DZONES
; i
++)
976 if (p
->i_zone
[i
] == 0) {
983 assert(indir_per_block
*sizeof(*blk
) == block_size
);
984 if(!(blk
= alloc_block()))
985 err(1, "Couldn't allocate indirect block");
987 /* File has grown beyond a small file. */
988 if (p
->i_zone
[S_INDIRECT_IDX
] == 0)
989 p
->i_zone
[S_INDIRECT_IDX
] = alloc_zone();
990 indir
= p
->i_zone
[S_INDIRECT_IDX
] << zone_shift
;
992 --indir
; /* Compensate for ++indir below */
993 for (i
= 0; i
< (indir_per_zone
); i
++) {
994 if (i
% indir_per_block
== 0)
995 get_block(++indir
, blk
);
996 if (blk
[i
% indir_per_block
] == 0) {
998 put_block(indir
, blk
);
1005 /* File has grown beyond single indirect; we need a double indirect */
1006 assert(indir_per_block
*sizeof(*dblk
) == block_size
);
1007 if(!(dblk
= alloc_block()))
1008 err(1, "Couldn't allocate double indirect block");
1010 if (p
->i_zone
[D_INDIRECT_IDX
] == 0)
1011 p
->i_zone
[D_INDIRECT_IDX
] = alloc_zone();
1012 dindir
= p
->i_zone
[D_INDIRECT_IDX
] << zone_shift
;
1013 put_block(b
, inode
);
1014 --dindir
; /* Compensate for ++indir below */
1015 for (j
= 0; j
< (indir_per_zone
); j
++) {
1016 if (j
% indir_per_block
== 0)
1017 get_block(++dindir
, dblk
);
1018 if (dblk
[j
% indir_per_block
] == 0)
1019 dblk
[j
% indir_per_block
] = alloc_zone();
1020 indir
= dblk
[j
% indir_per_block
] << zone_shift
;
1021 --indir
; /* Compensate for ++indir below */
1022 for (i
= 0; i
< (indir_per_zone
); i
++) {
1023 if (i
% indir_per_block
== 0)
1024 get_block(++indir
, blk
);
1025 if (blk
[i
% indir_per_block
] == 0) {
1027 put_block(dindir
, dblk
);
1028 put_block(indir
, blk
);
1037 pexit("File has grown beyond double indirect");
1041 /* Increment the link count to inode n */
1046 static int enter
= 0;
1047 static struct inode
*inodes
= NULL
;
1050 if (enter
++) pexit("internal error: recursive call to incr_link()");
1052 b
= ((n
- 1) / inodes_per_block
) + inode_offset
;
1053 off
= (n
- 1) % inodes_per_block
;
1055 assert(sizeof(*inodes
) * inodes_per_block
== block_size
);
1056 if(!inodes
&& !(inodes
= alloc_block()))
1057 err(1, "couldn't allocate a block of inodes");
1059 get_block(b
, inodes
);
1060 inodes
[off
].i_nlinks
++;
1061 /* Check overflow (particularly on V1)... */
1062 if (inodes
[off
].i_nlinks
<= 0)
1063 pexit("Too many links to a directory");
1064 put_block(b
, inodes
);
1070 /* Increment the file-size in inode n */
1072 incr_size(ino_t n
, size_t count
)
1077 b
= ((n
- 1) / inodes_per_block
) + inode_offset
;
1078 off
= (n
- 1) % inodes_per_block
;
1080 struct inode
*inodes
;
1082 assert(inodes_per_block
* sizeof(*inodes
) == block_size
);
1083 if(!(inodes
= alloc_block()))
1084 err(1, "couldn't allocate a block of inodes");
1086 get_block(b
, inodes
);
1087 /* Check overflow; avoid compiler spurious warnings */
1088 if (inodes
[off
].i_size
+(int)count
< inodes
[off
].i_size
||
1089 inodes
[off
].i_size
> MAX_MAX_SIZE
-(int)count
)
1090 pexit("File has become too big to be handled by MFS");
1091 inodes
[off
].i_size
+= count
;
1092 put_block(b
, inodes
);
1098 /*================================================================
1099 * allocation assist group
1100 *===============================================================*/
1102 alloc_inode(int mode
, int usrid
, int grpid
)
1107 struct inode
*inodes
;
1110 if (num
> nrinodes
) {
1111 pexit("File system does not have enough inodes (only %d)", nrinodes
);
1113 b
= ((num
- 1) / inodes_per_block
) + inode_offset
;
1114 off
= (num
- 1) % inodes_per_block
;
1116 assert(inodes_per_block
* sizeof(*inodes
) == block_size
);
1117 if(!(inodes
= alloc_block()))
1118 err(1, "couldn't allocate a block of inodes");
1120 get_block(b
, inodes
);
1121 if (inodes
[off
].i_mode
) {
1122 pexit("allocation new inode %d with non-zero mode - this cannot happen",
1125 inodes
[off
].i_mode
= mode
;
1126 inodes
[off
].i_uid
= usrid
;
1127 inodes
[off
].i_gid
= grpid
;
1128 if (verbose
&& (inodes
[off
].i_uid
!= usrid
|| inodes
[off
].i_gid
!= grpid
))
1129 fprintf(stderr
, "Uid/gid %d.%d do not fit within inode, truncated\n", usrid
, grpid
);
1130 put_block(b
, inodes
);
1134 /* Set the bit in the bit map. */
1135 insert_bit((block_t
) INODE_MAP
, num
);
1140 /* Allocate a new zone */
1144 /* Works for zone > block */
1150 b
= z
<< zone_shift
;
1151 if (b
> nrblocks
- zone_per_block
)
1152 pexit("File system not big enough for all the files");
1153 for (i
= 0; i
< zone_per_block
; i
++)
1154 put_block(b
+ i
, zero
); /* give an empty zone */
1156 insert_bit(zone_map
, z
- zoff
);
1161 /* Insert one bit into the bitmap */
1163 insert_bit(block_t map
, bit_t bit
)
1166 unsigned int bits_per_block
;
1170 buf
= alloc_block();
1172 bits_per_block
= FS_BITS_PER_BLOCK(block_size
);
1173 map_block
= map
+ bit
/ bits_per_block
;
1174 if (map_block
>= inode_offset
)
1175 pexit("insertbit invades inodes area - this cannot happen");
1176 boff
= bit
% bits_per_block
;
1179 assert(boff
< FS_BITS_PER_BLOCK(block_size
));
1180 get_block(map_block
, buf
);
1181 w
= boff
/ FS_BITCHUNK_BITS
;
1182 s
= boff
% FS_BITCHUNK_BITS
;
1184 put_block(map_block
, buf
);
1190 /*================================================================
1191 * proto-file processing assist group
1192 *===============================================================*/
1193 int mode_con(char *p
)
1195 /* Convert string to mode */
1196 int o1
, o2
, o3
, mode
;
1205 mode
= (o1
<< 6) | (o2
<< 3) | o3
;
1206 if (c1
== 'd') mode
|= S_IFDIR
;
1207 if (c1
== 'b') mode
|= S_IFBLK
;
1208 if (c1
== 'c') mode
|= S_IFCHR
;
1209 if (c1
== 's') mode
|= S_IFLNK
;
1210 if (c1
== 'l') mode
|= S_IFLNK
; /* just to be somewhat ls-compatible*/
1211 /* XXX note: some other mkfs programs consider L to create hardlinks */
1212 if (c1
== '-') mode
|= S_IFREG
;
1213 if (c2
== 'u') mode
|= S_ISUID
;
1214 if (c3
== 'g') mode
|= S_ISGID
;
1215 /* XXX There are no way to encode S_ISVTX */
1220 get_line(char line
[LINE_LEN
], char *parse
[MAX_TOKENS
])
1222 /* Read a line and break it up in tokens */
1227 for (k
= 0; k
< MAX_TOKENS
; k
++) parse
[k
] = 0;
1228 memset(line
, 0, LINE_LEN
);
1232 if (++k
> LINE_LEN
) pexit("Line too long");
1234 if (d
== EOF
) pexit("Unexpected end-of-file");
1236 if (*p
== ' ' || *p
== '\t') *p
= 0;
1250 if (c
== '\n') return;
1251 if (c
== 0) continue;
1255 } while (c
!= 0 && c
!= '\n');
1260 /*================================================================
1262 *===============================================================*/
1265 * Check to see if the special file named 'device' is mounted.
1268 check_mtab(const char *device
) /* /dev/hd1 or whatever */
1270 #if defined(__minix)
1273 char dev
[PATH_MAX
], mount_point
[PATH_MAX
],
1274 type
[MNTNAMELEN
], flags
[MNTFLAGLEN
];
1276 r
= stat(device
, &sb
);
1279 if (errno
== ENOENT
)
1280 return; /* Does not exist, and therefore not mounted. */
1281 err(1, "stat %s failed", device
);
1283 if (!S_ISBLK(sb
.st_mode
))
1285 /* Not a block device and therefore not mounted. */
1289 if (load_mtab(__UNCONST("mkfs")) < 0) return;
1291 n
= get_mtab_entry(dev
, mount_point
, type
, flags
);
1293 if (strcmp(device
, dev
) == 0) {
1294 /* Can't mkfs on top of a mounted file system. */
1295 errx(1, "%s is mounted on %s", device
, mount_point
);
1298 #elif defined(__linux__)
1299 /* XXX: this code is copyright Theodore T'so and distributed under the GPLv2. Rewrite.
1303 dev_t file_dev
=0, file_rdev
=0;
1307 char *mtab_file
= "/proc/mounts";
1309 if ((f
= setmntent (mtab_file
, "r")) == NULL
)
1312 if (stat(device
, &st_buf
) == 0) {
1313 if (S_ISBLK(st_buf
.st_mode
)) {
1314 file_rdev
= st_buf
.st_rdev
;
1316 file_dev
= st_buf
.st_dev
;
1317 file_ino
= st_buf
.st_ino
;
1321 while ((mnt
= getmntent (f
)) != NULL
) {
1322 if (strcmp(device
, mnt
->mnt_fsname
) == 0)
1324 if (stat(mnt
->mnt_fsname
, &st_buf
) == 0) {
1325 if (S_ISBLK(st_buf
.st_mode
)) {
1326 if (file_rdev
&& (file_rdev
== st_buf
.st_rdev
))
1329 if (file_dev
&& ((file_dev
== st_buf
.st_dev
) &&
1330 (file_ino
== st_buf
.st_ino
)))
1338 * Do an extra check to see if this is the root device. We
1339 * can't trust /etc/mtab, and /proc/mounts will only list
1340 * /dev/root for the root filesystem. Argh. Instead we
1341 * check if the given device has the same major/minor number
1342 * as the device that the root directory is on.
1344 if (file_rdev
&& stat("/", &st_buf
) == 0) {
1345 if (st_buf
.st_dev
== file_rdev
) {
1351 /* Validate the entry in case /etc/mtab is out of date */
1353 * We need to be paranoid, because some broken distributions
1354 * (read: Slackware) don't initialize /etc/mtab before checking
1355 * all of the non-root filesystems on the disk.
1357 if (stat(mnt
->mnt_dir
, &st_buf
) < 0) {
1358 if (errno
== ENOENT
) {
1363 if (file_rdev
&& (st_buf
.st_dev
!= file_rdev
)) {
1367 fprintf(stderr
, "Device %s is mounted, exiting\n", device
);
1371 * Check to see if we're referring to the root filesystem.
1372 * If so, do a manual check to see if we can open /etc/mtab
1373 * read/write, since if the root is mounted read/only, the
1374 * contents of /etc/mtab may not be accurate.
1376 if (!strcmp(mnt
->mnt_dir
, "/")) {
1378 fprintf(stderr
, "Device %s is mounted as root file system!\n",
1386 if ((stat(device
, &st_buf
) != 0) ||
1387 !S_ISBLK(st_buf
.st_mode
))
1389 fd
= open(device
, O_RDONLY
| O_EXCL
);
1391 if (errno
== EBUSY
) {
1392 fprintf(stderr
, "Device %s is used by the system\n", device
);
1402 fprintf(stderr
, "Error while checking if device %s is mounted\n", device
);
1405 (void) device
; /* shut up warnings about unused variable... */
1413 struct stat statbuf
;
1415 if (!fstat(f
, &statbuf
))
1416 return current_time
;
1417 if (statbuf
.st_mtime
<0 || statbuf
.st_mtime
>(uint32_t)(-1))
1418 return current_time
;
1419 return(statbuf
.st_mtime
);
1424 pexit(char const * s
, ...)
1432 warnx("Line %d being processed when error detected.\n", lct
);
1442 if(!(buf
= malloc(block_size
))) {
1443 err(1, "couldn't allocate filesystem buffer");
1445 memset(buf
, 0, block_size
);
1455 struct inode
*inode2
;
1456 unsigned short *usbuf
;
1460 assert(inodes_per_block
* sizeof(*inode2
) == block_size
);
1461 if(!(inode2
= alloc_block()))
1462 err(1, "couldn't allocate a block of inodes");
1464 assert(NR_DIR_ENTRIES(block_size
)*sizeof(*dir
) == block_size
);
1465 if(!(dir
= alloc_block()))
1466 err(1, "couldn't allocate a block of directory entries");
1468 usbuf
= alloc_block();
1469 get_super_block(usbuf
);
1470 printf("\nSuperblock: ");
1471 for (i
= 0; i
< 8; i
++) printf("%06ho ", usbuf
[i
]);
1473 for (i
= 0; i
< 8; i
++) printf("%#04hX ", usbuf
[i
]);
1475 for (i
= 8; i
< 15; i
++) printf("%06ho ", usbuf
[i
]);
1477 for (i
= 8; i
< 15; i
++) printf("%#04hX ", usbuf
[i
]);
1478 get_block((block_t
) INODE_MAP
, usbuf
);
1479 printf("...\nInode map: ");
1480 for (i
= 0; i
< 9; i
++) printf("%06ho ", usbuf
[i
]);
1481 get_block((block_t
) zone_map
, usbuf
);
1482 printf("...\nZone map: ");
1483 for (i
= 0; i
< 9; i
++) printf("%06ho ", usbuf
[i
]);
1490 for (b
= inode_offset
; k
< nrinodes
; b
++) {
1491 get_block(b
, inode2
);
1492 for (i
= 0; i
< inodes_per_block
; i
++) {
1493 k
= inodes_per_block
* (int) (b
- inode_offset
) + i
+ 1;
1495 if (k
> nrinodes
) break;
1497 if (inode2
[i
].i_mode
!= 0) {
1498 printf("Inode %3u: mode=", (unsigned)k
);
1499 printf("%06o", (unsigned)inode2
[i
].i_mode
);
1500 printf(" uid=%2d gid=%2d size=",
1501 (int)inode2
[i
].i_uid
, (int)inode2
[i
].i_gid
);
1502 printf("%6ld", (long)inode2
[i
].i_size
);
1503 printf(" zone[0]=%u\n", (unsigned)inode2
[i
].i_zone
[0]);
1505 if ((inode2
[i
].i_mode
& S_IFMT
) == S_IFDIR
) {
1506 /* This is a directory */
1507 get_block(inode2
[i
].i_zone
[0] << zone_shift
, dir
);
1508 for (j
= 0; j
< NR_DIR_ENTRIES(block_size
); j
++)
1510 printf("\tInode %2u: %s\n",
1511 (unsigned)dir
[j
].d_ino
,
1519 printf("%d inodes used. %u zones (%u blocks) used.\n",
1520 (int)next_inode
-1, next_zone
, next_zone
*zone_per_block
);
1522 printf("%d inodes used. %u zones used.\n", (int)next_inode
-1, next_zone
);
1529 * The first time a block is read, it returns all 0s, unless there has
1530 * been a write. This routine checks to see if a block has been accessed.
1533 read_and_set(block_t n
)
1539 assert(n
< nrblocks
);
1540 if(w
>= umap_array_elements
) {
1541 errx(1, "umap array too small - this can't happen");
1545 r
= (umap_array
[w
] & mask
? 1 : 0);
1546 umap_array
[w
] |= mask
;
1553 fprintf(stderr
, "Usage: %s [-dltv] [-b blocks] [-i inodes]\n"
1554 "\t[-z zone_shift] [-I offset] [-x extra] [-B blocksize] special [proto]\n",
1560 special(char * string
, int insertmode
)
1562 int openmode
= O_RDWR
;
1563 if(!insertmode
) openmode
|= O_TRUNC
;
1564 fd
= open(string
, O_RDWR
| O_CREAT
, 0644);
1565 if (fd
< 0) err(1, "Can't open special file %s", string
);
1566 mkfs_seek(0, SEEK_SET
);
1573 get_block(block_t n
, void *buf
)
1577 /* First access returns a zero block */
1578 if (read_and_set(n
) == 0) {
1579 memcpy(buf
, zero
, block_size
);
1582 mkfs_seek(mul64u(n
, block_size
), SEEK_SET
);
1583 k
= read(fd
, buf
, block_size
);
1584 if (k
!= block_size
)
1585 pexit("get_block couldn't read block #%u", (unsigned)n
);
1588 /* Read the super block. */
1590 get_super_block(void *buf
)
1594 mkfs_seek((off_t
) SUPER_BLOCK_BYTES
, SEEK_SET
);
1595 k
= read(fd
, buf
, SUPER_BLOCK_BYTES
);
1596 if (k
!= SUPER_BLOCK_BYTES
)
1597 err(1, "get_super_block couldn't read super block");
1600 /* Write a block. */
1602 put_block(block_t n
, void *buf
)
1605 (void) read_and_set(n
);
1607 mkfs_seek(mul64u(n
, block_size
), SEEK_SET
);
1608 mkfs_write(buf
, block_size
);
1612 mkfs_write(void * buf
, size_t count
)
1617 /* Perform & check write */
1618 w
= write(fd
, buf
, count
);
1620 err(1, "mkfs_write: write failed");
1622 errx(1, "mkfs_write: short write: %ld != %ld", w
, count
);
1624 /* Check if this has made the FS any bigger; count bytes after offset */
1625 fssize
= mkfs_seek(0, SEEK_CUR
);
1627 assert(fssize
>= fs_offset_bytes
);
1628 fssize
-= fs_offset_bytes
;
1629 fssize
= roundup(fssize
, block_size
);
1630 if(fssize
> written_fs_size
)
1631 written_fs_size
= fssize
;
1636 /* Seek to position in FS we're creating. */
1638 mkfs_seek(uint64_t pos
, int whence
)
1640 if(whence
== SEEK_SET
) pos
+= fs_offset_bytes
;
1643 if((lseek64(fd
, pos
, whence
, &newpos
)) < 0)
1644 err(1, "mkfs_seek: lseek64 failed");
1648 if((newpos
=lseek(fd
, pos
, whence
)) == (off_t
) -1)
1649 err(1, "mkfs_seek: lseek failed");