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>
24 #include <minix/config.h>
25 #include <minix/const.h>
26 #include <minix/type.h>
27 #include <minix/minlib.h>
28 #include "../../servers/mfs/const.h"
29 #if (MACHINE == IBM_PC)
30 #include <minix/partition.h>
31 #include <minix/u64.h>
32 #include <sys/ioctl.h>
39 #define EXTERN /* get rid of EXTERN by making it null */
40 #include "../../servers/mfs/super.h"
41 #include "../../servers/mfs/type.h"
42 #include "../../servers/mfs/inode.h"
43 #include <minix/fslib.h>
46 #define max(a,b) ((a) > (b) ? (a) : (b))
60 #define BIT_MAP_SHIFT 13
61 #define INODE_MAX ((unsigned) 65535)
65 maybedefine O_RDONLY
4 /* O_RDONLY | BINARY_BIT */
66 maybedefine BWRITE
5 /* O_WRONLY | BINARY_BIT */
72 int next_zone
, next_inode
, zone_size
, zone_shift
= 0, zoff
;
74 int inode_offset
, lct
= 0, disk
, fd
, print
= 0, file
= 0;
75 unsigned int nrinodes
;
76 int override
= 0, simple
= 0, dflag
;
77 int donttest
; /* skip test if it fits on medium */
80 long current_time
, bin_time
;
82 char *umap_array
; /* bit map tells if block read yet */
83 int umap_array_elements
= 0;
84 block_t zone_map
; /* where is zone map? (depends on # inodes) */
87 unsigned int block_size
;
91 _PROTOTYPE(int main
, (int argc
, char **argv
));
92 _PROTOTYPE(block_t sizeup
, (char *device
));
93 _PROTOTYPE(void super
, (zone_t zones
, Ino_t inodes
));
94 _PROTOTYPE(void rootdir
, (Ino_t inode
));
95 _PROTOTYPE(void eat_dir
, (Ino_t parent
));
96 _PROTOTYPE(void eat_file
, (Ino_t inode
, int f
));
97 _PROTOTYPE(void enter_dir
, (Ino_t parent
, char *name
, Ino_t child
));
98 _PROTOTYPE(void incr_size
, (Ino_t n
, long count
));
99 _PROTOTYPE(PRIVATE ino_t alloc_inode
, (int mode
, int usrid
, int grpid
));
100 _PROTOTYPE(PRIVATE zone_t alloc_zone
, (void));
101 _PROTOTYPE(void add_zone
, (Ino_t n
, zone_t z
, long bytes
, long cur_time
));
102 _PROTOTYPE(void add_z_1
, (Ino_t n
, zone_t z
, long bytes
, long cur_time
));
103 _PROTOTYPE(void add_z_2
, (Ino_t n
, zone_t z
, long bytes
, long cur_time
));
104 _PROTOTYPE(void incr_link
, (Ino_t n
));
105 _PROTOTYPE(void insert_bit
, (block_t block
, int bit
));
106 _PROTOTYPE(int mode_con
, (char *p
));
107 _PROTOTYPE(void getline
, (char line
[LINE_LEN
], char *parse
[MAX_TOKENS
]));
108 _PROTOTYPE(void check_mtab
, (char *devname
));
109 _PROTOTYPE(long file_time
, (int f
));
110 _PROTOTYPE(void pexit
, (char *s
));
111 _PROTOTYPE(void copy
, (char *from
, char *to
, int count
));
112 _PROTOTYPE(void print_fs
, (void));
113 _PROTOTYPE(int read_and_set
, (block_t n
));
114 _PROTOTYPE(void special
, (char *string
));
115 _PROTOTYPE(void get_block
, (block_t n
, char *buf
));
116 _PROTOTYPE(void get_super_block
, (char *buf
));
117 _PROTOTYPE(void put_block
, (block_t n
, char *buf
));
118 _PROTOTYPE(void cache_init
, (void));
119 _PROTOTYPE(void flush
, (void));
120 _PROTOTYPE(void mx_read
, (int blocknr
, char *buf
));
121 _PROTOTYPE(void mx_write
, (int blocknr
, char *buf
));
122 _PROTOTYPE(void dexit
, (char *s
, int sectnum
, int err
));
123 _PROTOTYPE(void usage
, (void));
124 _PROTOTYPE(char *alloc_block
, (void));
126 /*================================================================
127 * mkfs - make filesystem
128 *===============================================================*/
133 int nread
, mode
, usrid
, grpid
, ch
;
134 block_t blocks
, maxblocks
;
139 char *token
[MAX_TOKENS
], line
[LINE_LEN
];
142 /* Get two times, the current time and the mod time of the binary of
143 * mkfs itself. When the -d flag is used, the later time is put into
144 * the i_mtimes of all the files. This feature is useful when
145 * producing a set of file systems, and one wants all the times to be
146 * identical. First you set the time of the mkfs binary to what you
149 current_time
= time((time_t *) 0); /* time mkfs is being run */
150 stat(argv
[0], &statbuf
);
151 bin_time
= statbuf
.st_mtime
; /* time when mkfs binary was last modified */
153 /* Process switches. */
158 inodes_per_block
= 0;
160 while ((ch
= getopt(argc
, argv
, "12b:di:lotB:")) != EOF
)
164 inodes_per_block
= V1_INODES_PER_BLOCK
;
170 blocks
= strtoul(optarg
, (char **) NULL
, 0);
174 current_time
= bin_time
;
177 i
= strtoul(optarg
, (char **) NULL
, 0);
179 case 'l': print
= 1; break;
180 case 'o': override
= 1; break;
181 case 't': donttest
= 1; break;
182 case 'B': block_size
= atoi(optarg
); break;
186 if (argc
== optind
) usage();
188 if(fs_version
== 3) {
189 if(!block_size
) block_size
= _MAX_BLOCK_SIZE
; /* V3 default block size */
190 if(block_size
%SECTOR_SIZE
|| block_size
< _MIN_BLOCK_SIZE
) {
191 fprintf(stderr
, "block size must be multiple of sector (%d) "
192 "and at least %d bytes\n",
193 SECTOR_SIZE
, _MIN_BLOCK_SIZE
);
194 pexit("specified block size illegal");
196 if(block_size
%V2_INODE_SIZE
) {
197 fprintf(stderr
, "block size must be a multiple of inode size (%d bytes)\n",
199 pexit("specified block size illegal");
203 pexit("Can't specify a block size if FS version is <3");
205 block_size
= _STATIC_BLOCK_SIZE
; /* V1/V2 block size */
208 if(!inodes_per_block
)
209 inodes_per_block
= V2_INODES_PER_BLOCK(block_size
);
211 /* now that the block size is known, do buffer allocations where
214 zero
= alloc_block();
215 bzero(zero
, block_size
);
217 /* Determine the size of the device if not specified as -b or proto. */
218 maxblocks
= sizeup(argv
[optind
]);
219 if (argc
- optind
== 1 && blocks
== 0) {
221 /* blocks == 0 is checked later, but leads to a funny way of
222 * reporting a 0-sized device (displays usage).
225 fprintf(stderr
, "%s: zero size device.\n", progname
);
230 /* The remaining args must be 'special proto', or just 'special' if the
231 * no. of blocks has already been specified.
233 if (argc
- optind
!= 2 && (argc
- optind
!= 1 || blocks
== 0)) usage();
235 if (blocks
> maxblocks
) {
236 fprintf(stderr
, "%s: %s: number of blocks too large for device.\n",
237 progname
, argv
[optind
]);
242 check_mtab(argv
[optind
]);
244 /* Check and start processing proto. */
245 optarg
= argv
[++optind
];
246 if (optind
< argc
&& (proto
= fopen(optarg
, "r")) != NULL
) {
247 /* Prototype file is readable. */
249 getline(line
, token
); /* skip boot block info */
251 /* Read the line with the block and inode counts. */
252 getline(line
, token
);
253 blocks
= atol(token
[0]);
254 inodes
= atoi(token
[1]);
256 /* Process mode line for root directory. */
257 getline(line
, token
);
258 mode
= mode_con(token
[0]);
259 usrid
= atoi(token
[1]);
260 grpid
= atoi(token
[2]);
264 /* Maybe the prototype file is just a size. Check. */
265 blocks
= strtoul(optarg
, (char **) NULL
, 0);
266 if (blocks
== 0) pexit("Can't open prototype file");
269 u32_t kb
= div64u(mul64u(blocks
, block_size
), 1024);
271 if (kb
>= 100000) i
= kb
/ 4;
273 /* round up to fill inode block */
274 i
+= inodes_per_block
- 1;
275 i
= i
/ inodes_per_block
* inodes_per_block
;
276 if (i
> INODE_MAX
&& fs_version
< 3) i
= INODE_MAX
;
279 if (blocks
< 5) pexit("Block count too small");
280 if (i
< 1) pexit("Inode count too small");
281 if (i
> INODE_MAX
&& fs_version
< 3) pexit("Inode count too large");
284 /* Make simple file system of the given size, using defaults. */
296 bytes
= 1 + blocks
/8;
297 if(!(umap_array
= malloc(bytes
))) {
298 fprintf(stderr
, "mkfs: can't allocate block bitmap (%d bytes).\n",
302 umap_array_elements
= bytes
;
306 special(argv
[--optind
]);
313 testb
= (short *) alloc_block();
315 /* Try writing the last block of partition or diskette. */
316 if(lseek64(fd
, mul64u(blocks
- 1, block_size
), SEEK_SET
, NULL
) < 0) {
317 pexit("couldn't seek to last block to test size (1)");
321 testb
[block_size
/2-1] = 0x1F2F;
322 if ((w
=write(fd
, (char *) testb
, block_size
)) != block_size
) {
323 if(w
< 0) perror("write");
324 printf("%d/%d\n", w
, block_size
);
325 pexit("File system is too big for minor device (write)");
327 sync(); /* flush write, so if error next read fails */
328 if(lseek64(fd
, mul64u(blocks
- 1, block_size
), SEEK_SET
, NULL
) < 0) {
329 pexit("couldn't seek to last block to test size (2)");
333 nread
= read(fd
, (char *) testb
, block_size
);
334 if (nread
!= block_size
|| testb
[0] != 0x3245 || testb
[1] != 0x11FF ||
335 testb
[block_size
/2-1] != 0x1F2F) {
336 if(nread
< 0) perror("read");
337 printf("nread = %d\n", nread
);
338 printf("testb = 0x%x 0x%x 0x%x\n", testb
[0], testb
[1], testb
[block_size
-1]);
339 pexit("File system is too big for minor device (read)");
341 lseek64(fd
, mul64u(blocks
- 1, block_size
), SEEK_SET
, NULL
);
344 if (write(fd
, (char *) testb
, block_size
) != block_size
)
345 pexit("File system is too big for minor device (write2)");
346 lseek(fd
, 0L, SEEK_SET
);
351 /* Make the file-system */
355 put_block((block_t
) 0, zero
); /* Write a null boot block. */
357 zone_shift
= 0; /* for future use */
358 zones
= nrblocks
>> zone_shift
;
360 super(zones
, inodes
);
362 root_inum
= alloc_inode(mode
, usrid
, grpid
);
364 if (simple
== 0) eat_dir(root_inum
);
366 if (print
) print_fs();
374 /*================================================================
375 * sizeup - determine device size
376 *===============================================================*/
377 block_t
sizeup(device
)
381 struct partition entry
;
387 if ((fd
= open(device
, O_RDONLY
)) == -1) {
389 perror("sizeup open");
392 if (ioctl(fd
, DIOCGETP
, &entry
) == -1) {
393 perror("sizeup ioctl");
394 if(fstat(fd
, &st
) < 0) {
396 entry
.size
= cvu64(0);
398 fprintf(stderr
, "used fstat instead\n");
399 entry
.size
= cvu64(st
.st_size
);
403 d
= div64u(entry
.size
, block_size
);
404 rem
= rem64u(entry
.size
, block_size
);
406 resize
= add64u(mul64u(d
, block_size
), rem
);
407 if(cmp64(resize
, entry
.size
) != 0) {
409 fprintf(stderr
, "mkfs: truncating FS at %lu blocks\n", d
);
416 /*================================================================
417 * super - construct a superblock
418 *===============================================================*/
420 void super(zones
, inodes
)
430 struct super_block
*sup
;
435 for (cp
= buf
; cp
< &buf
[block_size
]; cp
++) *cp
= 0;
436 sup
= (struct super_block
*) buf
; /* lint - might use a union */
438 sup
->s_ninodes
= inodes
;
439 if (fs_version
== 1) {
440 sup
->s_nzones
= zones
;
441 if (sup
->s_nzones
!= zones
) pexit("too many zones");
443 sup
->s_nzones
= 0; /* not used in V2 - 0 forces errors early */
444 sup
->s_zones
= zones
;
447 #define BIGGERBLOCKS "Please try a larger block size for an FS of this size.\n"
448 sup
->s_imap_blocks
= nb
= bitmapsize((bit_t
) (1 + inodes
), block_size
);
449 if(sup
->s_imap_blocks
!= nb
) {
450 fprintf(stderr
, "mkfs: too many inode bitmap blocks.\n" BIGGERBLOCKS
);
453 sup
->s_zmap_blocks
= nb
= bitmapsize((bit_t
) zones
, block_size
);
454 if(nb
!= sup
->s_zmap_blocks
) {
455 fprintf(stderr
, "mkfs: too many block bitmap blocks.\n" BIGGERBLOCKS
);
458 inode_offset
= START_BLOCK
+ sup
->s_imap_blocks
+ sup
->s_zmap_blocks
;
459 inodeblks
= (inodes
+ inodes_per_block
- 1) / inodes_per_block
;
460 initblks
= inode_offset
+ inodeblks
;
461 sup
->s_firstdatazone_old
= nb
=
462 (initblks
+ (1 << zone_shift
) - 1) >> zone_shift
;
463 if(nb
>= zones
) pexit("bit maps too large");
464 if(nb
!= sup
->s_firstdatazone_old
) {
465 /* The field is too small to store the value. Fortunately, the value
466 * can be computed from other fields. We set the on-disk field to zero
467 * to indicate that it must not be used. Eventually, we can always set
468 * the on-disk field to zero, and stop using it.
470 sup
->s_firstdatazone_old
= 0;
472 sup
->s_firstdatazone
= nb
;
473 zoff
= sup
->s_firstdatazone
- 1;
474 sup
->s_log_zone_size
= zone_shift
;
475 if (fs_version
== 1) {
476 sup
->s_magic
= SUPER_MAGIC
; /* identify super blocks */
477 v1sq
= (zone_t
) V1_INDIRECTS
* V1_INDIRECTS
;
478 zo
= V1_NR_DZONES
+ (long) V1_INDIRECTS
+ v1sq
;
479 sup
->s_max_size
= zo
* block_size
;
481 v2sq
= (zone_t
) V2_INDIRECTS(block_size
) * V2_INDIRECTS(block_size
);
482 zo
= V2_NR_DZONES
+ (zone_t
) V2_INDIRECTS(block_size
) + v2sq
;
483 if(fs_version
== 2) {
484 sup
->s_magic
= SUPER_V2
;/* identify super blocks */
485 sup
->s_max_size
= zo
* block_size
;
487 sup
->s_magic
= SUPER_V3
;
488 sup
->s_block_size
= block_size
;
489 sup
->s_disk_version
= 0;
490 #define MAX_MAX_SIZE ((unsigned long) LONG_MAX)
491 if(MAX_MAX_SIZE
/block_size
< zo
) {
492 sup
->s_max_size
= MAX_MAX_SIZE
;
495 sup
->s_max_size
= zo
* block_size
;
500 zone_size
= 1 << zone_shift
; /* nr of blocks per zone */
502 if (lseek(fd
, (off_t
) _STATIC_BLOCK_SIZE
, SEEK_SET
) == (off_t
) -1) {
503 pexit("super() couldn't seek");
505 if (write(fd
, buf
, _STATIC_BLOCK_SIZE
) != _STATIC_BLOCK_SIZE
) {
506 pexit("super() couldn't write");
509 /* Clear maps and inodes. */
510 for (i
= START_BLOCK
; i
< initblks
; i
++) put_block((block_t
) i
, zero
);
512 next_zone
= sup
->s_firstdatazone
;
515 zone_map
= INODE_MAP
+ sup
->s_imap_blocks
;
517 insert_bit(zone_map
, 0); /* bit zero must always be allocated */
518 insert_bit((block_t
) INODE_MAP
, 0); /* inode zero not used but
519 * must be allocated */
525 /*================================================================
526 * rootdir - install the root directory
527 *===============================================================*/
534 add_zone(inode
, z
, 2 * sizeof(struct direct
), current_time
);
535 enter_dir(inode
, ".", inode
);
536 enter_dir(inode
, "..", inode
);
542 /*================================================================
543 * eat_dir - recursively install directory
544 *===============================================================*/
548 /* Read prototype lines and set up directory. Recurse if need be. */
549 char *token
[MAX_TOKENS
], *p
;
551 int mode
, usrid
, grpid
, maj
, min
, f
;
557 getline(line
, token
);
559 if (*p
== '$') return;
562 usrid
= atoi(token
[2]);
563 grpid
= atoi(token
[3]);
564 if (grpid
& 0200) fprintf(stderr
, "A.S.Tanenbaum\n");
565 n
= alloc_inode(mode
, usrid
, grpid
);
567 /* Enter name in directory and update directory's size. */
568 enter_dir(parent
, token
[0], n
);
569 incr_size(parent
, sizeof(struct direct
));
571 /* Check to see if file is directory or special. */
574 /* This is a directory. */
575 z
= alloc_zone(); /* zone for new directory */
576 add_zone(n
, z
, 2 * sizeof(struct direct
), current_time
);
577 enter_dir(n
, ".", n
);
578 enter_dir(n
, "..", parent
);
582 } else if (*p
== 'b' || *p
== 'c') {
584 maj
= atoi(token
[4]);
585 min
= atoi(token
[5]);
587 if (token
[6]) size
= atoi(token
[6]);
588 size
= block_size
* size
;
589 add_zone(n
, (zone_t
) ((maj
<< 8) | min
), size
, current_time
);
591 /* Regular file. Go read it. */
592 if ((f
= open(token
[4], O_RDONLY
)) < 0) {
593 fprintf(stderr
, "%s: Can't open %s: %s\n",
594 progname
, token
[4], strerror(errno
));
603 /*================================================================
604 * eat_file - copy file to MINIX
605 *===============================================================*/
606 /* Zonesize >= blocksize */
607 void eat_file(inode
, f
)
619 for (i
= 0, j
= 0; i
< zone_size
; i
++, j
+= ct
) {
620 for (k
= 0; k
< block_size
; k
++) buf
[k
] = 0;
621 if ((ct
= read(f
, buf
, block_size
)) > 0) {
622 if (i
== 0) z
= alloc_zone();
623 put_block((z
<< zone_shift
) + i
, buf
);
626 timeval
= (dflag
? current_time
: file_time(f
));
627 if (ct
) add_zone(inode
, z
, (long) j
, timeval
);
628 } while (ct
== block_size
);
634 /*================================================================
635 * directory & inode management assist group
636 *===============================================================*/
637 void enter_dir(parent
, name
, child
)
641 /* Enter child in parent directory */
642 /* Works for dir > 1 block and zone > block */
647 struct direct
*dir_entry
;
648 d1_inode ino1
[V1_INODES_PER_BLOCK
];
652 b
= ((parent
- 1) / inodes_per_block
) + inode_offset
;
653 off
= (parent
- 1) % inodes_per_block
;
655 if(!(dir_entry
= malloc(NR_DIR_ENTRIES(block_size
) * sizeof(*dir_entry
))))
656 pexit("couldn't allocate directory entry");
658 if(!(ino2
= malloc(V2_INODES_PER_BLOCK(block_size
) * sizeof(*ino2
))))
659 pexit("couldn't allocate block of inodes entry");
661 if (fs_version
== 1) {
662 get_block(b
, (char *) ino1
);
663 nr_dzones
= V1_NR_DZONES
;
665 get_block(b
, (char *) ino2
);
666 nr_dzones
= V2_NR_DZONES
;
668 for (k
= 0; k
< nr_dzones
; k
++) {
669 if (fs_version
== 1) {
670 z
= ino1
[off
].d1_zone
[k
];
673 ino1
[off
].d1_zone
[k
] = z
;
676 z
= ino2
[off
].d2_zone
[k
];
679 ino2
[off
].d2_zone
[k
] = z
;
682 for (l
= 0; l
< zone_size
; l
++) {
683 get_block((z
<< zone_shift
) + l
, (char *) dir_entry
);
684 for (i
= 0; i
< NR_DIR_ENTRIES(block_size
); i
++) {
685 if (dir_entry
[i
].d_ino
== 0) {
686 dir_entry
[i
].d_ino
= child
;
688 p2
= dir_entry
[i
].d_name
;
689 j
= sizeof(dir_entry
[i
].d_name
);
694 put_block((z
<< zone_shift
) + l
, (char *) dir_entry
);
695 if (fs_version
== 1) {
696 put_block(b
, (char *) ino1
);
698 put_block(b
, (char *) ino2
);
708 printf("Directory-inode %d beyond direct blocks. Could not enter %s\n",
714 void add_zone(n
, z
, bytes
, cur_time
)
717 long bytes
, cur_time
;
719 if (fs_version
== 1) {
720 add_z_1(n
, z
, bytes
, cur_time
);
722 add_z_2(n
, z
, bytes
, cur_time
);
726 void add_z_1(n
, z
, bytes
, cur_time
)
729 long bytes
, cur_time
;
731 /* Add zone z to inode n. The file has grown by 'bytes' bytes. */
736 zone1_t blk
[V1_INDIRECTS
];
738 d1_inode inode
[V1_INODES_PER_BLOCK
];
740 b
= ((n
- 1) / V1_INODES_PER_BLOCK
) + inode_offset
;
741 off
= (n
- 1) % V1_INODES_PER_BLOCK
;
742 get_block(b
, (char *) inode
);
745 p
->d1_mtime
= cur_time
;
746 for (i
= 0; i
< V1_NR_DZONES
; i
++)
747 if (p
->d1_zone
[i
] == 0) {
748 p
->d1_zone
[i
] = (zone1_t
) z
;
749 put_block(b
, (char *) inode
);
752 put_block(b
, (char *) inode
);
754 /* File has grown beyond a small file. */
755 if (p
->d1_zone
[V1_NR_DZONES
] == 0)
756 p
->d1_zone
[V1_NR_DZONES
] = (zone1_t
) alloc_zone();
757 indir
= p
->d1_zone
[V1_NR_DZONES
];
758 put_block(b
, (char *) inode
);
759 b
= indir
<< zone_shift
;
760 get_block(b
, (char *) blk
);
761 for (i
= 0; i
< V1_INDIRECTS
; i
++)
763 blk
[i
] = (zone1_t
) z
;
764 put_block(b
, (char *) blk
);
767 pexit("File has grown beyond single indirect");
770 void add_z_2(n
, z
, bytes
, cur_time
)
773 long bytes
, cur_time
;
775 /* Add zone z to inode n. The file has grown by 'bytes' bytes. */
784 if(!(blk
= malloc(V2_INDIRECTS(block_size
)*sizeof(*blk
))))
785 pexit("Couldn't allocate indirect block");
787 if(!(inode
= malloc(V2_INODES_PER_BLOCK(block_size
)*sizeof(*inode
))))
788 pexit("Couldn't allocate block of inodes");
790 b
= ((n
- 1) / V2_INODES_PER_BLOCK(block_size
)) + inode_offset
;
791 off
= (n
- 1) % V2_INODES_PER_BLOCK(block_size
);
792 get_block(b
, (char *) inode
);
795 p
->d2_mtime
= cur_time
;
796 for (i
= 0; i
< V2_NR_DZONES
; i
++)
797 if (p
->d2_zone
[i
] == 0) {
799 put_block(b
, (char *) inode
);
804 put_block(b
, (char *) inode
);
806 /* File has grown beyond a small file. */
807 if (p
->d2_zone
[V2_NR_DZONES
] == 0) p
->d2_zone
[V2_NR_DZONES
] = alloc_zone();
808 indir
= p
->d2_zone
[V2_NR_DZONES
];
809 put_block(b
, (char *) inode
);
810 b
= indir
<< zone_shift
;
811 get_block(b
, (char *) blk
);
812 for (i
= 0; i
< V2_INDIRECTS(block_size
); i
++)
815 put_block(b
, (char *) blk
);
820 pexit("File has grown beyond single indirect");
827 /* Increment the link count to inode n */
829 static int enter
= 0;
834 b
= ((n
- 1) / inodes_per_block
) + inode_offset
;
835 off
= (n
- 1) % inodes_per_block
;
836 if (fs_version
== 1) {
837 d1_inode inode1
[V1_INODES_PER_BLOCK
];
839 get_block(b
, (char *) inode1
);
840 inode1
[off
].d1_nlinks
++;
841 put_block(b
, (char *) inode1
);
843 static d2_inode
*inode2
= NULL
;
846 n
= sizeof(*inode2
) * V2_INODES_PER_BLOCK(block_size
);
847 if(!inode2
&& !(inode2
= malloc(n
)))
848 pexit("couldn't allocate a block of inodes");
850 get_block(b
, (char *) inode2
);
851 inode2
[off
].d2_nlinks
++;
852 put_block(b
, (char *) inode2
);
858 void incr_size(n
, count
)
862 /* Increment the file-size in inode n */
866 b
= ((n
- 1) / inodes_per_block
) + inode_offset
;
867 off
= (n
- 1) % inodes_per_block
;
868 if (fs_version
== 1) {
869 d1_inode inode1
[V1_INODES_PER_BLOCK
];
871 get_block(b
, (char *) inode1
);
872 inode1
[off
].d1_size
+= count
;
873 put_block(b
, (char *) inode1
);
876 if(!(inode2
= malloc(V2_INODES_PER_BLOCK(block_size
) * sizeof(*inode2
))))
877 pexit("couldn't allocate a block of inodes");
879 get_block(b
, (char *) inode2
);
880 inode2
[off
].d2_size
+= count
;
881 put_block(b
, (char *) inode2
);
887 /*================================================================
888 * allocation assist group
889 *===============================================================*/
890 PRIVATE ino_t
alloc_inode(mode
, usrid
, grpid
)
891 int mode
, usrid
, grpid
;
898 if (num
> nrinodes
) {
899 fprintf(stderr
, "have %d inodoes\n", nrinodes
);
900 pexit("File system does not have enough inodes");
902 b
= ((num
- 1) / inodes_per_block
) + inode_offset
;
903 off
= (num
- 1) % inodes_per_block
;
904 if (fs_version
== 1) {
905 d1_inode inode1
[V1_INODES_PER_BLOCK
];
907 get_block(b
, (char *) inode1
);
908 inode1
[off
].d1_mode
= mode
;
909 inode1
[off
].d1_uid
= usrid
;
910 inode1
[off
].d1_gid
= grpid
;
911 put_block(b
, (char *) inode1
);
915 if(!(inode2
= malloc(V2_INODES_PER_BLOCK(block_size
) * sizeof(*inode2
))))
916 pexit("couldn't allocate a block of inodes");
918 get_block(b
, (char *) inode2
);
919 inode2
[off
].d2_mode
= mode
;
920 inode2
[off
].d2_uid
= usrid
;
921 inode2
[off
].d2_gid
= grpid
;
922 put_block(b
, (char *) inode2
);
927 /* Set the bit in the bit map. */
928 /* DEBUG FIXME. This assumes the bit is in the first inode map block. */
929 insert_bit((block_t
) INODE_MAP
, (int) num
);
934 PRIVATE zone_t
alloc_zone()
936 /* Allocate a new zone */
937 /* Works for zone > block */
944 if ((b
+ zone_size
) > nrblocks
)
945 pexit("File system not big enough for all the files");
946 for (i
= 0; i
< zone_size
; i
++)
947 put_block(b
+ i
, zero
); /* give an empty zone */
948 /* DEBUG FIXME. This assumes the bit is in the first zone map block. */
949 insert_bit(zone_map
, (int) (z
- zoff
)); /* lint, NOT OK because
950 * z hasn't been broken
957 void insert_bit(block
, bit
)
961 /* Insert 'count' bits in the bitmap */
965 buf
= (short *) alloc_block();
967 if (block
< 0) pexit("insert_bit called with negative argument");
968 get_block(block
, (char *) buf
);
969 w
= bit
/ (8 * sizeof(short));
970 s
= bit
% (8 * sizeof(short));
972 put_block(block
, (char *) buf
);
978 /*================================================================
979 * proto-file processing assist group
980 *===============================================================*/
984 /* Convert string to mode */
985 int o1
, o2
, o3
, mode
;
994 mode
= (o1
<< 6) | (o2
<< 3) | o3
;
995 if (c1
== 'd') mode
+= I_DIRECTORY
;
996 if (c1
== 'b') mode
+= I_BLOCK_SPECIAL
;
997 if (c1
== 'c') mode
+= I_CHAR_SPECIAL
;
998 if (c1
== '-') mode
+= I_REGULAR
;
999 if (c2
== 'u') mode
+= I_SET_UID_BIT
;
1000 if (c3
== 'g') mode
+= I_SET_GID_BIT
;
1004 void getline(line
, parse
)
1005 char *parse
[MAX_TOKENS
];
1006 char line
[LINE_LEN
];
1008 /* Read a line and break it up in tokens */
1013 for (k
= 0; k
< MAX_TOKENS
; k
++) parse
[k
] = 0;
1014 for (k
= 0; k
< LINE_LEN
; k
++) line
[k
] = 0;
1019 if (++k
> LINE_LEN
) pexit("Line too long");
1021 if (d
== EOF
) pexit("Unexpected end-of-file");
1023 if (*p
== '\n') lct
++;
1024 if (*p
== ' ' || *p
== '\t') *p
= 0;
1038 if (c
== '\n') return;
1039 if (c
== 0) continue;
1043 } while (c
!= 0 && c
!= '\n');
1048 /*================================================================
1050 *===============================================================*/
1051 void check_mtab(devname
)
1052 char *devname
; /* /dev/hd1 or whatever */
1054 /* Check to see if the special file named in s is mounted. */
1058 char special
[PATH_MAX
+ 1], mounted_on
[PATH_MAX
+ 1], version
[10], rw_flag
[10];
1060 r
= stat(devname
, &sb
);
1063 if (errno
== ENOENT
)
1064 return; /* Does not exist, and therefore not mounted. */
1065 fprintf(stderr
, "%s: stat %s failed: %s\n",
1066 progname
, devname
, strerror(errno
));
1069 if (!S_ISBLK(sb
.st_mode
))
1071 /* Not a block device and therefore not mounted. */
1075 if (load_mtab("mkfs") < 0) return;
1077 n
= get_mtab_entry(special
, mounted_on
, version
, rw_flag
);
1079 if (strcmp(devname
, special
) == 0) {
1080 /* Can't mkfs on top of a mounted file system. */
1081 fprintf(stderr
, "%s: %s is mounted on %s\n",
1082 progname
, devname
, mounted_on
);
1093 struct stat statbuf
;
1095 return(statbuf
.st_mtime
);
1096 #else /* fstat not supported by DOS */
1105 fprintf(stderr
, "%s: %s\n", progname
, s
);
1107 fprintf(stderr
, "Line %d being processed when error detected.\n", lct
);
1113 void copy(from
, to
, count
)
1117 while (count
--) *to
++ = *from
++;
1124 if(!(buf
= malloc(block_size
))) {
1125 pexit("couldn't allocate filesystem buffer");
1127 bzero(buf
, block_size
);
1136 d1_inode inode1
[V1_INODES_PER_BLOCK
];
1138 unsigned short *usbuf
;
1139 block_t b
, inode_limit
;
1142 if(!(inode2
= malloc(V2_INODES_PER_BLOCK(block_size
) * sizeof(*inode2
))))
1143 pexit("couldn't allocate a block of inodes");
1145 if(!(dir
= malloc(NR_DIR_ENTRIES(block_size
)*sizeof(*dir
))))
1146 pexit("malloc of directory entry failed");
1148 usbuf
= (unsigned short *) alloc_block();
1150 get_super_block((char *) usbuf
);
1151 printf("\nSuperblock: ");
1152 for (i
= 0; i
< 8; i
++) printf("%06o ", usbuf
[i
]);
1153 get_block((block_t
) 2, (char *) usbuf
);
1154 printf("...\nInode map: ");
1155 for (i
= 0; i
< 9; i
++) printf("%06o ", usbuf
[i
]);
1156 get_block((block_t
) 3, (char *) usbuf
);
1157 printf("...\nZone map: ");
1158 for (i
= 0; i
< 9; i
++) printf("%06o ", usbuf
[i
]);
1165 for (b
= inode_offset
; k
< nrinodes
; b
++) {
1166 if (fs_version
== 1) {
1167 get_block(b
, (char *) inode1
);
1169 get_block(b
, (char *) inode2
);
1171 for (i
= 0; i
< inodes_per_block
; i
++) {
1172 k
= inodes_per_block
* (int) (b
- inode_offset
) + i
+ 1;
1174 if (k
> nrinodes
) break;
1175 if (fs_version
== 1) {
1176 if (inode1
[i
].d1_mode
!= 0) {
1177 printf("Inode %2d: mode=", k
);
1178 printf("%06o", inode1
[i
].d1_mode
);
1179 printf(" uid=%2d gid=%2d size=",
1180 inode1
[i
].d1_uid
, inode1
[i
].d1_gid
);
1181 printf("%6ld", inode1
[i
].d1_size
);
1182 printf(" zone[0]=%d\n", inode1
[i
].d1_zone
[0]);
1184 if ((inode1
[i
].d1_mode
& I_TYPE
) == I_DIRECTORY
) {
1185 /* This is a directory */
1186 get_block(inode1
[i
].d1_zone
[0], (char *) dir
);
1187 for (j
= 0; j
< NR_DIR_ENTRIES(block_size
); j
++)
1189 printf("\tInode %2d: %s\n", dir
[j
].d_ino
, dir
[j
].d_name
);
1192 if (inode2
[i
].d2_mode
!= 0) {
1193 printf("Inode %2d: mode=", k
);
1194 printf("%06o", inode2
[i
].d2_mode
);
1195 printf(" uid=%2d gid=%2d size=",
1196 inode2
[i
].d2_uid
, inode2
[i
].d2_gid
);
1197 printf("%6ld", inode2
[i
].d2_size
);
1198 printf(" zone[0]=%ld\n", inode2
[i
].d2_zone
[0]);
1200 if ((inode2
[i
].d2_mode
& I_TYPE
) == I_DIRECTORY
) {
1201 /* This is a directory */
1202 get_block(inode2
[i
].d2_zone
[0], (char *) dir
);
1203 for (j
= 0; j
< NR_DIR_ENTRIES(block_size
); j
++)
1205 printf("\tInode %2d: %s\n", dir
[j
].d_ino
, dir
[j
].d_name
);
1211 printf("%d inodes used. %d zones used.\n", next_inode
- 1, next_zone
);
1220 /* The first time a block is read, it returns all 0s, unless there has
1221 * been a write. This routine checks to see if a block has been accessed.
1227 if(w
>= umap_array_elements
) {
1228 pexit("umap array too small - this can't happen");
1232 r
= (umap_array
[w
] & mask
? 1 : 0);
1233 umap_array
[w
] |= mask
;
1240 "Usage: %s [-12dlot] [-b blocks] [-i inodes] [-B blocksize] special [proto]\n",
1245 /*================================================================
1246 * get_block & put_block for MS-DOS
1247 *===============================================================*/
1251 * These are the get_block and put_block routines
1252 * when compiling & running mkfs.c under MS-DOS.
1254 * It requires the (asembler) routines absread & abswrite
1255 * from the file diskio.asm. Since these routines just do
1256 * as they are told (read & write the sector specified),
1257 * a local cache is used to minimize the i/o-overhead for
1258 * frequently used blocks.
1260 * The global variable "file" determines whether the output
1261 * is to a disk-device or to a binary file.
1265 #define PH_SECTSIZE 512 /* size of a physical disk-sector */
1268 char *derrtab
[14] = {
1270 "disk is read-only",
1275 "internal error: bad request structure length",
1277 "unknown media type",
1279 "printer out of paper (?)",
1285 #define CACHE_SIZE 20 /* 20 block-buffers */
1289 char blockbuf
[BLOCK_SIZE
];
1293 } cache
[CACHE_SIZE
];
1296 void special(string
)
1300 if (string
[1] == ':' && string
[2] == 0) {
1301 /* Format: d: or d:fname */
1302 disk
= (string
[0] & ~32) - 'A';
1303 if (disk
> 1 && !override
) /* safety precaution */
1304 pexit("Bad drive specifier for special");
1307 if ((fd
= creat(string
, BWRITE
)) == 0)
1308 pexit("Can't open special file");
1312 void get_block(n
, buf
)
1316 /* Get a block to the user */
1317 struct cache
*bp
, *fp
;
1319 /* First access returns a zero block */
1320 if (read_and_set(n
) == 0) {
1321 copy(zero
, buf
, block_size
);
1325 /* Look for block in cache */
1327 for (bp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++) {
1328 if (bp
->blocknum
== n
) {
1329 copy(bp
, buf
, block_size
);
1334 /* Remember clean block */
1337 if (fp
->usecnt
> bp
->usecnt
) fp
= bp
;
1342 /* Block not in cache, get it */
1344 /* No clean buf, flush one */
1345 for (bp
= cache
, fp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++)
1346 if (fp
->usecnt
> bp
->usecnt
) fp
= bp
;
1347 mx_write(fp
->blocknum
, fp
);
1353 copy(fp
, buf
, block_size
);
1356 void put_block(n
, buf
)
1360 /* Accept block from user */
1361 struct cache
*fp
, *bp
;
1363 (void) read_and_set(n
);
1365 /* Look for block in cache */
1367 for (bp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++) {
1368 if (bp
->blocknum
== n
) {
1369 copy(buf
, bp
, block_size
);
1374 /* Remember clean block */
1377 if (fp
->usecnt
> bp
->usecnt
) fp
= bp
;
1382 /* Block not in cache */
1384 /* No clean buf, flush one */
1385 for (bp
= cache
, fp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++)
1386 if (fp
->usecnt
> bp
->usecnt
) fp
= bp
;
1387 mx_write(fp
->blocknum
, fp
);
1392 copy(buf
, fp
, block_size
);
1398 for (bp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++) bp
->blocknum
= -1;
1403 /* Flush all dirty blocks to disk */
1406 for (bp
= cache
; bp
< &cache
[CACHE_SIZE
]; bp
++)
1408 mx_write(bp
->blocknum
, bp
);
1413 /*==================================================================
1414 * hard read & write etc.
1415 *=================================================================*/
1416 #define MAX_RETRIES 5
1419 void mx_read(blocknr
, buf
)
1424 /* Read the requested MINIX-block in core */
1425 char (*bp
)[PH_SECTSIZE
];
1426 int sectnum
, retries
, err
;
1429 lseek(fd
, (off_t
) blocknr
* block_size
, 0);
1430 if (read(fd
, buf
, block_size
) != block_size
)
1431 pexit("mx_read: error reading file");
1433 sectnum
= blocknr
* (block_size
/ PH_SECTSIZE
);
1434 for (bp
= buf
; bp
< &buf
[block_size
]; bp
++) {
1435 retries
= MAX_RETRIES
;
1437 err
= absread(disk
, sectnum
, bp
);
1438 while (err
&& --retries
);
1443 dexit("mx_read", sectnum
, err
);
1449 void mx_write(blocknr
, buf
)
1453 /* Write the MINIX-block to disk */
1454 char (*bp
)[PH_SECTSIZE
];
1455 int retries
, sectnum
, err
;
1458 lseek(fd
, blocknr
* block_size
, 0);
1459 if (write(fd
, buf
, block_size
) != block_size
) {
1460 pexit("mx_write: error writing file");
1463 sectnum
= blocknr
* (block_size
/ PH_SECTSIZE
);
1464 for (bp
= buf
; bp
< &buf
[block_size
]; bp
++) {
1465 retries
= MAX_RETRIES
;
1467 err
= abswrite(disk
, sectnum
, bp
);
1468 } while (err
&& --retries
);
1473 dexit("mx_write", sectnum
, err
);
1480 void dexit(s
, sectnum
, err
)
1484 printf("Error: %s, sector: %d, code: %d, meaning: %s\n",
1485 s
, sectnum
, err
, derrtab
[err
]);
1491 /*================================================================
1492 * get_block & put_block for UNIX
1493 *===============================================================*/
1496 void special(string
)
1499 fd
= creat(string
, 0777);
1501 fd
= open(string
, O_RDWR
);
1502 if (fd
< 0) pexit("Can't open special file");
1507 void get_block(n
, buf
)
1515 /* First access returns a zero block */
1516 if (read_and_set(n
) == 0) {
1517 copy(zero
, buf
, block_size
);
1520 lseek64(fd
, mul64u(n
, block_size
), SEEK_SET
, NULL
);
1521 k
= read(fd
, buf
, block_size
);
1522 if (k
!= block_size
) {
1523 pexit("get_block couldn't read");
1527 void get_super_block(buf
)
1534 if(lseek(fd
, (off_t
) SUPER_BLOCK_BYTES
, SEEK_SET
) < 0) {
1536 pexit("seek failed");
1538 k
= read(fd
, buf
, _STATIC_BLOCK_SIZE
);
1539 if (k
!= _STATIC_BLOCK_SIZE
) {
1540 pexit("get_super_block couldn't read");
1544 void put_block(n
, buf
)
1548 /* Write a block. */
1550 (void) read_and_set(n
);
1552 /* XXX - check other lseeks too. */
1553 if (lseek64(fd
, mul64u(n
, block_size
), SEEK_SET
, NULL
) == (off_t
) -1) {
1554 pexit("put_block couldn't seek");
1556 if (write(fd
, buf
, block_size
) != block_size
) {
1557 pexit("put_block couldn't write");
1562 /* Dummy routines to keep source file clean from #ifdefs */