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