1 /* mkfs - make the MINIX filesystem Authors: Tanenbaum et al. */ 2 3 /* Authors: Andy Tanenbaum, Paul Ogilvie, Frans Meulenbroeks, Bruce Evans */ 4 5 #if HAVE_NBTOOL_CONFIG_H 6 #include "nbtool_config.h" 7 #endif 8 9 #include <sys/cdefs.h> 10 #include <sys/types.h> 11 #include <sys/stat.h> 12 13 #if defined(__minix) 14 #include <minix/minlib.h> 15 #include <minix/partition.h> 16 #include <sys/ioctl.h> 17 #elif defined(__linux__) 18 #include <mntent.h> 19 #endif 20 21 #include <assert.h> 22 #include <err.h> 23 #include <errno.h> 24 #include <fcntl.h> 25 #include <limits.h> 26 #include <stdarg.h> 27 #include <stdint.h> 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <string.h> 31 #include <time.h> 32 #include <unistd.h> 33 34 /* Definition of the file system layout: */ 35 #include "const.h" 36 #include "type.h" 37 #include "mfsdir.h" 38 #include "super.h" 39 40 #define INODE_MAP START_BLOCK 41 /* inode zone indexes pointing to single and double indirect zones */ 42 #define S_INDIRECT_IDX (NR_DZONES) 43 #define D_INDIRECT_IDX (NR_DZONES+1) 44 45 46 #define MAX_TOKENS 10 47 #define LINE_LEN 200 48 /* XXX why do we not use 0 / SU_ID ? */ 49 #define BIN 2 50 #define BINGRP 2 51 52 /* some Minix specific types that do not conflict with Posix */ 53 #ifndef block_t 54 typedef uint32_t block_t; /* block number */ 55 #endif 56 #ifndef zone_t 57 typedef uint32_t zone_t; /* zone number */ 58 #endif 59 #ifndef bit_t 60 typedef uint32_t bit_t; /* bit number in a bit map */ 61 #endif 62 #ifndef bitchunk_t 63 typedef uint32_t bitchunk_t; /* collection of bits in a bitmap */ 64 #endif 65 66 struct fs_size { 67 ino_t inocount; /* amount of inodes */ 68 zone_t zonecount; /* amount of zones */ 69 block_t blockcount; /* amount of blocks */ 70 }; 71 72 extern char *optarg; 73 extern int optind; 74 75 block_t nrblocks; 76 int zone_per_block, zone_shift = 0; 77 zone_t next_zone, zoff, nr_indirzones; 78 int inodes_per_block, indir_per_block, indir_per_zone; 79 unsigned int zone_size; 80 ino_t nrinodes, inode_offset, next_inode; 81 int lct = 0, fd, print = 0; 82 int simple = 0, dflag = 0, verbose = 0; 83 int donttest; /* skip test if it fits on medium */ 84 char *progname; 85 uint64_t fs_offset_bytes, fs_offset_blocks, written_fs_size = 0; 86 87 time_t current_time; 88 char *zero; 89 unsigned char *umap_array; /* bit map tells if block read yet */ 90 size_t umap_array_elements; 91 block_t zone_map; /* where is zone map? (depends on # inodes) */ 92 #ifndef MFS_STATIC_BLOCK_SIZE 93 size_t block_size; 94 #else 95 #define block_size MFS_STATIC_BLOCK_SIZE 96 #endif 97 98 FILE *proto; 99 100 int main(int argc, char **argv); 101 void detect_fs_size(struct fs_size * fssize); 102 void sizeup_dir(struct fs_size * fssize); 103 block_t sizeup(char *device); 104 static int bitmapsize(bit_t nr_bits, size_t blk_size); 105 void super(zone_t zones, ino_t inodes); 106 void rootdir(ino_t inode); 107 void enter_symlink(ino_t inode, char *link); 108 int dir_try_enter(zone_t z, ino_t child, char const *name); 109 void eat_dir(ino_t parent); 110 void eat_file(ino_t inode, int f); 111 void enter_dir(ino_t parent, char const *name, ino_t child); 112 void add_zone(ino_t n, zone_t z, size_t bytes, time_t cur_time); 113 void incr_link(ino_t n); 114 void incr_size(ino_t n, size_t count); 115 static ino_t alloc_inode(int mode, int usrid, int grpid); 116 static zone_t alloc_zone(void); 117 void insert_bit(block_t block, bit_t bit); 118 int mode_con(char *p); 119 void get_line(char line[LINE_LEN], char *parse[MAX_TOKENS]); 120 void check_mtab(const char *devname); 121 time_t file_time(int f); 122 __dead void pexit(char const *s, ...) __printflike(1,2); 123 void *alloc_block(void); 124 void print_fs(void); 125 int read_and_set(block_t n); 126 void special(char *string, int insertmode); 127 __dead void usage(void); 128 void get_block(block_t n, void *buf); 129 void get_super_block(void *buf); 130 void put_block(block_t n, void *buf); 131 static uint64_t mkfs_seek(uint64_t pos, int whence); 132 static ssize_t mkfs_write(void * buf, size_t count); 133 134 /*================================================================ 135 * mkfs - make filesystem 136 *===============================================================*/ 137 int 138 main(int argc, char *argv[]) 139 { 140 int nread, mode, usrid, grpid, ch, extra_space_percent, Tflag = 0; 141 block_t blocks, maxblocks, bblocks; 142 ino_t inodes, root_inum; 143 char *token[MAX_TOKENS], line[LINE_LEN], *sfx; 144 struct fs_size fssize; 145 int insertmode = 0; 146 147 progname = argv[0]; 148 149 /* Process switches. */ 150 blocks = 0; 151 inodes = 0; 152 bblocks = 0; 153 #ifndef MFS_STATIC_BLOCK_SIZE 154 block_size = 0; 155 #endif 156 zone_shift = 0; 157 extra_space_percent = 0; 158 while ((ch = getopt(argc, argv, "B:b:di:ltvx:z:I:T:")) != EOF) 159 switch (ch) { 160 #ifndef MFS_STATIC_BLOCK_SIZE 161 case 'B': 162 block_size = strtoul(optarg, &sfx, 0); 163 switch(*sfx) { 164 case 'b': case 'B': /* bytes; NetBSD-compatible */ 165 case '\0': break; 166 case 'K': 167 case 'k': block_size*=1024; break; 168 case 's': block_size*=SECTOR_SIZE; break; 169 default: usage(); 170 } 171 break; 172 #else 173 case 'B': 174 if (block_size != strtoul(optarg, (char **) NULL, 0)) 175 errx(4, "block size must be exactly %d bytes", 176 MFS_STATIC_BLOCK_SIZE); 177 break; 178 (void)sfx; /* shut up warnings about unused variable...*/ 179 #endif 180 case 'I': 181 fs_offset_bytes = strtoul(optarg, (char **) NULL, 0); 182 insertmode = 1; 183 break; 184 case 'b': 185 blocks = bblocks = strtoul(optarg, (char **) NULL, 0); 186 break; 187 case 'T': 188 Tflag = 1; 189 current_time = strtoul(optarg, (char **) NULL, 0); 190 break; 191 case 'd': 192 dflag = 1; 193 break; 194 case 'i': 195 inodes = strtoul(optarg, (char **) NULL, 0); 196 break; 197 case 'l': print = 1; break; 198 case 't': donttest = 1; break; 199 case 'v': ++verbose; break; 200 case 'x': extra_space_percent = atoi(optarg); break; 201 case 'z': zone_shift = atoi(optarg); break; 202 default: usage(); 203 } 204 205 if (argc == optind) usage(); 206 207 /* Get the current time, set it to the mod time of the binary of 208 * mkfs itself when the -d flag is used. The 'current' time is put into 209 * the i_mtimes of all the files. This -d feature is useful when 210 * producing a set of file systems, and one wants all the times to be 211 * identical. First you set the time of the mkfs binary to what you 212 * want, then go. 213 */ 214 if(Tflag) { 215 if(dflag) 216 errx(1, "-T and -d both specify a time and so are mutually exclusive"); 217 } else if(dflag) { 218 struct stat statbuf; 219 if (stat(progname, &statbuf)) { 220 err(1, "stat of itself"); 221 } 222 current_time = statbuf.st_mtime; 223 } else { 224 current_time = time((time_t *) 0); /* time mkfs is being run */ 225 } 226 227 /* Percentage of extra size must be nonnegative. 228 * It can legitimately be bigger than 100 but has to make some sort of sense. 229 */ 230 if(extra_space_percent < 0 || extra_space_percent > 2000) usage(); 231 232 #ifdef DEFAULT_BLOCK_SIZE 233 if(!block_size) block_size = DEFAULT_BLOCK_SIZE; 234 #endif 235 if (block_size % SECTOR_SIZE) 236 errx(4, "block size must be multiple of sector (%d bytes)", SECTOR_SIZE); 237 #ifdef MIN_BLOCK_SIZE 238 if (block_size < MIN_BLOCK_SIZE) 239 errx(4, "block size must be at least %d bytes", MIN_BLOCK_SIZE); 240 #endif 241 #ifdef MAX_BLOCK_SIZE 242 if (block_size > MAX_BLOCK_SIZE) 243 errx(4, "block size must be at most %d bytes", MAX_BLOCK_SIZE); 244 #endif 245 if(block_size%INODE_SIZE) 246 errx(4, "block size must be a multiple of inode size (%d bytes)", INODE_SIZE); 247 248 if(zone_shift < 0 || zone_shift > 14) 249 errx(4, "zone_shift must be a small non-negative integer"); 250 zone_per_block = 1 << zone_shift; /* nr of blocks per zone */ 251 252 inodes_per_block = INODES_PER_BLOCK(block_size); 253 indir_per_block = INDIRECTS(block_size); 254 indir_per_zone = INDIRECTS(block_size) << zone_shift; 255 /* number of file zones we can address directly and with a single indirect*/ 256 nr_indirzones = NR_DZONES + indir_per_zone; 257 zone_size = block_size << zone_shift; 258 /* Checks for an overflow: only with very big block size */ 259 if (zone_size <= 0) 260 errx(4, "Zones are too big for this program; smaller -B or -z, please!"); 261 262 /* now that the block size is known, do buffer allocations where 263 * possible. 264 */ 265 zero = alloc_block(); 266 267 fs_offset_blocks = roundup(fs_offset_bytes, block_size) / block_size; 268 269 /* Determine the size of the device if not specified as -b or proto. */ 270 maxblocks = sizeup(argv[optind]); 271 if (bblocks != 0 && bblocks + fs_offset_blocks > maxblocks && !insertmode) { 272 errx(4, "Given size -b %d exeeds device capacity(%d)\n", bblocks, maxblocks); 273 } 274 275 if (argc - optind == 1 && bblocks == 0) { 276 blocks = maxblocks; 277 /* blocks == 0 is checked later, but leads to a funny way of 278 * reporting a 0-sized device (displays usage). 279 */ 280 if(blocks < 1) { 281 errx(1, "zero size device."); 282 } 283 } 284 285 /* The remaining args must be 'special proto', or just 'special' if the 286 * no. of blocks has already been specified. 287 */ 288 if (argc - optind != 2 && (argc - optind != 1 || blocks == 0)) usage(); 289 290 if (maxblocks && blocks > maxblocks && !insertmode) { 291 errx(1, "%s: number of blocks too large for device.", argv[optind]); 292 } 293 294 /* Check special. */ 295 check_mtab(argv[optind]); 296 297 /* Check and start processing proto. */ 298 optarg = argv[++optind]; 299 if (optind < argc && (proto = fopen(optarg, "r")) != NULL) { 300 /* Prototype file is readable. */ 301 lct = 1; 302 get_line(line, token); /* skip boot block info */ 303 304 /* Read the line with the block and inode counts. */ 305 get_line(line, token); 306 if (bblocks == 0){ 307 blocks = strtol(token[0], (char **) NULL, 10); 308 } else { 309 if(bblocks < strtol(token[0], (char **) NULL, 10)) { 310 errx(1, "%s: number of blocks given as parameter(%d)" 311 " is too small for given proto file(%d).", 312 argv[optind], bblocks, 313 strtol(token[0], (char **) NULL, 10)); 314 }; 315 } 316 inodes = strtol(token[1], (char **) NULL, 10); 317 318 /* Process mode line for root directory. */ 319 get_line(line, token); 320 mode = mode_con(token[0]); 321 usrid = atoi(token[1]); 322 grpid = atoi(token[2]); 323 324 if(blocks <= 0 && inodes <= 0){ 325 detect_fs_size(&fssize); 326 blocks = fssize.blockcount; 327 inodes = fssize.inocount; 328 blocks += blocks*extra_space_percent/100; 329 inodes += inodes*extra_space_percent/100; 330 /* XXX is it OK to write on stdout? Use warn() instead? Also consider using verbose */ 331 fprintf(stderr, "dynamically sized filesystem: %u blocks, %u inodes\n", 332 (unsigned int) blocks, (unsigned int) inodes); 333 } 334 } else { 335 lct = 0; 336 if (optind < argc) { 337 /* Maybe the prototype file is just a size. Check. */ 338 blocks = strtoul(optarg, (char **) NULL, 0); 339 if (blocks == 0) errx(2, "Can't open prototype file"); 340 } 341 342 /* Make simple file system of the given size, using defaults. */ 343 mode = 040777; 344 usrid = BIN; 345 grpid = BINGRP; 346 simple = 1; 347 } 348 349 if (inodes == 0) { 350 long long kb = ((unsigned long long)blocks*block_size) / 1024; 351 352 inodes = kb / 2; 353 if (kb >= 100000) inodes = kb / 4; 354 if (kb >= 1000000) inodes = kb / 6; 355 if (kb >= 10000000) inodes = kb / 8; 356 if (kb >= 100000000) inodes = kb / 10; 357 if (kb >= 1000000000) inodes = kb / 12; 358 /* XXX check overflow: with very large number of blocks, this results in insanely large number of inodes */ 359 /* XXX check underflow (if/when ino_t is signed), else the message below will look strange */ 360 361 /* round up to fill inode block */ 362 inodes += inodes_per_block - 1; 363 inodes = inodes / inodes_per_block * inodes_per_block; 364 } 365 366 if (blocks < 5) errx(1, "Block count too small"); 367 if (inodes < 1) errx(1, "Inode count too small"); 368 369 nrblocks = blocks; 370 nrinodes = inodes; 371 372 umap_array_elements = 1 + blocks/8; 373 if(!(umap_array = malloc(umap_array_elements))) 374 err(1, "can't allocate block bitmap (%u bytes).", 375 (unsigned)umap_array_elements); 376 377 /* Open special. */ 378 special(argv[--optind], insertmode); 379 380 if (!donttest) { 381 uint16_t *testb; 382 ssize_t w; 383 384 testb = alloc_block(); 385 386 /* Try writing the last block of partition or diskette. */ 387 mkfs_seek((uint64_t)(blocks - 1) * block_size, SEEK_SET); 388 testb[0] = 0x3245; 389 testb[1] = 0x11FF; 390 testb[block_size/2-1] = 0x1F2F; 391 w=mkfs_write(testb, block_size); 392 sync(); /* flush write, so if error next read fails */ 393 mkfs_seek((uint64_t)(blocks - 1) * block_size, SEEK_SET); 394 testb[0] = 0; 395 testb[1] = 0; 396 testb[block_size/2-1] = 0; 397 nread = read(fd, testb, block_size); 398 if (nread != block_size || testb[0] != 0x3245 || testb[1] != 0x11FF || 399 testb[block_size/2-1] != 0x1F2F) { 400 warn("nread = %d\n", nread); 401 warnx("testb = 0x%x 0x%x 0x%x\n", 402 testb[0], testb[1], testb[block_size-1]); 403 errx(1, "File system is too big for minor device (read)"); 404 } 405 mkfs_seek((uint64_t)(blocks - 1) * block_size, SEEK_SET); 406 testb[0] = 0; 407 testb[1] = 0; 408 testb[block_size/2-1] = 0; 409 mkfs_write(testb, block_size); 410 mkfs_seek(0L, SEEK_SET); 411 free(testb); 412 } 413 414 /* Make the file-system */ 415 416 put_block(BOOT_BLOCK, zero); /* Write a null boot block. */ 417 put_block(BOOT_BLOCK+1, zero); /* Write another null block. */ 418 419 super(nrblocks >> zone_shift, inodes); 420 421 root_inum = alloc_inode(mode, usrid, grpid); 422 rootdir(root_inum); 423 if (simple == 0) eat_dir(root_inum); 424 425 if (print) print_fs(); 426 else if (verbose > 1) { 427 if (zone_shift) 428 fprintf(stderr, "%d inodes used. %u zones (%u blocks) used.\n", 429 (int)next_inode-1, next_zone, next_zone*zone_per_block); 430 else 431 fprintf(stderr, "%d inodes used. %u zones used.\n", 432 (int)next_inode-1, next_zone); 433 } 434 435 if(insertmode) printf("%ld\n", written_fs_size); 436 437 return(0); 438 439 /* NOTREACHED */ 440 } /* end main */ 441 442 /*================================================================ 443 * detect_fs_size - determine image size dynamically 444 *===============================================================*/ 445 void 446 detect_fs_size(struct fs_size * fssize) 447 { 448 int prev_lct = lct; 449 off_t point = ftell(proto); 450 block_t initb; 451 zone_t initz; 452 453 fssize->inocount = 1; /* root directory node */ 454 fssize->zonecount = 0; 455 fssize->blockcount = 0; 456 457 sizeup_dir(fssize); 458 459 initb = bitmapsize(1 + fssize->inocount, block_size); 460 initb += bitmapsize(fssize->zonecount, block_size); 461 initb += START_BLOCK; 462 initb += (fssize->inocount + inodes_per_block - 1) / inodes_per_block; 463 initz = (initb + zone_per_block - 1) >> zone_shift; 464 465 fssize->blockcount = initb+ fssize->zonecount; 466 lct = prev_lct; 467 fseek(proto, point, SEEK_SET); 468 } 469 470 void 471 sizeup_dir(struct fs_size * fssize) 472 { 473 char *token[MAX_TOKENS], *p; 474 char line[LINE_LEN]; 475 FILE *f; 476 off_t size; 477 int dir_entries = 2; 478 zone_t dir_zones = 0, fzones, indirects; 479 480 while (1) { 481 get_line(line, token); 482 p = token[0]; 483 if (*p == '$') { 484 dir_zones = (dir_entries / (NR_DIR_ENTRIES(block_size) * zone_per_block)); 485 if(dir_entries % (NR_DIR_ENTRIES(block_size) * zone_per_block)) 486 dir_zones++; 487 if(dir_zones > NR_DZONES) 488 dir_zones++; /* Max single indir */ 489 fssize->zonecount += dir_zones; 490 return; 491 } 492 493 p = token[1]; 494 fssize->inocount++; 495 dir_entries++; 496 497 if (*p == 'd') { 498 sizeup_dir(fssize); 499 } else if (*p == 'b' || *p == 'c') { 500 501 } else if (*p == 's') { 502 fssize->zonecount++; /* Symlink contents is always stored a block */ 503 } else { 504 if ((f = fopen(token[4], "rb")) == NULL) { 505 warn("dynamic sizing: can't open %s", token[4]); 506 } else if (fseek(f, 0, SEEK_END) < 0) { 507 pexit("dynamic size detection failed: seek to end of %s", 508 token[4]); 509 } else if ( (size = ftell(f)) == (off_t)(-1)) { 510 pexit("dynamic size detection failed: can't tell size of %s", 511 token[4]); 512 } else { 513 fclose(f); 514 fzones = roundup(size, zone_size) / zone_size; 515 indirects = 0; 516 /* XXX overflow? fzones is u32, size is potentially 64-bit */ 517 if (fzones > NR_DZONES) 518 indirects++; /* single indirect needed */ 519 if (fzones > nr_indirzones) { 520 /* Each further group of 'indir_per_zone' 521 * needs one supplementary indirect zone: 522 */ 523 indirects += roundup(fzones - nr_indirzones, 524 indir_per_zone) / indir_per_zone; 525 indirects++; /* + double indirect needed!*/ 526 } 527 fssize->zonecount += fzones + indirects; 528 } 529 } 530 } 531 } 532 533 /*================================================================ 534 * sizeup - determine device size 535 *===============================================================*/ 536 block_t 537 sizeup(char * device) 538 { 539 block_t d; 540 #if defined(__minix) 541 uint64_t bytes, resize; 542 uint32_t rem; 543 #else 544 off_t size; 545 #endif 546 547 548 if ((fd = open(device, O_RDONLY)) == -1) { 549 if (errno != ENOENT) 550 perror("sizeup open"); 551 return 0; 552 } 553 554 #if defined(__minix) 555 if(minix_sizeup(device, &bytes) < 0) { 556 perror("sizeup"); 557 return 0; 558 } 559 560 d = (uint32_t)(bytes / block_size); 561 rem = (uint32_t)(bytes % block_size); 562 563 resize = ((uint64_t)d * block_size) + rem; 564 if(resize != bytes) { 565 /* Assume block_t is unsigned */ 566 d = (block_t)(-1ul); 567 fprintf(stderr, "%s: truncating FS at %lu blocks\n", 568 progname, (unsigned long)d); 569 } 570 #else 571 size = mkfs_seek(0, SEEK_END); 572 /* Assume block_t is unsigned */ 573 if (size / block_size > (block_t)(-1ul)) { 574 d = (block_t)(-1ul); 575 fprintf(stderr, "%s: truncating FS at %lu blocks\n", 576 progname, (unsigned long)d); 577 } else 578 d = size / block_size; 579 #endif 580 581 return d; 582 } 583 584 /* 585 * copied from fslib 586 */ 587 static int 588 bitmapsize(bit_t nr_bits, size_t blk_size) 589 { 590 block_t nr_blocks; 591 592 nr_blocks = nr_bits / FS_BITS_PER_BLOCK(blk_size); 593 if (nr_blocks * FS_BITS_PER_BLOCK(blk_size) < nr_bits) 594 ++nr_blocks; 595 return(nr_blocks); 596 } 597 598 /*================================================================ 599 * super - construct a superblock 600 *===============================================================*/ 601 602 void 603 super(zone_t zones, ino_t inodes) 604 { 605 block_t inodeblks, initblks, i; 606 unsigned long nb; 607 long long ind_per_zone, zo; 608 void *buf; 609 struct super_block *sup; 610 611 sup = buf = alloc_block(); 612 613 #ifdef MFSFLAG_CLEAN 614 /* The assumption is that mkfs will create a clean FS. */ 615 sup->s_flags = MFSFLAG_CLEAN; 616 #endif 617 618 sup->s_ninodes = inodes; 619 /* Check for overflow; cannot happen on V3 file systems */ 620 if(inodes != sup->s_ninodes) 621 errx(1, "Too much inodes for that version of Minix FS."); 622 sup->s_nzones = 0; /* not used in V2 - 0 forces errors early */ 623 sup->s_zones = zones; 624 /* Check for overflow; can only happen on V1 file systems */ 625 if(zones != sup->s_zones) 626 errx(1, "Too much zones (blocks) for that version of Minix FS."); 627 628 #ifndef MFS_STATIC_BLOCK_SIZE 629 #define BIGGERBLOCKS "Please try a larger block size for an FS of this size." 630 #else 631 #define BIGGERBLOCKS "Please use MinixFS V3 for an FS of this size." 632 #endif 633 sup->s_imap_blocks = nb = bitmapsize(1 + inodes, block_size); 634 /* Checks for an overflow: nb is uint32_t while s_imap_blocks is of type 635 * int16_t */ 636 if(sup->s_imap_blocks != nb) { 637 errx(1, "too many inode bitmap blocks.\n" BIGGERBLOCKS); 638 } 639 sup->s_zmap_blocks = nb = bitmapsize(zones, block_size); 640 /* Idem here check for overflow */ 641 if(nb != sup->s_zmap_blocks) { 642 errx(1, "too many block bitmap blocks.\n" BIGGERBLOCKS); 643 } 644 inode_offset = START_BLOCK + sup->s_imap_blocks + sup->s_zmap_blocks; 645 inodeblks = (inodes + inodes_per_block - 1) / inodes_per_block; 646 initblks = inode_offset + inodeblks; 647 sup->s_firstdatazone_old = nb = 648 (initblks + (1 << zone_shift) - 1) >> zone_shift; 649 if(nb >= zones) errx(1, "bit maps too large"); 650 if(nb != sup->s_firstdatazone_old) { 651 /* The field is too small to store the value. Fortunately, the value 652 * can be computed from other fields. We set the on-disk field to zero 653 * to indicate that it must not be used. Eventually, we can always set 654 * the on-disk field to zero, and stop using it. 655 */ 656 sup->s_firstdatazone_old = 0; 657 } 658 sup->s_firstdatazone = nb; 659 zoff = sup->s_firstdatazone - 1; 660 sup->s_log_zone_size = zone_shift; 661 sup->s_magic = SUPER_MAGIC; 662 #ifdef MFS_SUPER_BLOCK_SIZE 663 sup->s_block_size = block_size; 664 /* Check for overflow */ 665 if(block_size != sup->MFS_SUPER_BLOCK_SIZE) 666 errx(1, "block_size too large."); 667 sup->s_disk_version = 0; 668 #endif 669 670 ind_per_zone = (long long) indir_per_zone; 671 zo = NR_DZONES + ind_per_zone + ind_per_zone*ind_per_zone; 672 #ifndef MAX_MAX_SIZE 673 #define MAX_MAX_SIZE (INT32_MAX) 674 #endif 675 if(MAX_MAX_SIZE/block_size < zo) { 676 sup->s_max_size = MAX_MAX_SIZE; 677 } 678 else { 679 sup->s_max_size = zo * block_size; 680 } 681 682 if (verbose>1) { 683 fprintf(stderr, "Super block values:\n" 684 "\tnumber of inodes\t%12d\n" 685 "\tnumber of zones \t%12d\n" 686 "\tinode bit map blocks\t%12d\n" 687 "\tzone bit map blocks\t%12d\n" 688 "\tfirst data zone \t%12d\n" 689 "\tblocks per zone shift\t%12d\n" 690 "\tmaximum file size\t%12d\n" 691 "\tmagic number\t\t%#12X\n", 692 sup->s_ninodes, sup->s_zones, 693 sup->s_imap_blocks, sup->s_zmap_blocks, sup->s_firstdatazone, 694 sup->s_log_zone_size, sup->s_max_size, sup->s_magic); 695 #ifdef MFS_SUPER_BLOCK_SIZE 696 fprintf(stderr, "\tblock size\t\t%12d\n", sup->s_block_size); 697 #endif 698 } 699 700 mkfs_seek((off_t) SUPER_BLOCK_BYTES, SEEK_SET); 701 mkfs_write(buf, SUPER_BLOCK_BYTES); 702 703 /* Clear maps and inodes. */ 704 for (i = START_BLOCK; i < initblks; i++) put_block((block_t) i, zero); 705 706 next_zone = sup->s_firstdatazone; 707 next_inode = 1; 708 709 zone_map = INODE_MAP + sup->s_imap_blocks; 710 711 insert_bit(zone_map, 0); /* bit zero must always be allocated */ 712 insert_bit((block_t) INODE_MAP, 0); /* inode zero not used but 713 * must be allocated */ 714 715 free(buf); 716 } 717 718 719 /*================================================================ 720 * rootdir - install the root directory 721 *===============================================================*/ 722 void 723 rootdir(ino_t inode) 724 { 725 zone_t z; 726 727 z = alloc_zone(); 728 add_zone(inode, z, 2 * sizeof(struct direct), current_time); 729 enter_dir(inode, ".", inode); 730 enter_dir(inode, "..", inode); 731 incr_link(inode); 732 incr_link(inode); 733 } 734 735 void 736 enter_symlink(ino_t inode, char *lnk) 737 { 738 zone_t z; 739 size_t len; 740 char *buf; 741 742 buf = alloc_block(); 743 z = alloc_zone(); 744 len = strlen(lnk); 745 if (len >= block_size) 746 pexit("symlink too long, max length is %u", (unsigned)block_size - 1); 747 strcpy(buf, lnk); 748 put_block((z << zone_shift), buf); 749 750 add_zone(inode, z, len, current_time); 751 752 free(buf); 753 } 754 755 756 /*================================================================ 757 * eat_dir - recursively install directory 758 *===============================================================*/ 759 void 760 eat_dir(ino_t parent) 761 { 762 /* Read prototype lines and set up directory. Recurse if need be. */ 763 char *token[MAX_TOKENS], *p; 764 char line[LINE_LEN]; 765 int mode, usrid, grpid, maj, min, f; 766 ino_t n; 767 zone_t z; 768 size_t size; 769 770 while (1) { 771 get_line(line, token); 772 p = token[0]; 773 if (*p == '$') return; 774 p = token[1]; 775 mode = mode_con(p); 776 usrid = atoi(token[2]); 777 grpid = atoi(token[3]); 778 n = alloc_inode(mode, usrid, grpid); 779 780 /* Enter name in directory and update directory's size. */ 781 enter_dir(parent, token[0], n); 782 incr_size(parent, sizeof(struct direct)); 783 784 /* Check to see if file is directory or special. */ 785 incr_link(n); 786 if (*p == 'd') { 787 /* This is a directory. */ 788 z = alloc_zone(); /* zone for new directory */ 789 add_zone(n, z, 2 * sizeof(struct direct), current_time); 790 enter_dir(n, ".", n); 791 enter_dir(n, "..", parent); 792 incr_link(parent); 793 incr_link(n); 794 eat_dir(n); 795 } else if (*p == 'b' || *p == 'c') { 796 /* Special file. */ 797 maj = atoi(token[4]); 798 min = atoi(token[5]); 799 size = 0; 800 if (token[6]) size = atoi(token[6]); 801 size = block_size * size; 802 add_zone(n, (zone_t) (makedev(maj,min)), size, current_time); 803 } else if (*p == 's') { 804 enter_symlink(n, token[4]); 805 } else { 806 /* Regular file. Go read it. */ 807 if ((f = open(token[4], O_RDONLY)) < 0) { 808 fprintf(stderr, "%s: Can't open %s: %s\n", 809 progname, token[4], strerror(errno)); 810 } else { 811 eat_file(n, f); 812 } 813 } 814 } 815 816 } 817 818 /*================================================================ 819 * eat_file - copy file to MINIX 820 *===============================================================*/ 821 /* Zonesize >= blocksize */ 822 void 823 eat_file(ino_t inode, int f) 824 { 825 int ct = 0, i, j; 826 zone_t z = 0; 827 char *buf; 828 time_t timeval; 829 830 buf = alloc_block(); 831 832 do { 833 for (i = 0, j = 0; i < zone_per_block; i++, j += ct) { 834 memset(buf, 0, block_size); 835 if ((ct = read(f, buf, block_size)) > 0) { 836 if (i == 0) z = alloc_zone(); 837 put_block((z << zone_shift) + i, buf); 838 } 839 } 840 timeval = (dflag ? current_time : file_time(f)); 841 if (ct) add_zone(inode, z, (size_t) j, timeval); 842 } while (ct == block_size); 843 close(f); 844 free(buf); 845 } 846 847 int 848 dir_try_enter(zone_t z, ino_t child, char const *name) 849 { 850 struct direct *dir_entry = alloc_block(); 851 int r = 0; 852 block_t b; 853 int i, l; 854 855 b = z << zone_shift; 856 for (l = 0; l < zone_per_block; l++, b++) { 857 get_block(b, dir_entry); 858 859 for (i = 0; i < NR_DIR_ENTRIES(block_size); i++) 860 if (!dir_entry[i].d_ino) 861 break; 862 863 if(i < NR_DIR_ENTRIES(block_size)) { 864 r = 1; 865 dir_entry[i].d_ino = child; 866 assert(sizeof(dir_entry[i].d_name) == MFS_DIRSIZ); 867 if (verbose && strlen(name) > MFS_DIRSIZ) 868 fprintf(stderr, "File name %s is too long, truncated\n", name); 869 strncpy(dir_entry[i].d_name, name, MFS_DIRSIZ); 870 put_block(b, dir_entry); 871 break; 872 } 873 } 874 875 free(dir_entry); 876 877 return r; 878 } 879 880 /*================================================================ 881 * directory & inode management assist group 882 *===============================================================*/ 883 void 884 enter_dir(ino_t parent, char const *name, ino_t child) 885 { 886 /* Enter child in parent directory */ 887 /* Works for dir > 1 block and zone > block */ 888 unsigned int k; 889 block_t b, indir; 890 zone_t z; 891 int off; 892 struct inode *ino; 893 struct inode *inoblock = alloc_block(); 894 zone_t *indirblock = alloc_block(); 895 896 assert(!(block_size % sizeof(struct direct))); 897 898 /* Obtain the inode structure */ 899 b = ((parent - 1) / inodes_per_block) + inode_offset; 900 off = (parent - 1) % inodes_per_block; 901 get_block(b, inoblock); 902 ino = inoblock + off; 903 904 for (k = 0; k < NR_DZONES; k++) { 905 z = ino->i_zone[k]; 906 if (z == 0) { 907 z = alloc_zone(); 908 ino->i_zone[k] = z; 909 } 910 911 if(dir_try_enter(z, child, __UNCONST(name))) { 912 put_block(b, inoblock); 913 free(inoblock); 914 free(indirblock); 915 return; 916 } 917 } 918 919 /* no space in directory using just direct blocks; try indirect */ 920 if (ino->i_zone[S_INDIRECT_IDX] == 0) 921 ino->i_zone[S_INDIRECT_IDX] = alloc_zone(); 922 923 indir = ino->i_zone[S_INDIRECT_IDX] << zone_shift; 924 --indir; /* Compensate for ++indir below */ 925 for(k = 0; k < (indir_per_zone); k++) { 926 if (k % indir_per_block == 0) 927 get_block(++indir, indirblock); 928 z = indirblock[k % indir_per_block]; 929 if(!z) { 930 z = indirblock[k % indir_per_block] = alloc_zone(); 931 put_block(indir, indirblock); 932 } 933 if(dir_try_enter(z, child, __UNCONST(name))) { 934 put_block(b, inoblock); 935 free(inoblock); 936 free(indirblock); 937 return; 938 } 939 } 940 941 pexit("Directory-inode %u beyond single indirect blocks. Could not enter %s", 942 (unsigned)parent, name); 943 } 944 945 946 void 947 add_zone(ino_t n, zone_t z, size_t bytes, time_t mtime) 948 { 949 /* Add zone z to inode n. The file has grown by 'bytes' bytes. */ 950 951 int off, i, j; 952 block_t b; 953 zone_t indir, dindir; 954 struct inode *p, *inode; 955 zone_t *blk, *dblk; 956 957 assert(inodes_per_block*sizeof(*inode) == block_size); 958 if(!(inode = alloc_block())) 959 err(1, "Couldn't allocate block of inodes"); 960 961 b = ((n - 1) / inodes_per_block) + inode_offset; 962 off = (n - 1) % inodes_per_block; 963 get_block(b, inode); 964 p = &inode[off]; 965 p->i_size += bytes; 966 p->i_mtime = mtime; 967 #ifndef MFS_INODE_ONLY_MTIME /* V1 file systems did not have them... */ 968 p->i_atime = p->i_ctime = current_time; 969 #endif 970 for (i = 0; i < NR_DZONES; i++) 971 if (p->i_zone[i] == 0) { 972 p->i_zone[i] = z; 973 put_block(b, inode); 974 free(inode); 975 return; 976 } 977 978 assert(indir_per_block*sizeof(*blk) == block_size); 979 if(!(blk = alloc_block())) 980 err(1, "Couldn't allocate indirect block"); 981 982 /* File has grown beyond a small file. */ 983 if (p->i_zone[S_INDIRECT_IDX] == 0) 984 p->i_zone[S_INDIRECT_IDX] = alloc_zone(); 985 indir = p->i_zone[S_INDIRECT_IDX] << zone_shift; 986 put_block(b, inode); 987 --indir; /* Compensate for ++indir below */ 988 for (i = 0; i < (indir_per_zone); i++) { 989 if (i % indir_per_block == 0) 990 get_block(++indir, blk); 991 if (blk[i % indir_per_block] == 0) { 992 blk[i] = z; 993 put_block(indir, blk); 994 free(blk); 995 free(inode); 996 return; 997 } 998 } 999 1000 /* File has grown beyond single indirect; we need a double indirect */ 1001 assert(indir_per_block*sizeof(*dblk) == block_size); 1002 if(!(dblk = alloc_block())) 1003 err(1, "Couldn't allocate double indirect block"); 1004 1005 if (p->i_zone[D_INDIRECT_IDX] == 0) 1006 p->i_zone[D_INDIRECT_IDX] = alloc_zone(); 1007 dindir = p->i_zone[D_INDIRECT_IDX] << zone_shift; 1008 put_block(b, inode); 1009 --dindir; /* Compensate for ++indir below */ 1010 for (j = 0; j < (indir_per_zone); j++) { 1011 if (j % indir_per_block == 0) 1012 get_block(++dindir, dblk); 1013 if (dblk[j % indir_per_block] == 0) 1014 dblk[j % indir_per_block] = alloc_zone(); 1015 indir = dblk[j % indir_per_block] << zone_shift; 1016 --indir; /* Compensate for ++indir below */ 1017 for (i = 0; i < (indir_per_zone); i++) { 1018 if (i % indir_per_block == 0) 1019 get_block(++indir, blk); 1020 if (blk[i % indir_per_block] == 0) { 1021 blk[i] = z; 1022 put_block(dindir, dblk); 1023 put_block(indir, blk); 1024 free(dblk); 1025 free(blk); 1026 free(inode); 1027 return; 1028 } 1029 } 1030 } 1031 1032 pexit("File has grown beyond double indirect"); 1033 } 1034 1035 1036 /* Increment the link count to inode n */ 1037 void 1038 incr_link(ino_t n) 1039 { 1040 int off; 1041 static int enter = 0; 1042 static struct inode *inodes = NULL; 1043 block_t b; 1044 1045 if (enter++) pexit("internal error: recursive call to incr_link()"); 1046 1047 b = ((n - 1) / inodes_per_block) + inode_offset; 1048 off = (n - 1) % inodes_per_block; 1049 { 1050 assert(sizeof(*inodes) * inodes_per_block == block_size); 1051 if(!inodes && !(inodes = alloc_block())) 1052 err(1, "couldn't allocate a block of inodes"); 1053 1054 get_block(b, inodes); 1055 inodes[off].i_nlinks++; 1056 /* Check overflow (particularly on V1)... */ 1057 if (inodes[off].i_nlinks <= 0) 1058 pexit("Too many links to a directory"); 1059 put_block(b, inodes); 1060 } 1061 enter = 0; 1062 } 1063 1064 1065 /* Increment the file-size in inode n */ 1066 void 1067 incr_size(ino_t n, size_t count) 1068 { 1069 block_t b; 1070 int off; 1071 1072 b = ((n - 1) / inodes_per_block) + inode_offset; 1073 off = (n - 1) % inodes_per_block; 1074 { 1075 struct inode *inodes; 1076 1077 assert(inodes_per_block * sizeof(*inodes) == block_size); 1078 if(!(inodes = alloc_block())) 1079 err(1, "couldn't allocate a block of inodes"); 1080 1081 get_block(b, inodes); 1082 /* Check overflow; avoid compiler spurious warnings */ 1083 if (inodes[off].i_size+(int)count < inodes[off].i_size || 1084 inodes[off].i_size > MAX_MAX_SIZE-(int)count) 1085 pexit("File has become too big to be handled by MFS"); 1086 inodes[off].i_size += count; 1087 put_block(b, inodes); 1088 free(inodes); 1089 } 1090 } 1091 1092 1093 /*================================================================ 1094 * allocation assist group 1095 *===============================================================*/ 1096 static ino_t 1097 alloc_inode(int mode, int usrid, int grpid) 1098 { 1099 ino_t num; 1100 int off; 1101 block_t b; 1102 struct inode *inodes; 1103 1104 num = next_inode++; 1105 if (num > nrinodes) { 1106 pexit("File system does not have enough inodes (only %d)", nrinodes); 1107 } 1108 b = ((num - 1) / inodes_per_block) + inode_offset; 1109 off = (num - 1) % inodes_per_block; 1110 1111 assert(inodes_per_block * sizeof(*inodes) == block_size); 1112 if(!(inodes = alloc_block())) 1113 err(1, "couldn't allocate a block of inodes"); 1114 1115 get_block(b, inodes); 1116 if (inodes[off].i_mode) { 1117 pexit("allocation new inode %d with non-zero mode - this cannot happen", 1118 num); 1119 } 1120 inodes[off].i_mode = mode; 1121 inodes[off].i_uid = usrid; 1122 inodes[off].i_gid = grpid; 1123 if (verbose && (inodes[off].i_uid != usrid || inodes[off].i_gid != grpid)) 1124 fprintf(stderr, "Uid/gid %d.%d do not fit within inode, truncated\n", usrid, grpid); 1125 put_block(b, inodes); 1126 1127 free(inodes); 1128 1129 /* Set the bit in the bit map. */ 1130 insert_bit((block_t) INODE_MAP, num); 1131 return(num); 1132 } 1133 1134 1135 /* Allocate a new zone */ 1136 static zone_t 1137 alloc_zone(void) 1138 { 1139 /* Works for zone > block */ 1140 block_t b; 1141 int i; 1142 zone_t z; 1143 1144 z = next_zone++; 1145 b = z << zone_shift; 1146 if (b > nrblocks - zone_per_block) 1147 pexit("File system not big enough for all the files"); 1148 for (i = 0; i < zone_per_block; i++) 1149 put_block(b + i, zero); /* give an empty zone */ 1150 1151 insert_bit(zone_map, z - zoff); 1152 return z; 1153 } 1154 1155 1156 /* Insert one bit into the bitmap */ 1157 void 1158 insert_bit(block_t map, bit_t bit) 1159 { 1160 int boff, w, s; 1161 unsigned int bits_per_block; 1162 block_t map_block; 1163 bitchunk_t *buf; 1164 1165 buf = alloc_block(); 1166 1167 bits_per_block = FS_BITS_PER_BLOCK(block_size); 1168 map_block = map + bit / bits_per_block; 1169 if (map_block >= inode_offset) 1170 pexit("insertbit invades inodes area - this cannot happen"); 1171 boff = bit % bits_per_block; 1172 1173 assert(boff >=0); 1174 assert(boff < FS_BITS_PER_BLOCK(block_size)); 1175 get_block(map_block, buf); 1176 w = boff / FS_BITCHUNK_BITS; 1177 s = boff % FS_BITCHUNK_BITS; 1178 buf[w] |= (1 << s); 1179 put_block(map_block, buf); 1180 1181 free(buf); 1182 } 1183 1184 1185 /*================================================================ 1186 * proto-file processing assist group 1187 *===============================================================*/ 1188 int mode_con(char *p) 1189 { 1190 /* Convert string to mode */ 1191 int o1, o2, o3, mode; 1192 char c1, c2, c3; 1193 1194 c1 = *p++; 1195 c2 = *p++; 1196 c3 = *p++; 1197 o1 = *p++ - '0'; 1198 o2 = *p++ - '0'; 1199 o3 = *p++ - '0'; 1200 mode = (o1 << 6) | (o2 << 3) | o3; 1201 if (c1 == 'd') mode |= S_IFDIR; 1202 if (c1 == 'b') mode |= S_IFBLK; 1203 if (c1 == 'c') mode |= S_IFCHR; 1204 if (c1 == 's') mode |= S_IFLNK; 1205 if (c1 == 'l') mode |= S_IFLNK; /* just to be somewhat ls-compatible*/ 1206 /* XXX note: some other mkfs programs consider L to create hardlinks */ 1207 if (c1 == '-') mode |= S_IFREG; 1208 if (c2 == 'u') mode |= S_ISUID; 1209 if (c3 == 'g') mode |= S_ISGID; 1210 /* XXX There are no way to encode S_ISVTX */ 1211 return(mode); 1212 } 1213 1214 void 1215 get_line(char line[LINE_LEN], char *parse[MAX_TOKENS]) 1216 { 1217 /* Read a line and break it up in tokens */ 1218 int k; 1219 char c, *p; 1220 int d; 1221 1222 for (k = 0; k < MAX_TOKENS; k++) parse[k] = 0; 1223 memset(line, 0, LINE_LEN); 1224 k = 0; 1225 p = line; 1226 while (1) { 1227 if (++k > LINE_LEN) pexit("Line too long"); 1228 d = fgetc(proto); 1229 if (d == EOF) pexit("Unexpected end-of-file"); 1230 *p = d; 1231 if (*p == ' ' || *p == '\t') *p = 0; 1232 if (*p == '\n') { 1233 lct++; 1234 *p++ = 0; 1235 *p = '\n'; 1236 break; 1237 } 1238 p++; 1239 } 1240 1241 k = 0; 1242 p = line; 1243 while (1) { 1244 c = *p++; 1245 if (c == '\n') return; 1246 if (c == 0) continue; 1247 parse[k++] = p - 1; 1248 do { 1249 c = *p++; 1250 } while (c != 0 && c != '\n'); 1251 } 1252 } 1253 1254 1255 /*================================================================ 1256 * other stuff 1257 *===============================================================*/ 1258 1259 /* 1260 * Check to see if the special file named 'device' is mounted. 1261 */ 1262 void 1263 check_mtab(const char *device) /* /dev/hd1 or whatever */ 1264 { 1265 #if defined(__minix) 1266 int n, r; 1267 struct stat sb; 1268 char dev[PATH_MAX], mount_point[PATH_MAX], 1269 type[MNTNAMELEN], flags[MNTFLAGLEN]; 1270 1271 r= stat(device, &sb); 1272 if (r == -1) 1273 { 1274 if (errno == ENOENT) 1275 return; /* Does not exist, and therefore not mounted. */ 1276 err(1, "stat %s failed", device); 1277 } 1278 if (!S_ISBLK(sb.st_mode)) 1279 { 1280 /* Not a block device and therefore not mounted. */ 1281 return; 1282 } 1283 1284 if (load_mtab(__UNCONST("mkfs")) < 0) return; 1285 while (1) { 1286 n = get_mtab_entry(dev, mount_point, type, flags); 1287 if (n < 0) return; 1288 if (strcmp(device, dev) == 0) { 1289 /* Can't mkfs on top of a mounted file system. */ 1290 errx(1, "%s is mounted on %s", device, mount_point); 1291 } 1292 } 1293 #elif defined(__linux__) 1294 /* XXX: this code is copyright Theodore T'so and distributed under the GPLv2. Rewrite. 1295 */ 1296 struct mntent *mnt; 1297 struct stat st_buf; 1298 dev_t file_dev=0, file_rdev=0; 1299 ino_t file_ino=0; 1300 FILE *f; 1301 int fd; 1302 char *mtab_file = "/proc/mounts"; 1303 1304 if ((f = setmntent (mtab_file, "r")) == NULL) 1305 goto error; 1306 1307 if (stat(device, &st_buf) == 0) { 1308 if (S_ISBLK(st_buf.st_mode)) { 1309 file_rdev = st_buf.st_rdev; 1310 } else { 1311 file_dev = st_buf.st_dev; 1312 file_ino = st_buf.st_ino; 1313 } 1314 } 1315 1316 while ((mnt = getmntent (f)) != NULL) { 1317 if (strcmp(device, mnt->mnt_fsname) == 0) 1318 break; 1319 if (stat(mnt->mnt_fsname, &st_buf) == 0) { 1320 if (S_ISBLK(st_buf.st_mode)) { 1321 if (file_rdev && (file_rdev == st_buf.st_rdev)) 1322 break; 1323 } else { 1324 if (file_dev && ((file_dev == st_buf.st_dev) && 1325 (file_ino == st_buf.st_ino))) 1326 break; 1327 } 1328 } 1329 } 1330 1331 if (mnt == NULL) { 1332 /* 1333 * Do an extra check to see if this is the root device. We 1334 * can't trust /etc/mtab, and /proc/mounts will only list 1335 * /dev/root for the root filesystem. Argh. Instead we 1336 * check if the given device has the same major/minor number 1337 * as the device that the root directory is on. 1338 */ 1339 if (file_rdev && stat("/", &st_buf) == 0) { 1340 if (st_buf.st_dev == file_rdev) { 1341 goto is_root; 1342 } 1343 } 1344 goto test_busy; 1345 } 1346 /* Validate the entry in case /etc/mtab is out of date */ 1347 /* 1348 * We need to be paranoid, because some broken distributions 1349 * (read: Slackware) don't initialize /etc/mtab before checking 1350 * all of the non-root filesystems on the disk. 1351 */ 1352 if (stat(mnt->mnt_dir, &st_buf) < 0) { 1353 if (errno == ENOENT) { 1354 goto test_busy; 1355 } 1356 goto error; 1357 } 1358 if (file_rdev && (st_buf.st_dev != file_rdev)) { 1359 goto error; 1360 } 1361 1362 fprintf(stderr, "Device %s is mounted, exiting\n", device); 1363 exit(-1); 1364 1365 /* 1366 * Check to see if we're referring to the root filesystem. 1367 * If so, do a manual check to see if we can open /etc/mtab 1368 * read/write, since if the root is mounted read/only, the 1369 * contents of /etc/mtab may not be accurate. 1370 */ 1371 if (!strcmp(mnt->mnt_dir, "/")) { 1372 is_root: 1373 fprintf(stderr, "Device %s is mounted as root file system!\n", 1374 device); 1375 exit(-1); 1376 } 1377 1378 test_busy: 1379 1380 endmntent (f); 1381 if ((stat(device, &st_buf) != 0) || 1382 !S_ISBLK(st_buf.st_mode)) 1383 return; 1384 fd = open(device, O_RDONLY | O_EXCL); 1385 if (fd < 0) { 1386 if (errno == EBUSY) { 1387 fprintf(stderr, "Device %s is used by the system\n", device); 1388 exit(-1); 1389 } 1390 } else 1391 close(fd); 1392 1393 return; 1394 1395 error: 1396 endmntent (f); 1397 fprintf(stderr, "Error while checking if device %s is mounted\n", device); 1398 exit(-1); 1399 #else 1400 (void) device; /* shut up warnings about unused variable... */ 1401 #endif 1402 } 1403 1404 1405 time_t 1406 file_time(int f) 1407 { 1408 struct stat statbuf; 1409 1410 if (!fstat(f, &statbuf)) 1411 return current_time; 1412 if (statbuf.st_mtime<0 || statbuf.st_mtime>(uint32_t)(-1)) 1413 return current_time; 1414 return(statbuf.st_mtime); 1415 } 1416 1417 1418 __dead void 1419 pexit(char const * s, ...) 1420 { 1421 va_list va; 1422 1423 va_start(va, s); 1424 vwarn(s, va); 1425 va_end(va); 1426 if (lct != 0) 1427 warnx("Line %d being processed when error detected.\n", lct); 1428 exit(2); 1429 } 1430 1431 1432 void * 1433 alloc_block(void) 1434 { 1435 void *buf; 1436 1437 if(!(buf = malloc(block_size))) { 1438 err(1, "couldn't allocate filesystem buffer"); 1439 } 1440 memset(buf, 0, block_size); 1441 1442 return buf; 1443 } 1444 1445 void 1446 print_fs(void) 1447 { 1448 int i, j; 1449 ino_t k; 1450 struct inode *inode2; 1451 unsigned short *usbuf; 1452 block_t b; 1453 struct direct *dir; 1454 1455 assert(inodes_per_block * sizeof(*inode2) == block_size); 1456 if(!(inode2 = alloc_block())) 1457 err(1, "couldn't allocate a block of inodes"); 1458 1459 assert(NR_DIR_ENTRIES(block_size)*sizeof(*dir) == block_size); 1460 if(!(dir = alloc_block())) 1461 err(1, "couldn't allocate a block of directory entries"); 1462 1463 usbuf = alloc_block(); 1464 get_super_block(usbuf); 1465 printf("\nSuperblock: "); 1466 for (i = 0; i < 8; i++) printf("%06ho ", usbuf[i]); 1467 printf("\n "); 1468 for (i = 0; i < 8; i++) printf("%#04hX ", usbuf[i]); 1469 printf("\n "); 1470 for (i = 8; i < 15; i++) printf("%06ho ", usbuf[i]); 1471 printf("\n "); 1472 for (i = 8; i < 15; i++) printf("%#04hX ", usbuf[i]); 1473 get_block((block_t) INODE_MAP, usbuf); 1474 printf("...\nInode map: "); 1475 for (i = 0; i < 9; i++) printf("%06ho ", usbuf[i]); 1476 get_block((block_t) zone_map, usbuf); 1477 printf("...\nZone map: "); 1478 for (i = 0; i < 9; i++) printf("%06ho ", usbuf[i]); 1479 printf("...\n"); 1480 1481 free(usbuf); 1482 usbuf = NULL; 1483 1484 k = 0; 1485 for (b = inode_offset; k < nrinodes; b++) { 1486 get_block(b, inode2); 1487 for (i = 0; i < inodes_per_block; i++) { 1488 k = inodes_per_block * (int) (b - inode_offset) + i + 1; 1489 /* Lint but OK */ 1490 if (k > nrinodes) break; 1491 { 1492 if (inode2[i].i_mode != 0) { 1493 printf("Inode %3u: mode=", (unsigned)k); 1494 printf("%06o", (unsigned)inode2[i].i_mode); 1495 printf(" uid=%2d gid=%2d size=", 1496 (int)inode2[i].i_uid, (int)inode2[i].i_gid); 1497 printf("%6ld", (long)inode2[i].i_size); 1498 printf(" zone[0]=%u\n", (unsigned)inode2[i].i_zone[0]); 1499 } 1500 if ((inode2[i].i_mode & S_IFMT) == S_IFDIR) { 1501 /* This is a directory */ 1502 get_block(inode2[i].i_zone[0] << zone_shift, dir); 1503 for (j = 0; j < NR_DIR_ENTRIES(block_size); j++) 1504 if (dir[j].d_ino) 1505 printf("\tInode %2u: %s\n", 1506 (unsigned)dir[j].d_ino, 1507 dir[j].d_name); 1508 } 1509 } 1510 } 1511 } 1512 1513 if (zone_shift) 1514 printf("%d inodes used. %u zones (%u blocks) used.\n", 1515 (int)next_inode-1, next_zone, next_zone*zone_per_block); 1516 else 1517 printf("%d inodes used. %u zones used.\n", (int)next_inode-1, next_zone); 1518 free(dir); 1519 free(inode2); 1520 } 1521 1522 1523 /* 1524 * The first time a block is read, it returns all 0s, unless there has 1525 * been a write. This routine checks to see if a block has been accessed. 1526 */ 1527 int 1528 read_and_set(block_t n) 1529 { 1530 int w, s, mask, r; 1531 1532 w = n / 8; 1533 1534 assert(n < nrblocks); 1535 if(w >= umap_array_elements) { 1536 errx(1, "umap array too small - this can't happen"); 1537 } 1538 s = n % 8; 1539 mask = 1 << s; 1540 r = (umap_array[w] & mask ? 1 : 0); 1541 umap_array[w] |= mask; 1542 return(r); 1543 } 1544 1545 __dead void 1546 usage(void) 1547 { 1548 fprintf(stderr, "Usage: %s [-dltv] [-b blocks] [-i inodes]\n" 1549 "\t[-z zone_shift] [-I offset] [-x extra] [-B blocksize] special [proto]\n", 1550 progname); 1551 exit(4); 1552 } 1553 1554 void 1555 special(char * string, int insertmode) 1556 { 1557 int openmode = O_RDWR; 1558 if(!insertmode) openmode |= O_TRUNC; 1559 fd = open(string, O_RDWR | O_CREAT, 0644); 1560 if (fd < 0) err(1, "Can't open special file %s", string); 1561 mkfs_seek(0, SEEK_SET); 1562 } 1563 1564 1565 1566 /* Read a block. */ 1567 void 1568 get_block(block_t n, void *buf) 1569 { 1570 ssize_t k; 1571 1572 /* First access returns a zero block */ 1573 if (read_and_set(n) == 0) { 1574 memcpy(buf, zero, block_size); 1575 return; 1576 } 1577 mkfs_seek((uint64_t)(n) * block_size, SEEK_SET); 1578 k = read(fd, buf, block_size); 1579 if (k != block_size) 1580 pexit("get_block couldn't read block #%u", (unsigned)n); 1581 } 1582 1583 /* Read the super block. */ 1584 void 1585 get_super_block(void *buf) 1586 { 1587 ssize_t k; 1588 1589 mkfs_seek((off_t) SUPER_BLOCK_BYTES, SEEK_SET); 1590 k = read(fd, buf, SUPER_BLOCK_BYTES); 1591 if (k != SUPER_BLOCK_BYTES) 1592 err(1, "get_super_block couldn't read super block"); 1593 } 1594 1595 /* Write a block. */ 1596 void 1597 put_block(block_t n, void *buf) 1598 { 1599 1600 (void) read_and_set(n); 1601 1602 mkfs_seek((uint64_t)(n) * block_size, SEEK_SET); 1603 mkfs_write(buf, block_size); 1604 } 1605 1606 static ssize_t 1607 mkfs_write(void * buf, size_t count) 1608 { 1609 uint64_t fssize; 1610 ssize_t w; 1611 1612 /* Perform & check write */ 1613 w = write(fd, buf, count); 1614 if(w < 0) 1615 err(1, "mkfs_write: write failed"); 1616 if(w != count) 1617 errx(1, "mkfs_write: short write: %ld != %ld", w, count); 1618 1619 /* Check if this has made the FS any bigger; count bytes after offset */ 1620 fssize = mkfs_seek(0, SEEK_CUR); 1621 1622 assert(fssize >= fs_offset_bytes); 1623 fssize -= fs_offset_bytes; 1624 fssize = roundup(fssize, block_size); 1625 if(fssize > written_fs_size) 1626 written_fs_size = fssize; 1627 1628 return w; 1629 } 1630 1631 /* Seek to position in FS we're creating. */ 1632 static uint64_t 1633 mkfs_seek(uint64_t pos, int whence) 1634 { 1635 if(whence == SEEK_SET) pos += fs_offset_bytes; 1636 off_t newpos; 1637 if((newpos=lseek(fd, pos, whence)) == (off_t) -1) 1638 err(1, "mkfs_seek: lseek failed"); 1639 return newpos; 1640 } 1641