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