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