1 /* 2 * Copyright (c) 1983, 1989, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static char sccsid[] = "@(#)newfs.c 8.8 (Berkeley) 4/18/94"; 36 #endif /* not lint */ 37 38 #ifndef lint 39 static char copyright[] = 40 "@(#) Copyright (c) 1983, 1989, 1993, 1994\n\ 41 The Regents of the University of California. All rights reserved.\n"; 42 #endif /* not lint */ 43 44 /* 45 * newfs: friendly front end to mkfs 46 */ 47 #include <sys/param.h> 48 #include <sys/stat.h> 49 #include <sys/ioctl.h> 50 #include <sys/disklabel.h> 51 #include <sys/file.h> 52 #include <sys/mount.h> 53 54 #include <ufs/ufs/dir.h> 55 #include <ufs/ffs/fs.h> 56 57 #include <ctype.h> 58 #include <errno.h> 59 #include <paths.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <syslog.h> 64 #include <unistd.h> 65 66 #if __STDC__ 67 #include <stdarg.h> 68 #else 69 #include <varargs.h> 70 #endif 71 72 #include "mntopts.h" 73 74 struct mntopt mopts[] = { 75 MOPT_STDOPTS, 76 MOPT_ASYNC, 77 { NULL }, 78 }; 79 80 #if __STDC__ 81 void fatal(const char *fmt, ...); 82 #else 83 void fatal(); 84 #endif 85 86 #define COMPAT /* allow non-labeled disks */ 87 88 /* 89 * The following two constants set the default block and fragment sizes. 90 * Both constants must be a power of 2 and meet the following constraints: 91 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE 92 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE 93 * DESBLKSIZE / DESFRAGSIZE <= 8 94 */ 95 #define DFL_FRAGSIZE 1024 96 #define DFL_BLKSIZE 8192 97 98 /* 99 * Cylinder groups may have up to many cylinders. The actual 100 * number used depends upon how much information can be stored 101 * on a single cylinder. The default is to use 16 cylinders 102 * per group. 103 */ 104 #define DESCPG 16 /* desired fs_cpg */ 105 106 /* 107 * Once upon a time... 108 * ROTDELAY gives the minimum number of milliseconds to initiate 109 * another disk transfer on the same cylinder. It is used in 110 * determining the rotationally optimal layout for disk blocks 111 * within a file; the default of fs_rotdelay is 4ms. 112 * 113 * ...but now we make this 0 to disable the rotdelay delay because 114 * modern drives with read/write-behind achieve higher performance 115 * without the delay. 116 */ 117 #define ROTDELAY 0 118 119 /* 120 * MAXBLKPG determines the maximum number of data blocks which are 121 * placed in a single cylinder group. The default is one indirect 122 * block worth of data blocks. 123 */ 124 #define MAXBLKPG(bsize) ((bsize) / sizeof(daddr_t)) 125 126 /* 127 * Each file system has a number of inodes statically allocated. 128 * We allocate one inode slot per NFPI fragments, expecting this 129 * to be far more than we will ever need. 130 */ 131 #define NFPI 4 132 133 /* 134 * Once upon a time... 135 * For each cylinder we keep track of the availability of blocks at different 136 * rotational positions, so that we can lay out the data to be picked 137 * up with minimum rotational latency. NRPOS is the default number of 138 * rotational positions that we distinguish. With NRPOS of 8 the resolution 139 * of our summary information is 2ms for a typical 3600 rpm drive. 140 * 141 * ...but now we make this 1 (which escentially disables the rotational 142 * position table because modern drives with read-ahead and write-behind do 143 * better without the rotational position table. 144 */ 145 #define NRPOS 1 /* number distinct rotational positions */ 146 147 /* 148 * About the same time as the above, we knew what went where on the disks. 149 * no longer so, so kill the code which finds the different platters too... 150 * We do this by saying one head, with a lot of sectors on it. 151 * The number of sectors are used to determine the size of a cyl-group. 152 * Kirk suggested one or two meg per "cylinder" so we say two. 153 */ 154 155 #define NTRACKS 1 /* number of heads */ 156 157 #define NSECTORS 4096 /* number of sectors */ 158 159 int mfs; /* run as the memory based filesystem */ 160 int Nflag; /* run without writing file system */ 161 int Oflag; /* format as an 4.3BSD file system */ 162 int fssize; /* file system size */ 163 int ntracks = NTRACKS; /* # tracks/cylinder */ 164 int nsectors = NSECTORS; /* # sectors/track */ 165 int nphyssectors; /* # sectors/track including spares */ 166 int secpercyl; /* sectors per cylinder */ 167 int trackspares = -1; /* spare sectors per track */ 168 int cylspares = -1; /* spare sectors per cylinder */ 169 int sectorsize; /* bytes/sector */ 170 #ifdef tahoe 171 int realsectorsize; /* bytes/sector in hardware */ 172 #endif 173 int rpm; /* revolutions/minute of drive */ 174 int interleave; /* hardware sector interleave */ 175 int trackskew = -1; /* sector 0 skew, per track */ 176 int headswitch; /* head switch time, usec */ 177 int trackseek; /* track-to-track seek, usec */ 178 int fsize = 0; /* fragment size */ 179 int bsize = 0; /* block size */ 180 int cpg = DESCPG; /* cylinders/cylinder group */ 181 int cpgflg; /* cylinders/cylinder group flag was given */ 182 int minfree = MINFREE; /* free space threshold */ 183 int opt = DEFAULTOPT; /* optimization preference (space or time) */ 184 int density; /* number of bytes per inode */ 185 int maxcontig = 0; /* max contiguous blocks to allocate */ 186 int rotdelay = ROTDELAY; /* rotational delay between blocks */ 187 int maxbpg; /* maximum blocks per file in a cyl group */ 188 int nrpos = NRPOS; /* # of distinguished rotational positions */ 189 int bbsize = BBSIZE; /* boot block size */ 190 int sbsize = SBSIZE; /* superblock size */ 191 int mntflags = MNT_ASYNC; /* flags to be passed to mount */ 192 int t_or_u_flag = 0; /* user has specified -t or -u */ 193 u_long memleft; /* virtual memory available */ 194 caddr_t membase; /* start address of memory based filesystem */ 195 char *filename; 196 #ifdef COMPAT 197 char *disktype; 198 int unlabeled; 199 #endif 200 201 char device[MAXPATHLEN]; 202 char *progname; 203 204 int 205 main(argc, argv) 206 int argc; 207 char *argv[]; 208 { 209 extern char *optarg; 210 extern int optind; 211 register int ch; 212 register struct partition *pp; 213 register struct disklabel *lp; 214 struct disklabel *getdisklabel(); 215 struct partition oldpartition; 216 struct stat st; 217 struct statfs *mp; 218 int fsi, fso, len, n; 219 char *cp, *s1, *s2, *special, *opstring, buf[BUFSIZ]; 220 221 if (progname = rindex(*argv, '/')) 222 ++progname; 223 else 224 progname = *argv; 225 226 if (strstr(progname, "mfs")) { 227 mfs = 1; 228 Nflag++; 229 } 230 231 opstring = mfs ? 232 "NF:T:a:b:c:d:e:f:i:m:o:s:" : 233 "NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:"; 234 while ((ch = getopt(argc, argv, opstring)) != EOF) 235 switch (ch) { 236 case 'N': 237 Nflag = 1; 238 break; 239 case 'O': 240 Oflag = 1; 241 break; 242 case 'S': 243 if ((sectorsize = atoi(optarg)) <= 0) 244 fatal("%s: bad sector size", optarg); 245 break; 246 #ifdef COMPAT 247 case 'T': 248 disktype = optarg; 249 break; 250 #endif 251 case 'F': 252 filename = optarg; 253 break; 254 case 'a': 255 if ((maxcontig = atoi(optarg)) <= 0) 256 fatal("%s: bad maximum contiguous blocks\n", 257 optarg); 258 break; 259 case 'b': 260 if ((bsize = atoi(optarg)) < MINBSIZE) 261 fatal("%s: bad block size", optarg); 262 break; 263 case 'c': 264 if ((cpg = atoi(optarg)) <= 0) 265 fatal("%s: bad cylinders/group", optarg); 266 cpgflg++; 267 break; 268 case 'd': 269 if ((rotdelay = atoi(optarg)) < 0) 270 fatal("%s: bad rotational delay\n", optarg); 271 break; 272 case 'e': 273 if ((maxbpg = atoi(optarg)) <= 0) 274 fatal("%s: bad blocks per file in a cylinder group\n", 275 optarg); 276 break; 277 case 'f': 278 if ((fsize = atoi(optarg)) <= 0) 279 fatal("%s: bad fragment size", optarg); 280 break; 281 case 'i': 282 if ((density = atoi(optarg)) <= 0) 283 fatal("%s: bad bytes per inode\n", optarg); 284 break; 285 case 'k': 286 if ((trackskew = atoi(optarg)) < 0) 287 fatal("%s: bad track skew", optarg); 288 break; 289 case 'l': 290 if ((interleave = atoi(optarg)) <= 0) 291 fatal("%s: bad interleave", optarg); 292 break; 293 case 'm': 294 if ((minfree = atoi(optarg)) < 0 || minfree > 99) 295 fatal("%s: bad free space %%\n", optarg); 296 break; 297 case 'n': 298 if ((nrpos = atoi(optarg)) < 0) 299 fatal("%s: bad rotational layout count\n", 300 optarg); 301 if (nrpos == 0) 302 nrpos = 1; 303 break; 304 case 'o': 305 if (mfs) 306 getmntopts(optarg, mopts, &mntflags, 0); 307 else { 308 if (strcmp(optarg, "space") == 0) 309 opt = FS_OPTSPACE; 310 else if (strcmp(optarg, "time") == 0) 311 opt = FS_OPTTIME; 312 else 313 fatal("%s: unknown optimization preference: use `space' or `time'."); 314 } 315 break; 316 case 'p': 317 if ((trackspares = atoi(optarg)) < 0) 318 fatal("%s: bad spare sectors per track", 319 optarg); 320 break; 321 case 'r': 322 if ((rpm = atoi(optarg)) <= 0) 323 fatal("%s: bad revolutions/minute\n", optarg); 324 break; 325 case 's': 326 if ((fssize = atoi(optarg)) <= 0) 327 fatal("%s: bad file system size", optarg); 328 break; 329 case 't': 330 t_or_u_flag++; 331 if ((ntracks = atoi(optarg)) < 0) 332 fatal("%s: bad total tracks", optarg); 333 break; 334 case 'u': 335 t_or_u_flag++; 336 if ((nsectors = atoi(optarg)) < 0) 337 fatal("%s: bad sectors/track", optarg); 338 break; 339 case 'x': 340 if ((cylspares = atoi(optarg)) < 0) 341 fatal("%s: bad spare sectors per cylinder", 342 optarg); 343 break; 344 case '?': 345 default: 346 usage(); 347 } 348 argc -= optind; 349 argv += optind; 350 351 if (argc != 2 && (mfs || argc != 1)) 352 usage(); 353 354 special = argv[0]; 355 cp = rindex(special, '/'); 356 if (cp == 0) { 357 /* 358 * No path prefix; try /dev/r%s then /dev/%s. 359 */ 360 (void)sprintf(device, "%sr%s", _PATH_DEV, special); 361 if (stat(device, &st) == -1) 362 (void)sprintf(device, "%s%s", _PATH_DEV, special); 363 special = device; 364 } 365 if (Nflag) { 366 fso = -1; 367 } else { 368 fso = open(special, O_WRONLY); 369 if (fso < 0) 370 fatal("%s: %s", special, strerror(errno)); 371 372 /* Bail if target special is mounted */ 373 n = getmntinfo(&mp, MNT_NOWAIT); 374 if (n == 0) 375 fatal("%s: getmntinfo: %s", special, strerror(errno)); 376 377 len = sizeof(_PATH_DEV) - 1; 378 s1 = special; 379 if (strncmp(_PATH_DEV, s1, len) == 0) 380 s1 += len; 381 382 while (--n >= 0) { 383 s2 = mp->f_mntfromname; 384 if (strncmp(_PATH_DEV, s2, len) == 0) { 385 s2 += len - 1; 386 *s2 = 'r'; 387 } 388 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) 389 fatal("%s is mounted on %s", 390 special, mp->f_mntonname); 391 ++mp; 392 } 393 } 394 if (mfs && disktype != NULL) { 395 lp = (struct disklabel *)getdiskbyname(disktype); 396 if (lp == NULL) 397 fatal("%s: unknown disk type", disktype); 398 pp = &lp->d_partitions[1]; 399 } else { 400 fsi = open(special, O_RDONLY); 401 if (fsi < 0) 402 fatal("%s: %s", special, strerror(errno)); 403 if (fstat(fsi, &st) < 0) 404 fatal("%s: %s", special, strerror(errno)); 405 if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs) 406 printf("%s: %s: not a character-special device\n", 407 progname, special); 408 cp = index(argv[0], '\0') - 1; 409 if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp)) 410 fatal("%s: can't figure out file system partition", 411 argv[0]); 412 #ifdef COMPAT 413 if (!mfs && disktype == NULL) 414 disktype = argv[1]; 415 #endif 416 lp = getdisklabel(special, fsi); 417 if (isdigit(*cp)) 418 pp = &lp->d_partitions[0]; 419 else 420 pp = &lp->d_partitions[*cp - 'a']; 421 if (pp->p_size == 0) 422 fatal("%s: `%c' partition is unavailable", 423 argv[0], *cp); 424 if (pp->p_fstype == FS_BOOT) 425 fatal("%s: `%c' partition overlaps boot program", 426 argv[0], *cp); 427 } 428 if (fssize == 0) 429 fssize = pp->p_size; 430 if (fssize > pp->p_size && !mfs) 431 fatal("%s: maximum file system size on the `%c' partition is %d", 432 argv[0], *cp, pp->p_size); 433 if (rpm == 0) { 434 rpm = lp->d_rpm; 435 if (rpm <= 0) 436 rpm = 3600; 437 } 438 if (ntracks == 0) { 439 ntracks = lp->d_ntracks; 440 if (ntracks <= 0) 441 fatal("%s: no default #tracks", argv[0]); 442 } 443 if (nsectors == 0) { 444 nsectors = lp->d_nsectors; 445 if (nsectors <= 0) 446 fatal("%s: no default #sectors/track", argv[0]); 447 } 448 if (sectorsize == 0) { 449 sectorsize = lp->d_secsize; 450 if (sectorsize <= 0) 451 fatal("%s: no default sector size", argv[0]); 452 } 453 if (trackskew == -1) { 454 trackskew = lp->d_trackskew; 455 if (trackskew < 0) 456 trackskew = 0; 457 } 458 if (interleave == 0) { 459 interleave = lp->d_interleave; 460 if (interleave <= 0) 461 interleave = 1; 462 } 463 if (fsize == 0) { 464 fsize = pp->p_fsize; 465 if (fsize <= 0) 466 fsize = MAX(DFL_FRAGSIZE, lp->d_secsize); 467 } 468 if (bsize == 0) { 469 bsize = pp->p_frag * pp->p_fsize; 470 if (bsize <= 0) 471 bsize = MIN(DFL_BLKSIZE, 8 * fsize); 472 } 473 /* 474 * Maxcontig sets the default for the maximum number of blocks 475 * that may be allocated sequentially. With filesystem clustering 476 * it is possible to allocate contiguous blocks up to the maximum 477 * transfer size permitted by the controller or buffering. 478 */ 479 if (maxcontig == 0) 480 maxcontig = MAX(1, MAXPHYS / bsize - 1); 481 if (density == 0) 482 density = NFPI * fsize; 483 if (minfree < MINFREE && opt != FS_OPTSPACE) { 484 fprintf(stderr, "Warning: changing optimization to space "); 485 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE); 486 opt = FS_OPTSPACE; 487 } 488 if (trackspares == -1) { 489 trackspares = lp->d_sparespertrack; 490 if (trackspares < 0) 491 trackspares = 0; 492 } 493 nphyssectors = nsectors + trackspares; 494 if (cylspares == -1) { 495 cylspares = lp->d_sparespercyl; 496 if (cylspares < 0) 497 cylspares = 0; 498 } 499 secpercyl = nsectors * ntracks - cylspares; 500 /* 501 * Only complain if -t or -u have been specified; the default 502 * case (4096 sectors per cylinder) is intented to disagree 503 * with the disklabel. 504 */ 505 if (t_or_u_flag && secpercyl != lp->d_secpercyl) 506 fprintf(stderr, "%s (%d) %s (%lu)\n", 507 "Warning: calculated sectors per cylinder", secpercyl, 508 "disagrees with disk label", lp->d_secpercyl); 509 if (maxbpg == 0) 510 maxbpg = MAXBLKPG(bsize); 511 headswitch = lp->d_headswitch; 512 trackseek = lp->d_trkseek; 513 #ifdef notdef /* label may be 0 if faked up by kernel */ 514 bbsize = lp->d_bbsize; 515 sbsize = lp->d_sbsize; 516 #endif 517 oldpartition = *pp; 518 #ifdef tahoe 519 realsectorsize = sectorsize; 520 if (sectorsize != DEV_BSIZE) { /* XXX */ 521 int secperblk = DEV_BSIZE / sectorsize; 522 523 sectorsize = DEV_BSIZE; 524 nsectors /= secperblk; 525 nphyssectors /= secperblk; 526 secpercyl /= secperblk; 527 fssize /= secperblk; 528 pp->p_size /= secperblk; 529 } 530 #endif 531 mkfs(pp, special, fsi, fso); 532 #ifdef tahoe 533 if (realsectorsize != DEV_BSIZE) 534 pp->p_size *= DEV_BSIZE / realsectorsize; 535 #endif 536 if (!Nflag) 537 close(fso); 538 close(fsi); 539 #ifdef MFS 540 if (mfs) { 541 struct mfs_args args; 542 543 sprintf(buf, "mfs:%d", getpid()); 544 args.fspec = buf; 545 args.export.ex_root = -2; 546 if (mntflags & MNT_RDONLY) 547 args.export.ex_flags = MNT_EXRDONLY; 548 else 549 args.export.ex_flags = 0; 550 args.base = membase; 551 args.size = fssize * sectorsize; 552 if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0) 553 fatal("%s: %s", argv[1], strerror(errno)); 554 if(filename) { 555 munmap(membase,fssize * sectorsize); 556 } 557 } 558 #endif 559 exit(0); 560 } 561 562 #ifdef COMPAT 563 char lmsg[] = "%s: can't read disk label; disk type must be specified"; 564 #else 565 char lmsg[] = "%s: can't read disk label"; 566 #endif 567 568 struct disklabel * 569 getdisklabel(s, fd) 570 char *s; 571 int fd; 572 { 573 static struct disklabel lab; 574 575 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 576 #ifdef COMPAT 577 if (disktype) { 578 struct disklabel *lp, *getdiskbyname(); 579 580 unlabeled++; 581 lp = getdiskbyname(disktype); 582 if (lp == NULL) 583 fatal("%s: unknown disk type", disktype); 584 return (lp); 585 } 586 #endif 587 warn("ioctl (GDINFO)"); 588 fatal(lmsg, s); 589 } 590 return (&lab); 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