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) */
94 int extra_space_percent
;
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);
141 void detect_fs_size(void);
142 void sizeup_dir(void);
143 void detect_size(void);
145 static int bitmapsize(uint32_t nr_bits
, size_t block_size
);
147 /*================================================================
148 * mkfs - make filesystem
149 *===============================================================*/
154 int nread
, mode
, usrid
, grpid
, ch
;
155 block_t blocks
, maxblocks
;
160 char *token
[MAX_TOKENS
], line
[LINE_LEN
];
163 /* Get two times, the current time and the mod time of the binary of
164 * mkfs itself. When the -d flag is used, the later time is put into
165 * the i_mtimes of all the files. This feature is useful when
166 * producing a set of file systems, and one wants all the times to be
167 * identical. First you set the time of the mkfs binary to what you
170 current_time
= time((time_t *) 0); /* time mkfs is being run */
171 stat(argv
[0], &statbuf
);
172 bin_time
= statbuf
.st_mtime
; /* time when mkfs binary was last modified */
174 /* Process switches. */
178 inodes_per_block
= 0;
180 extra_space_percent
= 0;
181 while ((ch
= getopt(argc
, argv
, "b:di:lotB:x:")) != EOF
)
184 blocks
= strtoul(optarg
, (char **) NULL
, 0);
188 current_time
= bin_time
;
191 i
= strtoul(optarg
, (char **) NULL
, 0);
193 case 'l': print
= 1; break;
194 case 'o': override
= 1; break;
195 case 't': donttest
= 1; break;
196 case 'B': block_size
= atoi(optarg
); break;
197 case 'x': extra_space_percent
= atoi(optarg
); break;
201 if (argc
== optind
) usage();
203 /* Percentage of extra size must be nonnegative.
204 * It can legitimately be bigger than 100 but has to make some sort of sense.
206 if(extra_space_percent
< 0 || extra_space_percent
> 2000) usage();
209 if(!block_size
) block_size
= _MAX_BLOCK_SIZE
; /* V3 default block size */
210 if(block_size
%SECTOR_SIZE
|| block_size
< _MIN_BLOCK_SIZE
) {
211 fprintf(stderr
, "block size must be multiple of sector (%d) "
212 "and at least %d bytes\n",
213 SECTOR_SIZE
, _MIN_BLOCK_SIZE
);
214 pexit("specified block size illegal");
216 if(block_size
%V2_INODE_SIZE
) {
217 fprintf(stderr
, "block size must be a multiple of inode size (%d bytes)\n",
219 pexit("specified block size illegal");
223 zone_shift
= 0; /* for future use */
224 zone_size
= 1 << zone_shift
; /* nr of blocks per zone */
226 if(!inodes_per_block
)
227 inodes_per_block
= V2_INODES_PER_BLOCK(block_size
);
229 /* now that the block size is known, do buffer allocations where
232 zero
= alloc_block();
233 bzero(zero
, block_size
);
235 /* Determine the size of the device if not specified as -b or proto. */
236 maxblocks
= sizeup(argv
[optind
]);
237 if (argc
- optind
== 1 && blocks
== 0) {
239 /* blocks == 0 is checked later, but leads to a funny way of
240 * reporting a 0-sized device (displays usage).
243 fprintf(stderr
, "%s: zero size device.\n", progname
);
248 /* The remaining args must be 'special proto', or just 'special' if the
249 * no. of blocks has already been specified.
251 if (argc
- optind
!= 2 && (argc
- optind
!= 1 || blocks
== 0)) usage();
253 if (blocks
> maxblocks
) {
254 fprintf(stderr
, "%s: %s: number of blocks too large for device.\n",
255 progname
, argv
[optind
]);
260 check_mtab(argv
[optind
]);
262 /* Check and start processing proto. */
263 optarg
= argv
[++optind
];
264 if (optind
< argc
&& (proto
= fopen(optarg
, "r")) != NULL
) {
265 /* Prototype file is readable. */
267 getline(line
, token
); /* skip boot block info */
269 /* Read the line with the block and inode counts. */
270 getline(line
, token
);
271 blocks
= atol(token
[0]);
272 inodes
= atoi(token
[1]);
274 /* Process mode line for root directory. */
275 getline(line
, token
);
276 mode
= mode_con(token
[0]);
277 usrid
= atoi(token
[1]);
278 grpid
= atoi(token
[2]);
280 if(blocks
<= 0 && inodes
<= 0){
281 block_t extrablocks
= 0;
282 ino_t extrainodes
= 0;
283 if(blocks
< 0) extrablocks
= -blocks
;
284 if(inodes
< 0) extrainodes
= -inodes
;
286 blocks
= blockcount
+ extrablocks
;
287 inodes
= inocount
+ extrainodes
;
288 blocks
+= blocks
*extra_space_percent
/100;
289 inodes
+= inodes
*extra_space_percent
/100;
290 printf("dynamically sized filesystem: %d blocks, %d inodes\n", blocks
,
291 (unsigned int) inodes
);
296 /* Maybe the prototype file is just a size. Check. */
297 blocks
= strtoul(optarg
, (char **) NULL
, 0);
298 if (blocks
== 0) pexit("Can't open prototype file");
302 uint32_t kb
= div64u(mul64u(blocks
, block_size
), 1024);
304 uint32_t kb
= ((unsigned long long) blocks
* block_size
) / 1024;
307 if (kb
>= 100000) i
= kb
/ 4;
309 /* round up to fill inode block */
310 i
+= inodes_per_block
- 1;
311 i
= i
/ inodes_per_block
* inodes_per_block
;
313 if (blocks
< 5) pexit("Block count too small");
314 if (i
< 1) pexit("Inode count too small");
317 /* Make simple file system of the given size, using defaults. */
329 bytes
= 1 + blocks
/8;
330 if(!(umap_array
= malloc(bytes
))) {
331 fprintf(stderr
, "mkfs: can't allocate block bitmap (%u bytes).\n",
335 umap_array_elements
= bytes
;
339 special(argv
[--optind
]);
346 testb
= (short *) alloc_block();
348 /* Try writing the last block of partition or diskette. */
349 if(lseek64(fd
, mul64u(blocks
- 1, block_size
), SEEK_SET
, NULL
) < 0) {
350 pexit("couldn't seek to last block to test size (1)");
354 testb
[block_size
/2-1] = 0x1F2F;
355 if ((w
=write(fd
, (char *) testb
, block_size
)) != block_size
) {
356 if(w
< 0) perror("write");
357 printf("%d/%u\n", w
, block_size
);
358 pexit("File system is too big for minor device (write)");
360 sync(); /* flush write, so if error next read fails */
361 if(lseek64(fd
, mul64u(blocks
- 1, block_size
), SEEK_SET
, NULL
) < 0) {
362 pexit("couldn't seek to last block to test size (2)");
366 nread
= read(fd
, (char *) testb
, block_size
);
367 if (nread
!= block_size
|| testb
[0] != 0x3245 || testb
[1] != 0x11FF ||
368 testb
[block_size
/2-1] != 0x1F2F) {
369 if(nread
< 0) perror("read");
370 printf("nread = %d\n", nread
);
371 printf("testb = 0x%x 0x%x 0x%x\n", testb
[0], testb
[1], testb
[block_size
-1]);
372 pexit("File system is too big for minor device (read)");
374 lseek64(fd
, mul64u(blocks
- 1, block_size
), SEEK_SET
, NULL
);
377 if (write(fd
, (char *) testb
, block_size
) != block_size
)
378 pexit("File system is too big for minor device (write2)");
379 lseek(fd
, 0L, SEEK_SET
);
384 /* Make the file-system */
388 put_block((block_t
) 0, zero
); /* Write a null boot block. */
390 zones
= nrblocks
>> zone_shift
;
392 super(zones
, inodes
);
394 root_inum
= alloc_inode(mode
, usrid
, grpid
);
396 if (simple
== 0) eat_dir(root_inum
);
398 if (print
) print_fs();
405 /*================================================================
406 * detect_fs_size - determine image size dynamically
407 *===============================================================*/
408 void detect_fs_size()
410 uint32_t point
= ftell(proto
);
412 inocount
= 1; /* root directory node */
419 initb
= bitmapsize((uint32_t) (1 + inocount
), block_size
);
420 initb
+= bitmapsize((uint32_t) zonecount
, block_size
);
421 initb
+= START_BLOCK
;
422 initb
+= (inocount
+ inodes_per_block
- 1) / inodes_per_block
;
423 initb
= (initb
+ (1 << zone_shift
) - 1) >> zone_shift
;
425 blockcount
= initb
+zonecount
*zone_size
;
426 fseek(proto
, point
, SEEK_SET
);
431 char *token
[MAX_TOKENS
], *p
;
436 zone_t dir_zones
= 0;
439 nr_dzones
= V2_NR_DZONES
;
442 getline(line
, token
);
445 dir_zones
= (dir_entries
/ (NR_DIR_ENTRIES(block_size
) * zone_size
));
446 if(dir_entries
% (NR_DIR_ENTRIES(block_size
) * zone_size
))
448 /* Assumes directory fits in direct blocks */
449 zonecount
+= dir_zones
;
459 } else if (*p
== 'b' || *p
== 'c') {
461 } else if (*p
== 's') {
462 zonecount
++; /* Symlink contents is always stored a block */
464 if ((f
= fopen(token
[4], "r")) == NULL
) {
465 fprintf(stderr
, "%s: Can't open %s: %s\n",
466 progname
, token
[4], strerror(errno
));
467 pexit("dynamic size detection failed");
469 if (fseek(f
, 0, SEEK_END
) < 0) {
470 fprintf(stderr
, "%s: Can't seek to end of %s\n",
472 pexit("dynamic size detection failed");
476 zone_t fzones
= (size
/ (zone_size
* block_size
));
477 if (size
% (zone_size
* block_size
))
479 if (fzones
> nr_dzones
)
480 fzones
++; /* Assumes files fit within single indirect */
487 /*================================================================
488 * sizeup - determine device size
489 *===============================================================*/
490 block_t
sizeup(device
)
502 if ((fd
= open(device
, O_RDONLY
)) == -1) {
504 perror("sizeup open");
509 if(minix_sizeup(device
, &bytes
) < 0) {
514 d
= div64u(bytes
, block_size
);
515 rem
= rem64u(bytes
, block_size
);
517 resize
= add64u(mul64u(d
, block_size
), rem
);
518 if(cmp64(resize
, bytes
) != 0) {
520 fprintf(stderr
, "mkfs: truncating FS at %u blocks\n", d
);
523 size
= lseek(fd
, 0, SEEK_END
);
524 if (size
== (off_t
) -1) {
525 fprintf(stderr
, "Cannot get device size fd=%d\n", fd
);
528 d
= size
/ block_size
;
537 static int bitmapsize(nr_bits
, block_size
)
543 nr_blocks
= (int) (nr_bits
/ FS_BITS_PER_BLOCK(block_size
));
544 if (((uint32_t) nr_blocks
* FS_BITS_PER_BLOCK(block_size
)) < nr_bits
) ++nr_blocks
;
548 /*================================================================
549 * super - construct a superblock
550 *===============================================================*/
552 void super(zones
, inodes
)
562 struct super_block
*sup
;
567 for (cp
= buf
; cp
< &buf
[block_size
]; cp
++) *cp
= 0;
568 sup
= (struct super_block
*) buf
; /* lint - might use a union */
570 /* The assumption is that mkfs will create a clean FS. */
571 sup
->s_flags
= MFSFLAG_CLEAN
;
573 sup
->s_ninodes
= inodes
;
574 sup
->s_nzones
= 0; /* not used in V2 - 0 forces errors early */
575 sup
->s_zones
= zones
;
577 #define BIGGERBLOCKS "Please try a larger block size for an FS of this size.\n"
578 sup
->s_imap_blocks
= nb
= bitmapsize((uint32_t) (1 + inodes
), block_size
);
579 if(sup
->s_imap_blocks
!= nb
) {
580 fprintf(stderr
, "mkfs: too many inode bitmap blocks.\n" BIGGERBLOCKS
);
583 sup
->s_zmap_blocks
= nb
= bitmapsize((uint32_t) zones
, block_size
);
584 if(nb
!= sup
->s_zmap_blocks
) {
585 fprintf(stderr
, "mkfs: too many block bitmap blocks.\n" BIGGERBLOCKS
);
588 inode_offset
= START_BLOCK
+ sup
->s_imap_blocks
+ sup
->s_zmap_blocks
;
589 inodeblks
= (inodes
+ inodes_per_block
- 1) / inodes_per_block
;
590 initblks
= inode_offset
+ inodeblks
;
591 sup
->s_firstdatazone_old
= nb
=
592 (initblks
+ (1 << zone_shift
) - 1) >> zone_shift
;
593 if(nb
>= zones
) pexit("bit maps too large");
594 if(nb
!= sup
->s_firstdatazone_old
) {
595 /* The field is too small to store the value. Fortunately, the value
596 * can be computed from other fields. We set the on-disk field to zero
597 * to indicate that it must not be used. Eventually, we can always set
598 * the on-disk field to zero, and stop using it.
600 sup
->s_firstdatazone_old
= 0;
602 sup
->s_firstdatazone
= nb
;
603 zoff
= sup
->s_firstdatazone
- 1;
604 sup
->s_log_zone_size
= zone_shift
;
606 v2sq
= (zone_t
) V2_INDIRECTS(block_size
) * V2_INDIRECTS(block_size
);
607 zo
= V2_NR_DZONES
+ (zone_t
) V2_INDIRECTS(block_size
) + v2sq
;
609 sup
->s_magic
= SUPER_V3
;
610 sup
->s_block_size
= block_size
;
611 sup
->s_disk_version
= 0;
612 #define MAX_MAX_SIZE (INT_MAX)
613 if(MAX_MAX_SIZE
/block_size
< zo
) {
614 sup
->s_max_size
= (int32_t) MAX_MAX_SIZE
;
617 sup
->s_max_size
= zo
* block_size
;
622 if (lseek(fd
, (off_t
) _STATIC_BLOCK_SIZE
, SEEK_SET
) == (off_t
) -1) {
623 pexit("super() couldn't seek");
625 if (write(fd
, buf
, _STATIC_BLOCK_SIZE
) != _STATIC_BLOCK_SIZE
) {
626 pexit("super() couldn't write");
629 /* Clear maps and inodes. */
630 for (i
= START_BLOCK
; i
< initblks
; i
++) put_block((block_t
) i
, zero
);
632 next_zone
= sup
->s_firstdatazone
;
635 zone_map
= INODE_MAP
+ sup
->s_imap_blocks
;
637 insert_bit(zone_map
, 0); /* bit zero must always be allocated */
638 insert_bit((block_t
) INODE_MAP
, 0); /* inode zero not used but
639 * must be allocated */
645 /*================================================================
646 * rootdir - install the root directory
647 *===============================================================*/
654 add_zone(inode
, z
, 2 * sizeof(struct direct
), current_time
);
655 enter_dir(inode
, ".", inode
);
656 enter_dir(inode
, "..", inode
);
661 void enter_symlink(ino_t inode
, char *link
)
669 put_block((z
<< zone_shift
), buf
);
671 add_zone(inode
, z
, (size_t) strlen(link
), current_time
);
677 /*================================================================
678 * eat_dir - recursively install directory
679 *===============================================================*/
683 /* Read prototype lines and set up directory. Recurse if need be. */
684 char *token
[MAX_TOKENS
], *p
;
686 int mode
, usrid
, grpid
, maj
, min
, f
;
692 getline(line
, token
);
694 if (*p
== '$') return;
697 usrid
= atoi(token
[2]);
698 grpid
= atoi(token
[3]);
699 n
= alloc_inode(mode
, usrid
, grpid
);
701 /* Enter name in directory and update directory's size. */
702 enter_dir(parent
, token
[0], n
);
703 incr_size(parent
, sizeof(struct direct
));
705 /* Check to see if file is directory or special. */
708 /* This is a directory. */
709 z
= alloc_zone(); /* zone for new directory */
710 add_zone(n
, z
, 2 * sizeof(struct direct
), current_time
);
711 enter_dir(n
, ".", n
);
712 enter_dir(n
, "..", parent
);
716 } else if (*p
== 'b' || *p
== 'c') {
718 maj
= atoi(token
[4]);
719 min
= atoi(token
[5]);
721 if (token
[6]) size
= atoi(token
[6]);
722 size
= block_size
* size
;
723 add_zone(n
, (zone_t
) (makedev(maj
,min
)), size
, current_time
);
724 } else if (*p
== 's') {
725 enter_symlink(n
, token
[4]);
727 /* Regular file. Go read it. */
728 if ((f
= open(token
[4], O_RDONLY
)) < 0) {
729 fprintf(stderr
, "%s: Can't open %s: %s\n",
730 progname
, token
[4], strerror(errno
));
739 /*================================================================
740 * eat_file - copy file to MINIX
741 *===============================================================*/
742 /* Zonesize >= blocksize */
743 void eat_file(inode
, f
)
755 for (i
= 0, j
= 0; i
< zone_size
; i
++, j
+= ct
) {
756 for (k
= 0; k
< block_size
; k
++) buf
[k
] = 0;
757 if ((ct
= read(f
, buf
, block_size
)) > 0) {
758 if (i
== 0) z
= alloc_zone();
759 put_block((z
<< zone_shift
) + i
, buf
);
762 timeval
= (dflag
? current_time
: file_time(f
));
763 if (ct
) add_zone(inode
, z
, (size_t) j
, timeval
);
764 } while (ct
== block_size
);
769 /*================================================================
770 * directory & inode management assist group
771 *===============================================================*/
772 void enter_dir(parent
, name
, child
)
776 /* Enter child in parent directory */
777 /* Works for dir > 1 block and zone > block */
778 unsigned int i
, j
, k
, l
, off
;
782 struct direct
*dir_entry
;
786 b
= ((parent
- 1) / inodes_per_block
) + inode_offset
;
787 off
= (parent
- 1) % inodes_per_block
;
789 if(!(dir_entry
= malloc(NR_DIR_ENTRIES(block_size
) * sizeof(*dir_entry
))))
790 pexit("couldn't allocate directory entry");
792 if(!(ino2
= malloc(V2_INODES_PER_BLOCK(block_size
) * sizeof(*ino2
))))
793 pexit("couldn't allocate block of inodes entry");
795 get_block(b
, (char *) ino2
);
796 nr_dzones
= V2_NR_DZONES
;
797 for (k
= 0; k
< nr_dzones
; k
++) {
798 z
= ino2
[off
].d2_zone
[k
];
801 ino2
[off
].d2_zone
[k
] = z
;
804 for (l
= 0; l
< zone_size
; l
++) {
805 get_block((z
<< zone_shift
) + l
, (char *) dir_entry
);
806 for (i
= 0; i
< NR_DIR_ENTRIES(block_size
); i
++) {
807 if (dir_entry
[i
].mfs_d_ino
== 0) {
808 dir_entry
[i
].mfs_d_ino
= child
;
810 p2
= dir_entry
[i
].mfs_d_name
;
811 j
= sizeof(dir_entry
[i
].mfs_d_name
);
817 put_block((z
<< zone_shift
) + l
, (char *) dir_entry
);
818 put_block(b
, (char *) ino2
);
827 printf("Directory-inode %lu beyond direct blocks. Could not enter %s\n",
833 void add_zone(ino_t n
, zone_t z
, size_t bytes
, uint32_t cur_time
)
835 /* Add zone z to inode n. The file has grown by 'bytes' bytes. */
844 if(!(blk
= malloc(V2_INDIRECTS(block_size
)*sizeof(*blk
))))
845 pexit("Couldn't allocate indirect block");
847 if(!(inode
= malloc(V2_INODES_PER_BLOCK(block_size
)*sizeof(*inode
))))
848 pexit("Couldn't allocate block of inodes");
850 b
= ((n
- 1) / V2_INODES_PER_BLOCK(block_size
)) + inode_offset
;
851 off
= (n
- 1) % V2_INODES_PER_BLOCK(block_size
);
852 get_block(b
, (char *) inode
);
855 p
->d2_mtime
= cur_time
;
856 for (i
= 0; i
< V2_NR_DZONES
; i
++)
857 if (p
->d2_zone
[i
] == 0) {
859 put_block(b
, (char *) inode
);
864 put_block(b
, (char *) inode
);
866 /* File has grown beyond a small file. */
867 if (p
->d2_zone
[V2_NR_DZONES
] == 0) p
->d2_zone
[V2_NR_DZONES
] = alloc_zone();
868 indir
= p
->d2_zone
[V2_NR_DZONES
];
869 put_block(b
, (char *) inode
);
870 b
= indir
<< zone_shift
;
871 get_block(b
, (char *) blk
);
872 for (i
= 0; i
< V2_INDIRECTS(block_size
); i
++)
875 put_block(b
, (char *) blk
);
880 pexit("File has grown beyond single indirect");
887 /* Increment the link count to inode n */
889 static int enter
= 0;
894 b
= ((n
- 1) / inodes_per_block
) + inode_offset
;
895 off
= (n
- 1) % inodes_per_block
;
897 static d2_inode
*inode2
= NULL
;
900 n
= sizeof(*inode2
) * V2_INODES_PER_BLOCK(block_size
);
901 if(!inode2
&& !(inode2
= malloc(n
)))
902 pexit("couldn't allocate a block of inodes");
904 get_block(b
, (char *) inode2
);
905 inode2
[off
].d2_nlinks
++;
906 put_block(b
, (char *) inode2
);
912 void incr_size(n
, count
)
916 /* Increment the file-size in inode n */
920 b
= ((n
- 1) / inodes_per_block
) + inode_offset
;
921 off
= (n
- 1) % inodes_per_block
;
924 if(!(inode2
= malloc(V2_INODES_PER_BLOCK(block_size
) * sizeof(*inode2
))))
925 pexit("couldn't allocate a block of inodes");
927 get_block(b
, (char *) inode2
);
928 inode2
[off
].d2_size
+= count
;
929 put_block(b
, (char *) inode2
);
935 /*================================================================
936 * allocation assist group
937 *===============================================================*/
938 static ino_t
alloc_inode(mode
, usrid
, grpid
)
939 int mode
, usrid
, grpid
;
946 if (num
> nrinodes
) {
947 fprintf(stderr
, "have %d inodoes\n", nrinodes
);
948 pexit("File system does not have enough inodes");
950 b
= ((num
- 1) / inodes_per_block
) + inode_offset
;
951 off
= (num
- 1) % inodes_per_block
;
955 if(!(inode2
= malloc(V2_INODES_PER_BLOCK(block_size
) * sizeof(*inode2
))))
956 pexit("couldn't allocate a block of inodes");
958 get_block(b
, (char *) inode2
);
959 inode2
[off
].d2_mode
= mode
;
960 inode2
[off
].d2_uid
= usrid
;
961 inode2
[off
].d2_gid
= grpid
;
962 put_block(b
, (char *) inode2
);
967 /* Set the bit in the bit map. */
968 /* DEBUG FIXME. This assumes the bit is in the first inode map block. */
969 insert_bit((block_t
) INODE_MAP
, (int) num
);
974 static zone_t
alloc_zone()
976 /* Allocate a new zone */
977 /* Works for zone > block */
984 if ((b
+ zone_size
) > nrblocks
)
985 pexit("File system not big enough for all the files");
986 for (i
= 0; i
< zone_size
; i
++)
987 put_block(b
+ i
, zero
); /* give an empty zone */
988 /* DEBUG FIXME. This assumes the bit is in the first zone map block. */
989 insert_bit(zone_map
, (int) (z
- zoff
)); /* lint, NOT OK because
990 * z hasn't been broken
997 void insert_bit(block
, bit
)
1001 /* Insert 'count' bits in the bitmap */
1003 #if defined(__minix)
1009 #if defined(__minix)
1010 buf
= (bitchunk_t
*) alloc_block();
1012 buf
= (uint32_t *) alloc_block();
1015 get_block(block
, (char *) buf
);
1016 #if defined(__minix)
1017 w
= bit
/ (8 * sizeof(bitchunk_t
));
1018 s
= bit
% (8 * sizeof(bitchunk_t
));
1020 w
= bit
/ (8 * sizeof(uint32_t));
1021 s
= bit
% (8 * sizeof(uint32_t));
1024 put_block(block
, (char *) buf
);
1030 /*================================================================
1031 * proto-file processing assist group
1032 *===============================================================*/
1036 /* Convert string to mode */
1037 int o1
, o2
, o3
, mode
;
1046 mode
= (o1
<< 6) | (o2
<< 3) | o3
;
1047 if (c1
== 'd') mode
|= S_IFDIR
;
1048 if (c1
== 'b') mode
|= S_IFBLK
;
1049 if (c1
== 'c') mode
|= S_IFCHR
;
1050 if (c1
== 's') mode
|= S_IFLNK
;
1051 if (c1
== '-') mode
|= S_IFREG
;
1052 if (c2
== 'u') mode
|= S_ISUID
;
1053 if (c3
== 'g') mode
|= S_ISGID
;
1057 void getline(line
, parse
)
1058 char *parse
[MAX_TOKENS
];
1059 char line
[LINE_LEN
];
1061 /* Read a line and break it up in tokens */
1066 for (k
= 0; k
< MAX_TOKENS
; k
++) parse
[k
] = 0;
1067 for (k
= 0; k
< LINE_LEN
; k
++) line
[k
] = 0;
1072 if (++k
> LINE_LEN
) pexit("Line too long");
1074 if (d
== EOF
) pexit("Unexpected end-of-file");
1076 if (*p
== '\n') lct
++;
1077 if (*p
== ' ' || *p
== '\t') *p
= 0;
1091 if (c
== '\n') return;
1092 if (c
== 0) continue;
1096 } while (c
!= 0 && c
!= '\n');
1101 /*================================================================
1103 *===============================================================*/
1104 void check_mtab(device
)
1105 char *device
; /* /dev/hd1 or whatever */
1107 /* Check to see if the special file named in s is mounted. */
1108 #if defined(__minix)
1111 char special
[PATH_MAX
+ 1], mounted_on
[PATH_MAX
+ 1], version
[10], rw_flag
[10];
1113 r
= stat(device
, &sb
);
1116 if (errno
== ENOENT
)
1117 return; /* Does not exist, and therefore not mounted. */
1118 fprintf(stderr
, "%s: stat %s failed: %s\n",
1119 progname
, device
, strerror(errno
));
1122 if (!S_ISBLK(sb
.st_mode
))
1124 /* Not a block device and therefore not mounted. */
1128 if (load_mtab("mkfs") < 0) return;
1130 n
= get_mtab_entry(special
, mounted_on
, version
, rw_flag
);
1132 if (strcmp(device
, special
) == 0) {
1133 /* Can't mkfs on top of a mounted file system. */
1134 fprintf(stderr
, "%s: %s is mounted on %s\n",
1135 progname
, device
, mounted_on
);
1139 #elif defined(__linux__)
1140 /* XXX: this code is copyright Theodore T'so and distributed under the GPLv2. Rewrite.
1144 dev_t file_dev
=0, file_rdev
=0;
1148 char *mtab_file
= "/proc/mounts";
1150 if ((f
= setmntent (mtab_file
, "r")) == NULL
)
1153 if (stat(device
, &st_buf
) == 0) {
1154 if (S_ISBLK(st_buf
.st_mode
)) {
1155 file_rdev
= st_buf
.st_rdev
;
1157 file_dev
= st_buf
.st_dev
;
1158 file_ino
= st_buf
.st_ino
;
1162 while ((mnt
= getmntent (f
)) != NULL
) {
1163 if (strcmp(device
, mnt
->mnt_fsname
) == 0)
1165 if (stat(mnt
->mnt_fsname
, &st_buf
) == 0) {
1166 if (S_ISBLK(st_buf
.st_mode
)) {
1167 if (file_rdev
&& (file_rdev
== st_buf
.st_rdev
))
1170 if (file_dev
&& ((file_dev
== st_buf
.st_dev
) &&
1171 (file_ino
== st_buf
.st_ino
)))
1179 * Do an extra check to see if this is the root device. We
1180 * can't trust /etc/mtab, and /proc/mounts will only list
1181 * /dev/root for the root filesystem. Argh. Instead we
1182 * check if the given device has the same major/minor number
1183 * as the device that the root directory is on.
1185 if (file_rdev
&& stat("/", &st_buf
) == 0) {
1186 if (st_buf
.st_dev
== file_rdev
) {
1192 /* Validate the entry in case /etc/mtab is out of date */
1194 * We need to be paranoid, because some broken distributions
1195 * (read: Slackware) don't initialize /etc/mtab before checking
1196 * all of the non-root filesystems on the disk.
1198 if (stat(mnt
->mnt_dir
, &st_buf
) < 0) {
1199 if (errno
== ENOENT
) {
1204 if (file_rdev
&& (st_buf
.st_dev
!= file_rdev
)) {
1208 fprintf(stderr
, "Device %s is mounted, exiting\n", device
);
1212 * Check to see if we're referring to the root filesystem.
1213 * If so, do a manual check to see if we can open /etc/mtab
1214 * read/write, since if the root is mounted read/only, the
1215 * contents of /etc/mtab may not be accurate.
1217 if (!strcmp(mnt
->mnt_dir
, "/")) {
1219 fprintf(stderr
, "Device %s is mounted as root file system!\n",
1227 if ((stat(device
, &st_buf
) != 0) ||
1228 !S_ISBLK(st_buf
.st_mode
))
1230 fd
= open(device
, O_RDONLY
| O_EXCL
);
1232 if (errno
== EBUSY
) {
1233 fprintf(stderr
, "Device %s is used by the system\n", device
);
1243 fprintf(stderr
, "Error while checking if device %s is mounted\n", device
);
1249 uint32_t file_time(f
)
1253 struct stat statbuf
;
1255 return(statbuf
.st_mtime
);
1256 #else /* fstat not supported by DOS */
1265 fprintf(stderr
, "%s: %s\n", progname
, s
);
1267 fprintf(stderr
, "Line %d being processed when error detected.\n", lct
);
1273 void copy(from
, to
, count
)
1277 while (count
--) *to
++ = *from
++;
1284 if(!(buf
= malloc(block_size
))) {
1285 pexit("couldn't allocate filesystem buffer");
1287 bzero(buf
, block_size
);
1297 unsigned short *usbuf
;
1301 if(!(inode2
= malloc(V2_INODES_PER_BLOCK(block_size
) * sizeof(*inode2
))))
1302 pexit("couldn't allocate a block of inodes");
1304 if(!(dir
= malloc(NR_DIR_ENTRIES(block_size
)*sizeof(*dir
))))
1305 pexit("malloc of directory entry failed");
1307 usbuf
= (unsigned short *) alloc_block();
1309 get_super_block((char *) usbuf
);
1310 printf("\nSuperblock: ");
1311 for (i
= 0; i
< 8; i
++) printf("%06o ", usbuf
[i
]);
1312 get_block((block_t
) 2, (char *) usbuf
);
1313 printf("...\nInode map: ");
1314 for (i
= 0; i
< 9; i
++) printf("%06o ", usbuf
[i
]);
1315 get_block((block_t
) 3, (char *) usbuf
);
1316 printf("...\nZone map: ");
1317 for (i
= 0; i
< 9; i
++) printf("%06o ", usbuf
[i
]);
1324 for (b
= inode_offset
; k
< nrinodes
; b
++) {
1325 get_block(b
, (char *) inode2
);
1326 for (i
= 0; i
< inodes_per_block
; i
++) {
1327 k
= inodes_per_block
* (int) (b
- inode_offset
) + i
+ 1;
1329 if (k
> nrinodes
) break;
1331 if (inode2
[i
].d2_mode
!= 0) {
1332 printf("Inode %2lu: mode=", k
);
1333 printf("%06o", inode2
[i
].d2_mode
);
1334 printf(" uid=%2d gid=%2d size=",
1335 inode2
[i
].d2_uid
, inode2
[i
].d2_gid
);
1336 printf("%6d", inode2
[i
].d2_size
);
1337 printf(" zone[0]=%u\n", inode2
[i
].d2_zone
[0]);
1339 if ((inode2
[i
].d2_mode
& S_IFMT
) == S_IFDIR
) {
1340 /* This is a directory */
1341 get_block(inode2
[i
].d2_zone
[0], (char *) dir
);
1342 for (j
= 0; j
< NR_DIR_ENTRIES(block_size
); j
++)
1343 if (dir
[j
].mfs_d_ino
)
1344 printf("\tInode %2u: %s\n", dir
[j
].mfs_d_ino
, dir
[j
].mfs_d_name
);
1350 printf("%d inodes used. %d zones used.\n", next_inode
- 1, next_zone
);
1359 /* The first time a block is read, it returns all 0s, unless there has
1360 * been a write. This routine checks to see if a block has been accessed.
1366 if(w
>= umap_array_elements
) {
1367 pexit("umap array too small - this can't happen");
1371 r
= (umap_array
[w
] & mask
? 1 : 0);
1372 umap_array
[w
] |= mask
;
1379 "Usage: %s [-12dlot] [-b blocks] [-i inodes]\n"
1380 "\t[-x extra] [-B blocksize] special [proto]\n",
1385 /*================================================================
1386 * get_block & put_block for MS-DOS
1387 *===============================================================*/
1391 * These are the get_block and put_block routines
1392 * when compiling & running mkfs.c under MS-DOS.
1394 * It requires the (asembler) routines absread & abswrite
1395 * from the file diskio.asm. Since these routines just do
1396 * as they are told (read & write the sector specified),
1397 * a local cache is used to minimize the i/o-overhead for
1398 * frequently used blocks.
1400 * The global variable "file" determines whether the output
1401 * is to a disk-device or to a binary file.
1405 #define PH_SECTSIZE 512 /* size of a physical disk-sector */
1408 char *derrtab
[14] = {
1410 "disk is read-only",
1415 "internal error: bad request structure length",
1417 "unknown media type",
1419 "printer out of paper (?)",
1425 #define CACHE_SIZE 20 /* 20 block-buffers */
1429 char blockbuf
[BLOCK_SIZE
];
1433 } cache
[CACHE_SIZE
];
1436 void special(string
)
1440 if (string
[1] == ':' && string
[2] == 0) {
1441 /* Format: d: or d:fname */
1442 disk
= (string
[0] & ~32) - 'A';
1443 if (disk
> 1 && !override
) /* safety precaution */
1444 pexit("Bad drive specifier for special");
1447 if ((fd
= creat(string
, BWRITE
)) == 0)
1448 pexit("Can't open special file");
1452 void get_block(n
, buf
)
1456 /* Get a block to the user */
1457 struct cache
*bp
, *fp
;
1459 /* First access returns a zero block */
1460 if (read_and_set(n
) == 0) {
1461 copy(zero
, buf
, block_size
);
1465 /* Look for block in cache */
1467 for (bp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++) {
1468 if (bp
->blocknum
== n
) {
1469 copy(bp
, buf
, block_size
);
1474 /* Remember clean block */
1477 if (fp
->usecnt
> bp
->usecnt
) fp
= bp
;
1482 /* Block not in cache, get it */
1484 /* No clean buf, flush one */
1485 for (bp
= cache
, fp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++)
1486 if (fp
->usecnt
> bp
->usecnt
) fp
= bp
;
1487 mx_write(fp
->blocknum
, fp
);
1493 copy(fp
, buf
, block_size
);
1496 void put_block(n
, buf
)
1500 /* Accept block from user */
1501 struct cache
*fp
, *bp
;
1503 (void) read_and_set(n
);
1505 /* Look for block in cache */
1507 for (bp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++) {
1508 if (bp
->blocknum
== n
) {
1509 copy(buf
, bp
, block_size
);
1514 /* Remember clean block */
1517 if (fp
->usecnt
> bp
->usecnt
) fp
= bp
;
1522 /* Block not in cache */
1524 /* No clean buf, flush one */
1525 for (bp
= cache
, fp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++)
1526 if (fp
->usecnt
> bp
->usecnt
) fp
= bp
;
1527 mx_write(fp
->blocknum
, fp
);
1532 copy(buf
, fp
, block_size
);
1538 for (bp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++) bp
->blocknum
= -1;
1543 /* Flush all dirty blocks to disk */
1546 for (bp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++)
1548 mx_write(bp
->blocknum
, bp
);
1553 /*==================================================================
1554 * hard read & write etc.
1555 *=================================================================*/
1556 #define MAX_RETRIES 5
1559 void mx_read(blocknr
, buf
)
1564 /* Read the requested MINIX-block in core */
1565 char (*bp
)[PH_SECTSIZE
];
1566 int sectnum
, retries
, err
;
1569 lseek(fd
, (off_t
) blocknr
* block_size
, 0);
1570 if (read(fd
, buf
, block_size
) != block_size
)
1571 pexit("mx_read: error reading file");
1573 sectnum
= blocknr
* (block_size
/ PH_SECTSIZE
);
1574 for (bp
= buf
; bp
< &buf
[block_size
]; bp
++) {
1575 retries
= MAX_RETRIES
;
1577 err
= absread(disk
, sectnum
, bp
);
1578 while (err
&& --retries
);
1583 dexit("mx_read", sectnum
, err
);
1589 void mx_write(blocknr
, buf
)
1593 /* Write the MINIX-block to disk */
1594 char (*bp
)[PH_SECTSIZE
];
1595 int retries
, sectnum
, err
;
1598 lseek(fd
, blocknr
* block_size
, 0);
1599 if (write(fd
, buf
, block_size
) != block_size
) {
1600 pexit("mx_write: error writing file");
1603 sectnum
= blocknr
* (block_size
/ PH_SECTSIZE
);
1604 for (bp
= buf
; bp
< &buf
[block_size
]; bp
++) {
1605 retries
= MAX_RETRIES
;
1607 err
= abswrite(disk
, sectnum
, bp
);
1608 } while (err
&& --retries
);
1613 dexit("mx_write", sectnum
, err
);
1620 void dexit(s
, sectnum
, err
)
1624 printf("Error: %s, sector: %d, code: %d, meaning: %s\n",
1625 s
, sectnum
, err
, derrtab
[err
]);
1631 /*================================================================
1632 * get_block & put_block for UNIX
1633 *===============================================================*/
1636 void special(string
)
1639 fd
= creat(string
, 0777);
1641 fd
= open(string
, O_RDWR
);
1642 if (fd
< 0) pexit("Can't open special file");
1647 void get_block(n
, buf
)
1655 /* First access returns a zero block */
1656 if (read_and_set(n
) == 0) {
1657 copy(zero
, buf
, block_size
);
1660 lseek64(fd
, mul64u(n
, block_size
), SEEK_SET
, NULL
);
1661 k
= read(fd
, buf
, block_size
);
1662 if (k
!= block_size
) {
1663 pexit("get_block couldn't read");
1667 void get_super_block(buf
)
1674 if(lseek(fd
, (off_t
) SUPER_BLOCK_BYTES
, SEEK_SET
) < 0) {
1676 pexit("seek failed");
1678 k
= read(fd
, buf
, _STATIC_BLOCK_SIZE
);
1679 if (k
!= _STATIC_BLOCK_SIZE
) {
1680 pexit("get_super_block couldn't read");
1684 void put_block(n
, buf
)
1688 /* Write a block. */
1690 (void) read_and_set(n
);
1692 /* XXX - check other lseeks too. */
1693 if (lseek64(fd
, mul64u(n
, block_size
), SEEK_SET
, NULL
) == (off_t
) -1) {
1694 pexit("put_block couldn't seek");
1696 if (write(fd
, buf
, block_size
) != block_size
) {
1697 pexit("put_block couldn't write");
1702 /* Dummy routines to keep source file clean from #ifdefs */