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