1 /* 2 * Copyright (c) 1983, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)newfs.c 8.5 (Berkeley) 02/05/94"; 10 #endif /* not lint */ 11 12 #ifndef lint 13 static char copyright[] = 14 "@(#) Copyright (c) 1983, 1989, 1993\n\ 15 The Regents of the University of California. All rights reserved.\n"; 16 #endif /* not lint */ 17 18 /* 19 * newfs: friendly front end to mkfs 20 */ 21 #include <sys/param.h> 22 #include <sys/stat.h> 23 #include <sys/ioctl.h> 24 #include <sys/disklabel.h> 25 #include <sys/file.h> 26 #include <sys/mount.h> 27 #include <ufs/ufs/dir.h> 28 #include <ufs/ffs/fs.h> 29 30 #include <errno.h> 31 #if __STDC__ 32 #include <stdarg.h> 33 #else 34 #include <varargs.h> 35 #endif 36 #include <unistd.h> 37 #include <stdio.h> 38 #include <ctype.h> 39 #include <string.h> 40 #include <stdlib.h> 41 #include <syslog.h> 42 #include <paths.h> 43 44 #if __STDC__ 45 void fatal(const char *fmt, ...); 46 #else 47 void fatal(); 48 #endif 49 50 #define COMPAT /* allow non-labeled disks */ 51 52 /* 53 * The following two constants set the default block and fragment sizes. 54 * Both constants must be a power of 2 and meet the following constraints: 55 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE 56 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE 57 * DESBLKSIZE / DESFRAGSIZE <= 8 58 */ 59 #define DFL_FRAGSIZE 1024 60 #define DFL_BLKSIZE 8192 61 62 /* 63 * Cylinder groups may have up to many cylinders. The actual 64 * number used depends upon how much information can be stored 65 * on a single cylinder. The default is to use 16 cylinders 66 * per group. 67 */ 68 #define DESCPG 16 /* desired fs_cpg */ 69 70 /* 71 * MINFREE gives the minimum acceptable percentage of file system 72 * blocks which may be free. If the freelist drops below this level 73 * only the superuser may continue to allocate blocks. This may 74 * be set to 0 if no reserve of free blocks is deemed necessary, 75 * however throughput drops by fifty percent if the file system 76 * is run at between 95% and 100% full; thus the default value of 77 * fs_minfree is 5%. With 5% free space, fragmentation is not a 78 * problem, so we choose to optimize for time. 79 */ 80 #define MINFREE 5 81 #define DEFAULTOPT FS_OPTTIME 82 83 /* 84 * ROTDELAY gives the minimum number of milliseconds to initiate 85 * another disk transfer on the same cylinder. It is used in 86 * determining the rotationally optimal layout for disk blocks 87 * within a file; the default of fs_rotdelay is 4ms. 88 */ 89 #define ROTDELAY 4 90 91 /* 92 * MAXBLKPG determines the maximum number of data blocks which are 93 * placed in a single cylinder group. The default is one indirect 94 * block worth of data blocks. 95 */ 96 #define MAXBLKPG(bsize) ((bsize) / sizeof(daddr_t)) 97 98 /* 99 * Each file system has a number of inodes statically allocated. 100 * We allocate one inode slot per NFPI fragments, expecting this 101 * to be far more than we will ever need. 102 */ 103 #define NFPI 4 104 105 /* 106 * For each cylinder we keep track of the availability of blocks at different 107 * rotational positions, so that we can lay out the data to be picked 108 * up with minimum rotational latency. NRPOS is the default number of 109 * rotational positions that we distinguish. With NRPOS of 8 the resolution 110 * of our summary information is 2ms for a typical 3600 rpm drive. 111 */ 112 #define NRPOS 8 /* number distinct rotational positions */ 113 114 115 int mfs; /* run as the memory based filesystem */ 116 int Nflag; /* run without writing file system */ 117 int Oflag; /* format as an 4.3BSD file system */ 118 int fssize; /* file system size */ 119 int ntracks; /* # tracks/cylinder */ 120 int nsectors; /* # sectors/track */ 121 int nphyssectors; /* # sectors/track including spares */ 122 int secpercyl; /* sectors per cylinder */ 123 int trackspares = -1; /* spare sectors per track */ 124 int cylspares = -1; /* spare sectors per cylinder */ 125 int sectorsize; /* bytes/sector */ 126 #ifdef tahoe 127 int realsectorsize; /* bytes/sector in hardware */ 128 #endif 129 int rpm; /* revolutions/minute of drive */ 130 int interleave; /* hardware sector interleave */ 131 int trackskew = -1; /* sector 0 skew, per track */ 132 int headswitch; /* head switch time, usec */ 133 int trackseek; /* track-to-track seek, usec */ 134 int fsize = 0; /* fragment size */ 135 int bsize = 0; /* block size */ 136 int cpg = DESCPG; /* cylinders/cylinder group */ 137 int cpgflg; /* cylinders/cylinder group flag was given */ 138 int minfree = MINFREE; /* free space threshold */ 139 int opt = DEFAULTOPT; /* optimization preference (space or time) */ 140 int density; /* number of bytes per inode */ 141 int maxcontig = 0; /* max contiguous blocks to allocate */ 142 int rotdelay = ROTDELAY; /* rotational delay between blocks */ 143 int maxbpg; /* maximum blocks per file in a cyl group */ 144 int nrpos = NRPOS; /* # of distinguished rotational positions */ 145 int bbsize = BBSIZE; /* boot block size */ 146 int sbsize = SBSIZE; /* superblock size */ 147 int mntflags; /* flags to be passed to mount */ 148 u_long memleft; /* virtual memory available */ 149 caddr_t membase; /* start address of memory based filesystem */ 150 #ifdef COMPAT 151 char *disktype; 152 int unlabeled; 153 #endif 154 155 char device[MAXPATHLEN]; 156 char *progname; 157 158 main(argc, argv) 159 int argc; 160 char *argv[]; 161 { 162 extern char *optarg; 163 extern int optind; 164 register int ch; 165 register struct partition *pp; 166 register struct disklabel *lp; 167 struct disklabel *getdisklabel(); 168 struct partition oldpartition; 169 struct stat st; 170 int fsi, fso; 171 char *cp, *special, *opstring, buf[BUFSIZ]; 172 struct statfs *mp; 173 char *s1, *s2; 174 int len, n; 175 176 if (progname = rindex(*argv, '/')) 177 ++progname; 178 else 179 progname = *argv; 180 181 if (strstr(progname, "mfs")) { 182 mfs = 1; 183 Nflag++; 184 } 185 186 opstring = "F:NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:"; 187 if (!mfs) 188 opstring += 2; /* -F is mfs only */ 189 190 while ((ch = getopt(argc, argv, opstring)) != EOF) 191 switch(ch) { 192 case 'F': 193 if ((mntflags = atoi(optarg)) == 0) 194 fatal("%s: bad mount flags", optarg); 195 break; 196 case 'N': 197 Nflag++; 198 break; 199 case 'O': 200 Oflag++; 201 break; 202 case 'S': 203 if ((sectorsize = atoi(optarg)) <= 0) 204 fatal("%s: bad sector size", optarg); 205 break; 206 #ifdef COMPAT 207 case 'T': 208 disktype = optarg; 209 break; 210 #endif 211 case 'a': 212 if ((maxcontig = atoi(optarg)) <= 0) 213 fatal("%s: bad max contiguous blocks\n", 214 optarg); 215 break; 216 case 'b': 217 if ((bsize = atoi(optarg)) < MINBSIZE) 218 fatal("%s: bad block size", optarg); 219 break; 220 case 'c': 221 if ((cpg = atoi(optarg)) <= 0) 222 fatal("%s: bad cylinders/group", optarg); 223 cpgflg++; 224 break; 225 case 'd': 226 if ((rotdelay = atoi(optarg)) < 0) 227 fatal("%s: bad rotational delay\n", optarg); 228 break; 229 case 'e': 230 if ((maxbpg = atoi(optarg)) <= 0) 231 fatal("%s: bad blocks per file in a cyl group\n", 232 optarg); 233 break; 234 case 'f': 235 if ((fsize = atoi(optarg)) <= 0) 236 fatal("%s: bad frag size", optarg); 237 break; 238 case 'i': 239 if ((density = atoi(optarg)) <= 0) 240 fatal("%s: bad bytes per inode\n", optarg); 241 break; 242 case 'k': 243 if ((trackskew = atoi(optarg)) < 0) 244 fatal("%s: bad track skew", optarg); 245 break; 246 case 'l': 247 if ((interleave = atoi(optarg)) <= 0) 248 fatal("%s: bad interleave", optarg); 249 break; 250 case 'm': 251 if ((minfree = atoi(optarg)) < 0 || minfree > 99) 252 fatal("%s: bad free space %%\n", optarg); 253 break; 254 case 'n': 255 if ((nrpos = atoi(optarg)) <= 0) 256 fatal("%s: bad rotational layout count\n", 257 optarg); 258 break; 259 case 'o': 260 if (strcmp(optarg, "space") == 0) 261 opt = FS_OPTSPACE; 262 else if (strcmp(optarg, "time") == 0) 263 opt = FS_OPTTIME; 264 else 265 fatal("%s: bad optimization preference %s", 266 optarg, "(options are `space' or `time')"); 267 break; 268 case 'p': 269 if ((trackspares = atoi(optarg)) < 0) 270 fatal("%s: bad spare sectors per track", 271 optarg); 272 break; 273 case 'r': 274 if ((rpm = atoi(optarg)) <= 0) 275 fatal("%s: bad revs/minute\n", optarg); 276 break; 277 case 's': 278 if ((fssize = atoi(optarg)) <= 0) 279 fatal("%s: bad file system size", optarg); 280 break; 281 case 't': 282 if ((ntracks = atoi(optarg)) <= 0) 283 fatal("%s: bad total tracks", optarg); 284 break; 285 case 'u': 286 if ((nsectors = atoi(optarg)) <= 0) 287 fatal("%s: bad sectors/track", optarg); 288 break; 289 case 'x': 290 if ((cylspares = atoi(optarg)) < 0) 291 fatal("%s: bad spare sectors per cylinder", 292 optarg); 293 break; 294 case '?': 295 default: 296 usage(); 297 } 298 argc -= optind; 299 argv += optind; 300 301 if (argc != 2 && (mfs || argc != 1)) 302 usage(); 303 304 special = argv[0]; 305 cp = rindex(special, '/'); 306 if (cp == 0) { 307 /* 308 * No path prefix; try /dev/r%s then /dev/%s. 309 */ 310 (void)sprintf(device, "%sr%s", _PATH_DEV, special); 311 if (stat(device, &st) == -1) 312 (void)sprintf(device, "%s%s", _PATH_DEV, special); 313 special = device; 314 } 315 if (Nflag) { 316 fso = -1; 317 } else { 318 fso = open(special, O_WRONLY); 319 if (fso < 0) 320 fatal("%s: %s", special, strerror(errno)); 321 322 /* Bail if target special is mounted */ 323 n = getmntinfo(&mp, MNT_NOWAIT); 324 if (n == 0) 325 fatal("%s: getmntinfo: %s", special, strerror(errno)); 326 327 len = sizeof(_PATH_DEV) - 1; 328 s1 = special; 329 if (strncmp(_PATH_DEV, s1, len) == 0) 330 s1 += len; 331 332 while (--n >= 0) { 333 s2 = mp->f_mntfromname; 334 if (strncmp(_PATH_DEV, s2, len) == 0) { 335 s2 += len - 1; 336 *s2 = 'r'; 337 } 338 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) 339 fatal("%s is mounted on %s", 340 special, mp->f_mntonname); 341 ++mp; 342 } 343 } 344 if (mfs && disktype != NULL) { 345 lp = (struct disklabel *)getdiskbyname(disktype); 346 if (lp == NULL) 347 fatal("%s: unknown disk type", disktype); 348 pp = &lp->d_partitions[1]; 349 } else { 350 fsi = open(special, O_RDONLY); 351 if (fsi < 0) 352 fatal("%s: %s", special, strerror(errno)); 353 if (fstat(fsi, &st) < 0) 354 fatal("%s: %s", special, strerror(errno)); 355 if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs) 356 printf("%s: %s: not a character-special device\n", 357 progname, special); 358 cp = index(argv[0], '\0') - 1; 359 if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp)) 360 fatal("%s: can't figure out file system partition", 361 argv[0]); 362 #ifdef COMPAT 363 if (!mfs && disktype == NULL) 364 disktype = argv[1]; 365 #endif 366 lp = getdisklabel(special, fsi); 367 if (isdigit(*cp)) 368 pp = &lp->d_partitions[0]; 369 else 370 pp = &lp->d_partitions[*cp - 'a']; 371 if (pp->p_size == 0) 372 fatal("%s: `%c' partition is unavailable", 373 argv[0], *cp); 374 if (pp->p_fstype == FS_BOOT) 375 fatal("%s: `%c' partition overlaps boot program", 376 argv[0], *cp); 377 } 378 if (fssize == 0) 379 fssize = pp->p_size; 380 if (fssize > pp->p_size && !mfs) 381 fatal("%s: maximum file system size on the `%c' partition is %d", 382 argv[0], *cp, pp->p_size); 383 if (rpm == 0) { 384 rpm = lp->d_rpm; 385 if (rpm <= 0) 386 rpm = 3600; 387 } 388 if (ntracks == 0) { 389 ntracks = lp->d_ntracks; 390 if (ntracks <= 0) 391 fatal("%s: no default #tracks", argv[0]); 392 } 393 if (nsectors == 0) { 394 nsectors = lp->d_nsectors; 395 if (nsectors <= 0) 396 fatal("%s: no default #sectors/track", argv[0]); 397 } 398 if (sectorsize == 0) { 399 sectorsize = lp->d_secsize; 400 if (sectorsize <= 0) 401 fatal("%s: no default sector size", argv[0]); 402 } 403 if (trackskew == -1) { 404 trackskew = lp->d_trackskew; 405 if (trackskew < 0) 406 trackskew = 0; 407 } 408 if (interleave == 0) { 409 interleave = lp->d_interleave; 410 if (interleave <= 0) 411 interleave = 1; 412 } 413 if (fsize == 0) { 414 fsize = pp->p_fsize; 415 if (fsize <= 0) 416 fsize = MAX(DFL_FRAGSIZE, lp->d_secsize); 417 } 418 if (bsize == 0) { 419 bsize = pp->p_frag * pp->p_fsize; 420 if (bsize <= 0) 421 bsize = MIN(DFL_BLKSIZE, 8 * fsize); 422 } 423 /* 424 * Maxcontig sets the default for the maximum number of blocks 425 * that may be allocated sequentially. With filesystem clustering 426 * it is possible to allocate contiguous blocks up to the maximum 427 * transfer size permitted by the controller or buffering. 428 */ 429 if (maxcontig == 0) 430 maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize - 1); 431 if (density == 0) 432 density = NFPI * fsize; 433 if (minfree < MINFREE && opt != FS_OPTSPACE) { 434 fprintf(stderr, "Warning: changing optimization to space "); 435 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE); 436 opt = FS_OPTSPACE; 437 } 438 if (trackspares == -1) { 439 trackspares = lp->d_sparespertrack; 440 if (trackspares < 0) 441 trackspares = 0; 442 } 443 nphyssectors = nsectors + trackspares; 444 if (cylspares == -1) { 445 cylspares = lp->d_sparespercyl; 446 if (cylspares < 0) 447 cylspares = 0; 448 } 449 secpercyl = nsectors * ntracks - cylspares; 450 if (secpercyl != lp->d_secpercyl) 451 fprintf(stderr, "%s (%d) %s (%lu)\n", 452 "Warning: calculated sectors per cylinder", secpercyl, 453 "disagrees with disk label", lp->d_secpercyl); 454 if (maxbpg == 0) 455 maxbpg = MAXBLKPG(bsize); 456 headswitch = lp->d_headswitch; 457 trackseek = lp->d_trkseek; 458 #ifdef notdef /* label may be 0 if faked up by kernel */ 459 bbsize = lp->d_bbsize; 460 sbsize = lp->d_sbsize; 461 #endif 462 oldpartition = *pp; 463 #ifdef tahoe 464 realsectorsize = sectorsize; 465 if (sectorsize != DEV_BSIZE) { /* XXX */ 466 int secperblk = DEV_BSIZE / sectorsize; 467 468 sectorsize = DEV_BSIZE; 469 nsectors /= secperblk; 470 nphyssectors /= secperblk; 471 secpercyl /= secperblk; 472 fssize /= secperblk; 473 pp->p_size /= secperblk; 474 } 475 #endif 476 mkfs(pp, special, fsi, fso); 477 #ifdef tahoe 478 if (realsectorsize != DEV_BSIZE) 479 pp->p_size *= DEV_BSIZE / realsectorsize; 480 #endif 481 if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition))) 482 rewritelabel(special, fso, lp); 483 if (!Nflag) 484 close(fso); 485 close(fsi); 486 #ifdef MFS 487 if (mfs) { 488 struct mfs_args args; 489 490 sprintf(buf, "mfs:%d", getpid()); 491 args.fspec = buf; 492 args.export.ex_root = -2; 493 if (mntflags & MNT_RDONLY) 494 args.export.ex_flags = MNT_EXRDONLY; 495 else 496 args.export.ex_flags = 0; 497 args.base = membase; 498 args.size = fssize * sectorsize; 499 if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0) 500 fatal("%s: %s", argv[1], strerror(errno)); 501 } 502 #endif 503 exit(0); 504 } 505 506 #ifdef COMPAT 507 char lmsg[] = "%s: can't read disk label; disk type must be specified"; 508 #else 509 char lmsg[] = "%s: can't read disk label"; 510 #endif 511 512 struct disklabel * 513 getdisklabel(s, fd) 514 char *s; 515 int fd; 516 { 517 static struct disklabel lab; 518 519 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 520 #ifdef COMPAT 521 if (disktype) { 522 struct disklabel *lp, *getdiskbyname(); 523 524 unlabeled++; 525 lp = getdiskbyname(disktype); 526 if (lp == NULL) 527 fatal("%s: unknown disk type", disktype); 528 return (lp); 529 } 530 #endif 531 warn("ioctl (GDINFO)"); 532 fatal(lmsg, s); 533 } 534 return (&lab); 535 } 536 537 rewritelabel(s, fd, lp) 538 char *s; 539 int fd; 540 register struct disklabel *lp; 541 { 542 #ifdef COMPAT 543 if (unlabeled) 544 return; 545 #endif 546 lp->d_checksum = 0; 547 lp->d_checksum = dkcksum(lp); 548 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) { 549 warn("ioctl (WDINFO)"); 550 fatal("%s: can't rewrite disk label", s); 551 } 552 #if vax 553 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) { 554 register i; 555 int cfd; 556 daddr_t alt; 557 char specname[64]; 558 char blk[1024]; 559 char *cp; 560 561 /* 562 * Make name for 'c' partition. 563 */ 564 strcpy(specname, s); 565 cp = specname + strlen(specname) - 1; 566 if (!isdigit(*cp)) 567 *cp = 'c'; 568 cfd = open(specname, O_WRONLY); 569 if (cfd < 0) 570 fatal("%s: %s", specname, strerror(errno)); 571 bzero(blk, sizeof(blk)); 572 *(struct disklabel *)(blk + LABELOFFSET) = *lp; 573 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors; 574 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) { 575 if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize, 576 L_SET) == -1) 577 fatal("lseek to badsector area: %s", 578 strerror(errno)); 579 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize) 580 warn("alternate label %d write", i/2); 581 } 582 close(cfd); 583 } 584 #endif 585 } 586 587 /*VARARGS*/ 588 void 589 #if __STDC__ 590 fatal(const char *fmt, ...) 591 #else 592 fatal(fmt, va_alist) 593 char *fmt; 594 va_dcl 595 #endif 596 { 597 va_list ap; 598 599 #if __STDC__ 600 va_start(ap, fmt); 601 #else 602 va_start(ap); 603 #endif 604 if (fcntl(STDERR_FILENO, F_GETFL) < 0) { 605 openlog(progname, LOG_CONS, LOG_DAEMON); 606 vsyslog(LOG_ERR, fmt, ap); 607 closelog(); 608 } else { 609 vwarnx(fmt, ap); 610 } 611 va_end(ap); 612 exit(1); 613 /*NOTREACHED*/ 614 } 615 616 usage() 617 { 618 if (mfs) { 619 fprintf(stderr, 620 "usage: %s [ -fsoptions ] special-device mount-point\n", 621 progname); 622 } else 623 fprintf(stderr, 624 "usage: %s [ -fsoptions ] special-device%s\n", 625 progname, 626 #ifdef COMPAT 627 " [device-type]"); 628 #else 629 ""); 630 #endif 631 fprintf(stderr, "where fsoptions are:\n"); 632 fprintf(stderr, 633 "\t-N do not create file system, just print out parameters\n"); 634 fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n"); 635 fprintf(stderr, "\t-S sector size\n"); 636 #ifdef COMPAT 637 fprintf(stderr, "\t-T disktype\n"); 638 #endif 639 fprintf(stderr, "\t-a maximum contiguous blocks\n"); 640 fprintf(stderr, "\t-b block size\n"); 641 fprintf(stderr, "\t-c cylinders/group\n"); 642 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n"); 643 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 644 fprintf(stderr, "\t-f frag size\n"); 645 fprintf(stderr, "\t-i number of bytes per inode\n"); 646 fprintf(stderr, "\t-k sector 0 skew, per track\n"); 647 fprintf(stderr, "\t-l hardware sector interleave\n"); 648 fprintf(stderr, "\t-m minimum free space %%\n"); 649 fprintf(stderr, "\t-n number of distinguished rotational positions\n"); 650 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 651 fprintf(stderr, "\t-p spare sectors per track\n"); 652 fprintf(stderr, "\t-s file system size (sectors)\n"); 653 fprintf(stderr, "\t-r revolutions/minute\n"); 654 fprintf(stderr, "\t-t tracks/cylinder\n"); 655 fprintf(stderr, "\t-u sectors/track\n"); 656 fprintf(stderr, "\t-x spare sectors per cylinder\n"); 657 exit(1); 658 } 659