mkfs.mfs, mkproto: minor features
[minix3.git] / usr.sbin / mkfs.mfs / mkfs.c
blob8fd95c3a58311b9c9958124be0eb4363348c50bd
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"
7 #endif
9 #include <sys/cdefs.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
13 #if defined(__minix)
14 #include <minix/minlib.h>
15 #include <minix/partition.h>
16 #include <minix/u64.h>
17 #include <sys/ioctl.h>
18 #elif defined(__linux__)
19 #include <mntent.h>
20 #endif
22 #include <assert.h>
23 #include <err.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
35 /* Definition of the file system layout: */
36 #include "const.h"
37 #include "type.h"
38 #include "mfsdir.h"
39 #include "super.h"
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)
47 #define MAX_TOKENS 10
48 #define LINE_LEN 200
49 /* XXX why do we not use 0 / SU_ID ? */
50 #define BIN 2
51 #define BINGRP 2
53 #if !defined(__minix)
54 #define mul64u(a,b) ((uint64_t)(a) * (b))
55 #endif
57 /* some Minix specific types that do not conflict with Posix */
58 #ifndef block_t
59 typedef uint32_t block_t; /* block number */
60 #endif
61 #ifndef zone_t
62 typedef uint32_t zone_t; /* zone number */
63 #endif
64 #ifndef bit_t
65 typedef uint32_t bit_t; /* bit number in a bit map */
66 #endif
67 #ifndef bitchunk_t
68 typedef uint32_t bitchunk_t; /* collection of bits in a bitmap */
69 #endif
71 struct fs_size {
72 ino_t inocount; /* amount of inodes */
73 zone_t zonecount; /* amount of zones */
74 block_t blockcount; /* amount of blocks */
77 extern char *optarg;
78 extern int optind;
80 block_t nrblocks;
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 */
89 char *progname;
90 uint64_t fs_offset_bytes, fs_offset_blocks, written_fs_size = 0;
92 time_t current_time;
93 char *zero;
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
98 size_t block_size;
99 #else
100 #define block_size MFS_STATIC_BLOCK_SIZE
101 #endif
103 FILE *proto;
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);
129 void print_fs(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;
150 int insertmode = 0;
152 progname = argv[0];
154 /* Process switches. */
155 blocks = 0;
156 inodes = 0;
157 bblocks = 0;
158 #ifndef MFS_STATIC_BLOCK_SIZE
159 block_size = 0;
160 #endif
161 zone_shift = 0;
162 extra_space_percent = 0;
163 while ((ch = getopt(argc, argv, "B:b:di:ltvx:z:I:T:")) != EOF)
164 switch (ch) {
165 #ifndef MFS_STATIC_BLOCK_SIZE
166 case 'B':
167 block_size = strtoul(optarg, &sfx, 0);
168 switch(*sfx) {
169 case 'b': case 'B': /* bytes; NetBSD-compatible */
170 case '\0': break;
171 case 'K':
172 case 'k': block_size*=1024; break;
173 case 's': block_size*=SECTOR_SIZE; break;
174 default: usage();
176 break;
177 #else
178 case 'B':
179 if (block_size != strtoul(optarg, (char **) NULL, 0))
180 errx(4, "block size must be exactly %d bytes",
181 MFS_STATIC_BLOCK_SIZE);
182 break;
183 (void)sfx; /* shut up warnings about unused variable...*/
184 #endif
185 case 'I':
186 fs_offset_bytes = strtoul(optarg, (char **) NULL, 0);
187 insertmode = 1;
188 break;
189 case 'b':
190 blocks = bblocks = strtoul(optarg, (char **) NULL, 0);
191 break;
192 case 'T':
193 Tflag = 1;
194 current_time = strtoul(optarg, (char **) NULL, 0);
195 break;
196 case 'd':
197 dflag = 1;
198 break;
199 case 'i':
200 inodes = strtoul(optarg, (char **) NULL, 0);
201 break;
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;
207 default: usage();
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
217 * want, then go.
219 if(Tflag) {
220 if(dflag)
221 errx(1, "-T and -d both specify a time and so are mutually exclusive");
222 } else if(dflag) {
223 struct stat statbuf;
224 if (stat(progname, &statbuf)) {
225 err(1, "stat of itself");
227 current_time = statbuf.st_mtime;
228 } else {
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;
239 #endif
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);
245 #endif
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);
249 #endif
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 */
264 if (zone_size <= 0)
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
268 * possible.
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) {
281 blocks = maxblocks;
282 /* blocks == 0 is checked later, but leads to a funny way of
283 * reporting a 0-sized device (displays usage).
285 if(blocks < 1) {
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]);
299 /* Check special. */
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. */
306 lct = 1;
307 get_line(line, token); /* skip boot block info */
309 /* Read the line with the block and inode counts. */
310 get_line(line, token);
311 if (bblocks == 0){
312 blocks = strtol(token[0], (char **) NULL, 10);
313 } else {
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);
339 } else {
340 lct = 0;
341 if (optind < argc) {
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. */
348 mode = 040777;
349 usrid = BIN;
350 grpid = BINGRP;
351 simple = 1;
354 if (inodes == 0) {
355 long long kb = ((unsigned long long)blocks*block_size) / 1024;
357 inodes = kb / 2;
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");
374 nrblocks = blocks;
375 nrinodes = inodes;
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);
382 /* Open special. */
383 special(argv[--optind], insertmode);
385 if (!donttest) {
386 uint16_t *testb;
387 ssize_t w;
389 testb = alloc_block();
391 /* Try writing the last block of partition or diskette. */
392 mkfs_seek(mul64u(blocks - 1, block_size), SEEK_SET);
393 testb[0] = 0x3245;
394 testb[1] = 0x11FF;
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);
399 testb[0] = 0;
400 testb[1] = 0;
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);
411 testb[0] = 0;
412 testb[1] = 0;
413 testb[block_size/2-1] = 0;
414 mkfs_write(testb, block_size);
415 mkfs_seek(0L, SEEK_SET);
416 free(testb);
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);
427 rootdir(root_inum);
428 if (simple == 0) eat_dir(root_inum);
430 if (print) print_fs();
431 else if (verbose > 1) {
432 if (zone_shift)
433 fprintf(stderr, "%d inodes used. %u zones (%u blocks) used.\n",
434 (int)next_inode-1, next_zone, next_zone*zone_per_block);
435 else
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);
442 return(0);
444 /* NOTREACHED */
445 } /* end main */
447 /*================================================================
448 * detect_fs_size - determine image size dynamically
449 *===============================================================*/
450 void
451 detect_fs_size(struct fs_size * fssize)
453 int prev_lct = lct;
454 off_t point = ftell(proto);
455 block_t initb;
456 zone_t initz;
458 fssize->inocount = 1; /* root directory node */
459 fssize->zonecount = 0;
460 fssize->blockcount = 0;
462 sizeup_dir(fssize);
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;
471 lct = prev_lct;
472 fseek(proto, point, SEEK_SET);
475 void
476 sizeup_dir(struct fs_size * fssize)
478 char *token[MAX_TOKENS], *p;
479 char line[LINE_LEN];
480 FILE *f;
481 off_t size;
482 int dir_entries = 2;
483 zone_t dir_zones = 0, fzones, indirects;
485 while (1) {
486 get_line(line, token);
487 p = token[0];
488 if (*p == '$') {
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))
491 dir_zones++;
492 if(dir_zones > NR_DZONES)
493 dir_zones++; /* Max single indir */
494 fssize->zonecount += dir_zones;
495 return;
498 p = token[1];
499 fssize->inocount++;
500 dir_entries++;
502 if (*p == 'd') {
503 sizeup_dir(fssize);
504 } else if (*p == 'b' || *p == 'c') {
506 } else if (*p == 's') {
507 fssize->zonecount++; /* Symlink contents is always stored a block */
508 } else {
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",
513 token[4]);
514 } else if ( (size = ftell(f)) == (off_t)(-1)) {
515 pexit("dynamic size detection failed: can't tell size of %s",
516 token[4]);
517 } else {
518 fclose(f);
519 fzones = roundup(size, zone_size) / zone_size;
520 indirects = 0;
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 *===============================================================*/
541 block_t
542 sizeup(char * device)
544 block_t d;
545 #if defined(__minix)
546 u64_t bytes, resize;
547 u32_t rem;
548 #else
549 off_t size;
550 #endif
553 if ((fd = open(device, O_RDONLY)) == -1) {
554 if (errno != ENOENT)
555 perror("sizeup open");
556 return 0;
559 #if defined(__minix)
560 if(minix_sizeup(device, &bytes) < 0) {
561 perror("sizeup");
562 return 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 */
571 d = (block_t)(-1ul);
572 fprintf(stderr, "%s: truncating FS at %lu blocks\n",
573 progname, (unsigned long)d);
575 #else
576 size = mkfs_seek(0, SEEK_END);
577 /* Assume block_t is unsigned */
578 if (size / block_size > (block_t)(-1ul)) {
579 d = (block_t)(-1ul);
580 fprintf(stderr, "%s: truncating FS at %lu blocks\n",
581 progname, (unsigned long)d);
582 } else
583 d = size / block_size;
584 #endif
586 return d;
590 * copied from fslib
592 static int
593 bitmapsize(bit_t nr_bits, size_t blk_size)
595 block_t nr_blocks;
597 nr_blocks = nr_bits / FS_BITS_PER_BLOCK(blk_size);
598 if (nr_blocks * FS_BITS_PER_BLOCK(blk_size) < nr_bits)
599 ++nr_blocks;
600 return(nr_blocks);
603 /*================================================================
604 * super - construct a superblock
605 *===============================================================*/
607 void
608 super(zone_t zones, ino_t inodes)
610 block_t inodeblks, initblks, i;
611 unsigned long nb;
612 long long ind_per_zone, zo;
613 void *buf;
614 struct super_block *sup;
616 sup = buf = alloc_block();
618 #ifdef MFSFLAG_CLEAN
619 /* The assumption is that mkfs will create a clean FS. */
620 sup->s_flags = MFSFLAG_CLEAN;
621 #endif
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."
635 #else
636 #define BIGGERBLOCKS "Please use MinixFS V3 for an FS of this size."
637 #endif
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
640 * int16_t */
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;
673 #endif
675 ind_per_zone = (long long) indir_per_zone;
676 zo = NR_DZONES + ind_per_zone + ind_per_zone*ind_per_zone;
677 #ifndef MAX_MAX_SIZE
678 #define MAX_MAX_SIZE (INT32_MAX)
679 #endif
680 if(MAX_MAX_SIZE/block_size < zo) {
681 sup->s_max_size = MAX_MAX_SIZE;
683 else {
684 sup->s_max_size = zo * block_size;
687 if (verbose>1) {
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);
702 #endif
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;
712 next_inode = 1;
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 */
720 free(buf);
724 /*================================================================
725 * rootdir - install the root directory
726 *===============================================================*/
727 void
728 rootdir(ino_t inode)
730 zone_t z;
732 z = alloc_zone();
733 add_zone(inode, z, 2 * sizeof(struct direct), current_time);
734 enter_dir(inode, ".", inode);
735 enter_dir(inode, "..", inode);
736 incr_link(inode);
737 incr_link(inode);
740 void
741 enter_symlink(ino_t inode, char *lnk)
743 zone_t z;
744 size_t len;
745 char *buf;
747 buf = alloc_block();
748 z = alloc_zone();
749 len = strlen(lnk);
750 if (len >= block_size)
751 pexit("symlink too long, max length is %u", (unsigned)block_size - 1);
752 strcpy(buf, lnk);
753 put_block((z << zone_shift), buf);
755 add_zone(inode, z, len, current_time);
757 free(buf);
761 /*================================================================
762 * eat_dir - recursively install directory
763 *===============================================================*/
764 void
765 eat_dir(ino_t parent)
767 /* Read prototype lines and set up directory. Recurse if need be. */
768 char *token[MAX_TOKENS], *p;
769 char line[LINE_LEN];
770 int mode, usrid, grpid, maj, min, f;
771 ino_t n;
772 zone_t z;
773 size_t size;
775 while (1) {
776 get_line(line, token);
777 p = token[0];
778 if (*p == '$') return;
779 p = token[1];
780 mode = mode_con(p);
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. */
790 incr_link(n);
791 if (*p == 'd') {
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);
797 incr_link(parent);
798 incr_link(n);
799 eat_dir(n);
800 } else if (*p == 'b' || *p == 'c') {
801 /* Special file. */
802 maj = atoi(token[4]);
803 min = atoi(token[5]);
804 size = 0;
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]);
810 } else {
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));
815 } else {
816 eat_file(n, f);
823 /*================================================================
824 * eat_file - copy file to MINIX
825 *===============================================================*/
826 /* Zonesize >= blocksize */
827 void
828 eat_file(ino_t inode, int f)
830 int ct = 0, i, j;
831 zone_t z = 0;
832 char *buf;
833 time_t timeval;
835 buf = alloc_block();
837 do {
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);
848 close(f);
849 free(buf);
853 dir_try_enter(zone_t z, ino_t child, char const *name)
855 struct direct *dir_entry = alloc_block();
856 int r = 0;
857 block_t b;
858 int i, l;
860 b = z << zone_shift;
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)
866 break;
868 if(i < NR_DIR_ENTRIES(block_size)) {
869 r = 1;
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);
876 break;
880 free(dir_entry);
882 return r;
885 /*================================================================
886 * directory & inode management assist group
887 *===============================================================*/
888 void
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 */
893 unsigned int k;
894 block_t b, indir;
895 zone_t z;
896 int off;
897 struct inode *ino;
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++) {
910 z = ino->i_zone[k];
911 if (z == 0) {
912 z = alloc_zone();
913 ino->i_zone[k] = z;
916 if(dir_try_enter(z, child, __UNCONST(name))) {
917 put_block(b, inoblock);
918 free(inoblock);
919 free(indirblock);
920 return;
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];
934 if(!z) {
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);
940 free(inoblock);
941 free(indirblock);
942 return;
946 pexit("Directory-inode %u beyond single indirect blocks. Could not enter %s",
947 (unsigned)parent, name);
951 void
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. */
956 int off, i, j;
957 block_t b;
958 zone_t indir, dindir;
959 struct inode *p, *inode;
960 zone_t *blk, *dblk;
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;
968 get_block(b, inode);
969 p = &inode[off];
970 p->i_size += bytes;
971 p->i_mtime = mtime;
972 #ifndef MFS_INODE_ONLY_MTIME /* V1 file systems did not have them... */
973 p->i_atime = p->i_ctime = current_time;
974 #endif
975 for (i = 0; i < NR_DZONES; i++)
976 if (p->i_zone[i] == 0) {
977 p->i_zone[i] = z;
978 put_block(b, inode);
979 free(inode);
980 return;
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;
991 put_block(b, inode);
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) {
997 blk[i] = z;
998 put_block(indir, blk);
999 free(blk);
1000 free(inode);
1001 return;
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) {
1026 blk[i] = z;
1027 put_block(dindir, dblk);
1028 put_block(indir, blk);
1029 free(dblk);
1030 free(blk);
1031 free(inode);
1032 return;
1037 pexit("File has grown beyond double indirect");
1041 /* Increment the link count to inode n */
1042 void
1043 incr_link(ino_t n)
1045 int off;
1046 static int enter = 0;
1047 static struct inode *inodes = NULL;
1048 block_t b;
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);
1066 enter = 0;
1070 /* Increment the file-size in inode n */
1071 void
1072 incr_size(ino_t n, size_t count)
1074 block_t b;
1075 int off;
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);
1093 free(inodes);
1098 /*================================================================
1099 * allocation assist group
1100 *===============================================================*/
1101 static ino_t
1102 alloc_inode(int mode, int usrid, int grpid)
1104 ino_t num;
1105 int off;
1106 block_t b;
1107 struct inode *inodes;
1109 num = next_inode++;
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",
1123 num);
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);
1132 free(inodes);
1134 /* Set the bit in the bit map. */
1135 insert_bit((block_t) INODE_MAP, num);
1136 return(num);
1140 /* Allocate a new zone */
1141 static zone_t
1142 alloc_zone(void)
1144 /* Works for zone > block */
1145 block_t b;
1146 int i;
1147 zone_t z;
1149 z = next_zone++;
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);
1157 return z;
1161 /* Insert one bit into the bitmap */
1162 void
1163 insert_bit(block_t map, bit_t bit)
1165 int boff, w, s;
1166 unsigned int bits_per_block;
1167 block_t map_block;
1168 bitchunk_t *buf;
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;
1178 assert(boff >=0);
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;
1183 buf[w] |= (1 << s);
1184 put_block(map_block, buf);
1186 free(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;
1197 char c1, c2, c3;
1199 c1 = *p++;
1200 c2 = *p++;
1201 c3 = *p++;
1202 o1 = *p++ - '0';
1203 o2 = *p++ - '0';
1204 o3 = *p++ - '0';
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 */
1216 return(mode);
1219 void
1220 get_line(char line[LINE_LEN], char *parse[MAX_TOKENS])
1222 /* Read a line and break it up in tokens */
1223 int k;
1224 char c, *p;
1225 int d;
1227 for (k = 0; k < MAX_TOKENS; k++) parse[k] = 0;
1228 memset(line, 0, LINE_LEN);
1229 k = 0;
1230 p = line;
1231 while (1) {
1232 if (++k > LINE_LEN) pexit("Line too long");
1233 d = fgetc(proto);
1234 if (d == EOF) pexit("Unexpected end-of-file");
1235 *p = d;
1236 if (*p == ' ' || *p == '\t') *p = 0;
1237 if (*p == '\n') {
1238 lct++;
1239 *p++ = 0;
1240 *p = '\n';
1241 break;
1243 p++;
1246 k = 0;
1247 p = line;
1248 while (1) {
1249 c = *p++;
1250 if (c == '\n') return;
1251 if (c == 0) continue;
1252 parse[k++] = p - 1;
1253 do {
1254 c = *p++;
1255 } while (c != 0 && c != '\n');
1260 /*================================================================
1261 * other stuff
1262 *===============================================================*/
1265 * Check to see if the special file named 'device' is mounted.
1267 void
1268 check_mtab(const char *device) /* /dev/hd1 or whatever */
1270 #if defined(__minix)
1271 int n, r;
1272 struct stat sb;
1273 char dev[PATH_MAX], mount_point[PATH_MAX],
1274 type[MNTNAMELEN], flags[MNTFLAGLEN];
1276 r= stat(device, &sb);
1277 if (r == -1)
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. */
1286 return;
1289 if (load_mtab(__UNCONST("mkfs")) < 0) return;
1290 while (1) {
1291 n = get_mtab_entry(dev, mount_point, type, flags);
1292 if (n < 0) return;
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.
1301 struct mntent *mnt;
1302 struct stat st_buf;
1303 dev_t file_dev=0, file_rdev=0;
1304 ino_t file_ino=0;
1305 FILE *f;
1306 int fd;
1307 char *mtab_file = "/proc/mounts";
1309 if ((f = setmntent (mtab_file, "r")) == NULL)
1310 goto error;
1312 if (stat(device, &st_buf) == 0) {
1313 if (S_ISBLK(st_buf.st_mode)) {
1314 file_rdev = st_buf.st_rdev;
1315 } else {
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)
1323 break;
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))
1327 break;
1328 } else {
1329 if (file_dev && ((file_dev == st_buf.st_dev) &&
1330 (file_ino == st_buf.st_ino)))
1331 break;
1336 if (mnt == NULL) {
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) {
1346 goto is_root;
1349 goto test_busy;
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) {
1359 goto test_busy;
1361 goto error;
1363 if (file_rdev && (st_buf.st_dev != file_rdev)) {
1364 goto error;
1367 fprintf(stderr, "Device %s is mounted, exiting\n", device);
1368 exit(-1);
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, "/")) {
1377 is_root:
1378 fprintf(stderr, "Device %s is mounted as root file system!\n",
1379 device);
1380 exit(-1);
1383 test_busy:
1385 endmntent (f);
1386 if ((stat(device, &st_buf) != 0) ||
1387 !S_ISBLK(st_buf.st_mode))
1388 return;
1389 fd = open(device, O_RDONLY | O_EXCL);
1390 if (fd < 0) {
1391 if (errno == EBUSY) {
1392 fprintf(stderr, "Device %s is used by the system\n", device);
1393 exit(-1);
1395 } else
1396 close(fd);
1398 return;
1400 error:
1401 endmntent (f);
1402 fprintf(stderr, "Error while checking if device %s is mounted\n", device);
1403 exit(-1);
1404 #else
1405 (void) device; /* shut up warnings about unused variable... */
1406 #endif
1410 time_t
1411 file_time(int f)
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);
1423 __dead void
1424 pexit(char const * s, ...)
1426 va_list va;
1428 va_start(va, s);
1429 vwarn(s, va);
1430 va_end(va);
1431 if (lct != 0)
1432 warnx("Line %d being processed when error detected.\n", lct);
1433 exit(2);
1437 void *
1438 alloc_block(void)
1440 void *buf;
1442 if(!(buf = malloc(block_size))) {
1443 err(1, "couldn't allocate filesystem buffer");
1445 memset(buf, 0, block_size);
1447 return buf;
1450 void
1451 print_fs(void)
1453 int i, j;
1454 ino_t k;
1455 struct inode *inode2;
1456 unsigned short *usbuf;
1457 block_t b;
1458 struct direct *dir;
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]);
1472 printf("\n ");
1473 for (i = 0; i < 8; i++) printf("%#04hX ", usbuf[i]);
1474 printf("\n ");
1475 for (i = 8; i < 15; i++) printf("%06ho ", usbuf[i]);
1476 printf("\n ");
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]);
1484 printf("...\n");
1486 free(usbuf);
1487 usbuf = NULL;
1489 k = 0;
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;
1494 /* Lint but OK */
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++)
1509 if (dir[j].d_ino)
1510 printf("\tInode %2u: %s\n",
1511 (unsigned)dir[j].d_ino,
1512 dir[j].d_name);
1518 if (zone_shift)
1519 printf("%d inodes used. %u zones (%u blocks) used.\n",
1520 (int)next_inode-1, next_zone, next_zone*zone_per_block);
1521 else
1522 printf("%d inodes used. %u zones used.\n", (int)next_inode-1, next_zone);
1523 free(dir);
1524 free(inode2);
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)
1535 int w, s, mask, r;
1537 w = n / 8;
1539 assert(n < nrblocks);
1540 if(w >= umap_array_elements) {
1541 errx(1, "umap array too small - this can't happen");
1543 s = n % 8;
1544 mask = 1 << s;
1545 r = (umap_array[w] & mask ? 1 : 0);
1546 umap_array[w] |= mask;
1547 return(r);
1550 __dead void
1551 usage(void)
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",
1555 progname);
1556 exit(4);
1559 void
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);
1571 /* Read a block. */
1572 void
1573 get_block(block_t n, void *buf)
1575 ssize_t k;
1577 /* First access returns a zero block */
1578 if (read_and_set(n) == 0) {
1579 memcpy(buf, zero, block_size);
1580 return;
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. */
1589 void
1590 get_super_block(void *buf)
1592 ssize_t k;
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. */
1601 void
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);
1611 static ssize_t
1612 mkfs_write(void * buf, size_t count)
1614 uint64_t fssize;
1615 ssize_t w;
1617 /* Perform & check write */
1618 w = write(fd, buf, count);
1619 if(w < 0)
1620 err(1, "mkfs_write: write failed");
1621 if(w != count)
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;
1633 return w;
1636 /* Seek to position in FS we're creating. */
1637 static uint64_t
1638 mkfs_seek(uint64_t pos, int whence)
1640 if(whence == SEEK_SET) pos += fs_offset_bytes;
1641 #ifdef __minix
1642 uint64_t newpos;
1643 if((lseek64(fd, pos, whence, &newpos)) < 0)
1644 err(1, "mkfs_seek: lseek64 failed");
1645 return newpos;
1646 #else
1647 off_t newpos;
1648 if((newpos=lseek(fd, pos, whence)) == (off_t) -1)
1649 err(1, "mkfs_seek: lseek failed");
1650 return newpos;
1651 #endif