121159Sdist /* 239049Smckusick * Copyright (c) 1983, 1989 The Regents of the University of California. 339049Smckusick * All rights reserved. 439049Smckusick * 542704Sbostic * %sccs.include.redist.c% 621159Sdist */ 721159Sdist 810762Ssam #ifndef lint 9*53709Smckusick static char sccsid[] = "@(#)newfs.c 6.30 (Berkeley) 05/27/92"; 1039049Smckusick #endif /* not lint */ 1139049Smckusick 1239049Smckusick #ifndef lint 1321159Sdist char copyright[] = 1439049Smckusick "@(#) Copyright (c) 1983, 1989 Regents of the University of California.\n\ 1521159Sdist All rights reserved.\n"; 1639049Smckusick #endif /* not lint */ 1710762Ssam 1810762Ssam /* 1911069Ssam * newfs: friendly front end to mkfs 2010762Ssam */ 2110762Ssam #include <sys/param.h> 2210762Ssam #include <sys/stat.h> 2330380Smckusick #include <sys/ioctl.h> 2430380Smckusick #include <sys/disklabel.h> 2530380Smckusick #include <sys/file.h> 2639049Smckusick #include <sys/mount.h> 2751533Sbostic #include <ufs/ufs/dir.h> 2851533Sbostic #include <ufs/ffs/fs.h> 2910762Ssam 3047870Skarels #include <errno.h> 3150787Storek #if __STDC__ 3247870Skarels #include <stdarg.h> 3350787Storek #else 3450787Storek #include <varargs.h> 3550787Storek #endif 3610762Ssam #include <stdio.h> 3730380Smckusick #include <ctype.h> 3847870Skarels #include <string.h> 3947870Skarels #include <stdlib.h> 4037951Sbostic #include <paths.h> 4110762Ssam 4250787Storek #if __STDC__ 4350787Storek void fatal(const char *fmt, ...); 4450787Storek #else 4550787Storek void fatal(); 4650787Storek #endif 4750787Storek 4847870Skarels #define COMPAT /* allow non-labeled disks */ 4931680Skarels 5030380Smckusick /* 5130380Smckusick * The following two constants set the default block and fragment sizes. 5230380Smckusick * Both constants must be a power of 2 and meet the following constraints: 5330380Smckusick * MINBSIZE <= DESBLKSIZE <= MAXBSIZE 5430380Smckusick * sectorsize <= DESFRAGSIZE <= DESBLKSIZE 5530380Smckusick * DESBLKSIZE / DESFRAGSIZE <= 8 5630380Smckusick */ 5730380Smckusick #define DFL_FRAGSIZE 1024 5830380Smckusick #define DFL_BLKSIZE 8192 5911183Ssam 6030380Smckusick /* 6134137Smckusick * Cylinder groups may have up to many cylinders. The actual 6230380Smckusick * number used depends upon how much information can be stored 6334137Smckusick * on a single cylinder. The default is to use 16 cylinders 6430380Smckusick * per group. 6530380Smckusick */ 6630380Smckusick #define DESCPG 16 /* desired fs_cpg */ 6730380Smckusick 6830380Smckusick /* 6930380Smckusick * MINFREE gives the minimum acceptable percentage of file system 7030380Smckusick * blocks which may be free. If the freelist drops below this level 7130380Smckusick * only the superuser may continue to allocate blocks. This may 7230380Smckusick * be set to 0 if no reserve of free blocks is deemed necessary, 7330380Smckusick * however throughput drops by fifty percent if the file system 7430380Smckusick * is run at between 90% and 100% full; thus the default value of 7530380Smckusick * fs_minfree is 10%. With 10% free space, fragmentation is not a 7630380Smckusick * problem, so we choose to optimize for time. 7730380Smckusick */ 7830380Smckusick #define MINFREE 10 7930380Smckusick #define DEFAULTOPT FS_OPTTIME 8030380Smckusick 8130380Smckusick /* 8230380Smckusick * ROTDELAY gives the minimum number of milliseconds to initiate 8330380Smckusick * another disk transfer on the same cylinder. It is used in 8430380Smckusick * determining the rotationally optimal layout for disk blocks 8530380Smckusick * within a file; the default of fs_rotdelay is 4ms. 8630380Smckusick */ 8730380Smckusick #define ROTDELAY 4 8830380Smckusick 8930380Smckusick /* 9030380Smckusick * MAXCONTIG sets the default for the maximum number of blocks 9130380Smckusick * that may be allocated sequentially. Since UNIX drivers are 9230380Smckusick * not capable of scheduling multi-block transfers, this defaults 9330380Smckusick * to 1 (ie no contiguous blocks are allocated). 9430380Smckusick */ 9530380Smckusick #define MAXCONTIG 1 9630380Smckusick 9730380Smckusick /* 9832309Smckusick * MAXBLKPG determines the maximum number of data blocks which are 9932309Smckusick * placed in a single cylinder group. The default is one indirect 10032309Smckusick * block worth of data blocks. 10132309Smckusick */ 10232309Smckusick #define MAXBLKPG(bsize) ((bsize) / sizeof(daddr_t)) 10332309Smckusick 10432309Smckusick /* 10530380Smckusick * Each file system has a number of inodes statically allocated. 10639049Smckusick * We allocate one inode slot per NFPI fragments, expecting this 10730380Smckusick * to be far more than we will ever need. 10830380Smckusick */ 10939049Smckusick #define NFPI 4 11030380Smckusick 11134137Smckusick /* 11234137Smckusick * For each cylinder we keep track of the availability of blocks at different 11334137Smckusick * rotational positions, so that we can lay out the data to be picked 11434137Smckusick * up with minimum rotational latency. NRPOS is the default number of 11534137Smckusick * rotational positions that we distinguish. With NRPOS of 8 the resolution 11634137Smckusick * of our summary information is 2ms for a typical 3600 rpm drive. 11734137Smckusick */ 11834137Smckusick #define NRPOS 8 /* number distinct rotational positions */ 11934137Smckusick 12034137Smckusick 12139325Smckusick int mfs; /* run as the memory based filesystem */ 12230380Smckusick int Nflag; /* run without writing file system */ 12310762Ssam int fssize; /* file system size */ 12410762Ssam int ntracks; /* # tracks/cylinder */ 12510762Ssam int nsectors; /* # sectors/track */ 12630386Smckusick int nphyssectors; /* # sectors/track including spares */ 12730380Smckusick int secpercyl; /* sectors per cylinder */ 12830386Smckusick int trackspares = -1; /* spare sectors per track */ 12930386Smckusick int cylspares = -1; /* spare sectors per cylinder */ 13010762Ssam int sectorsize; /* bytes/sector */ 13130691Skarels #ifdef tahoe 13230691Skarels int realsectorsize; /* bytes/sector in hardware */ 13330691Skarels #endif 13411069Ssam int rpm; /* revolutions/minute of drive */ 13530386Smckusick int interleave; /* hardware sector interleave */ 13630386Smckusick int trackskew = -1; /* sector 0 skew, per track */ 13730386Smckusick int headswitch; /* head switch time, usec */ 13830386Smckusick int trackseek; /* track-to-track seek, usec */ 13930862Skarels int fsize = 0; /* fragment size */ 14030862Skarels int bsize = 0; /* block size */ 14130380Smckusick int cpg = DESCPG; /* cylinders/cylinder group */ 14232119Smckusick int cpgflg; /* cylinders/cylinder group flag was given */ 14330380Smckusick int minfree = MINFREE; /* free space threshold */ 14430380Smckusick int opt = DEFAULTOPT; /* optimization preference (space or time) */ 14539049Smckusick int density; /* number of bytes per inode */ 14630380Smckusick int maxcontig = MAXCONTIG; /* max contiguous blocks to allocate */ 14730380Smckusick int rotdelay = ROTDELAY; /* rotational delay between blocks */ 14832309Smckusick int maxbpg; /* maximum blocks per file in a cyl group */ 14934137Smckusick int nrpos = NRPOS; /* # of distinguished rotational positions */ 15030691Skarels int bbsize = BBSIZE; /* boot block size */ 15130691Skarels int sbsize = SBSIZE; /* superblock size */ 15239325Smckusick int mntflags; /* flags to be passed to mount */ 15339049Smckusick u_long memleft; /* virtual memory available */ 15439049Smckusick caddr_t membase; /* start address of memory based filesystem */ 15531680Skarels #ifdef COMPAT 15643314Smckusick char *disktype; 15747870Skarels int unlabeled; 15831680Skarels #endif 15910762Ssam 16010762Ssam char device[MAXPATHLEN]; 16139049Smckusick char *progname; 16210762Ssam 16310762Ssam main(argc, argv) 16414064Smckusick int argc; 16510762Ssam char *argv[]; 16610762Ssam { 16747870Skarels extern char *optarg; 16847870Skarels extern int optind; 16947870Skarels register int ch; 17010762Ssam register struct partition *pp; 17130380Smckusick register struct disklabel *lp; 17230380Smckusick struct disklabel *getdisklabel(); 17330380Smckusick struct partition oldpartition; 17410762Ssam struct stat st; 17530380Smckusick int fsi, fso; 17647870Skarels char *cp, *special, *opstring, buf[BUFSIZ]; 177*53709Smckusick struct statfs *mp; 178*53709Smckusick char *s1, *s2; 179*53709Smckusick int len, n; 18010762Ssam 18147870Skarels if (progname = rindex(*argv, '/')) 18247870Skarels ++progname; 18347870Skarels else 18439049Smckusick progname = *argv; 18547870Skarels 18650402Smckusick if (strstr(progname, "mfs")) { 18747870Skarels mfs = 1; 18839049Smckusick Nflag++; 18939049Smckusick } 19010762Ssam 19147870Skarels opstring = "F:NS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:"; 19247870Skarels if (!mfs) 19347870Skarels opstring += 2; /* -F is mfs only */ 19439325Smckusick 19547870Skarels while ((ch = getopt(argc, argv, opstring)) != EOF) 19647870Skarels switch(ch) { 19747870Skarels case 'F': 19847870Skarels if ((mntflags = atoi(optarg)) == 0) 19947870Skarels fatal("%s: bad mount flags", optarg); 20047870Skarels break; 20147870Skarels case 'N': 20247870Skarels Nflag++; 20347870Skarels break; 20447870Skarels case 'S': 20547870Skarels if ((sectorsize = atoi(optarg)) <= 0) 20647870Skarels fatal("%s: bad sector size", optarg); 20747870Skarels break; 20843314Smckusick #ifdef COMPAT 20947870Skarels case 'T': 21047870Skarels disktype = optarg; 21147870Skarels break; 21243314Smckusick #endif 21347870Skarels case 'a': 21447870Skarels if ((maxcontig = atoi(optarg)) <= 0) 21547870Skarels fatal("%s: bad max contiguous blocks\n", 21647870Skarels optarg); 21747870Skarels break; 21847870Skarels case 'b': 21947870Skarels if ((bsize = atoi(optarg)) < MINBSIZE) 22047870Skarels fatal("%s: bad block size", optarg); 22147870Skarels break; 22247870Skarels case 'c': 22347870Skarels if ((cpg = atoi(optarg)) <= 0) 22447870Skarels fatal("%s: bad cylinders/group", optarg); 22547870Skarels cpgflg++; 22647870Skarels break; 22747870Skarels case 'd': 22847870Skarels if ((rotdelay = atoi(optarg)) < 0) 22947870Skarels fatal("%s: bad rotational delay\n", optarg); 23047870Skarels break; 23147870Skarels case 'e': 23247870Skarels if ((maxbpg = atoi(optarg)) <= 0) 23347870Skarels fatal("%s: bad blocks per file in a cyl group\n", 23447870Skarels optarg); 23547870Skarels break; 23647870Skarels case 'f': 23747870Skarels if ((fsize = atoi(optarg)) <= 0) 23847870Skarels fatal("%s: bad frag size", optarg); 23947870Skarels break; 24047870Skarels case 'i': 24147870Skarels if ((density = atoi(optarg)) <= 0) 24247870Skarels fatal("%s: bad bytes per inode\n", optarg); 24347870Skarels break; 24447870Skarels case 'k': 24547870Skarels if ((trackskew = atoi(optarg)) < 0) 24647870Skarels fatal("%s: bad track skew", optarg); 24747870Skarels break; 24847870Skarels case 'l': 24947870Skarels if ((interleave = atoi(optarg)) <= 0) 25047870Skarels fatal("%s: bad interleave", optarg); 25147870Skarels break; 25247870Skarels case 'm': 25347870Skarels if ((minfree = atoi(optarg)) < 0 || minfree > 99) 25447870Skarels fatal("%s: bad free space %%\n", optarg); 25547870Skarels break; 25647870Skarels case 'n': 25747870Skarels if ((nrpos = atoi(optarg)) <= 0) 25847870Skarels fatal("%s: bad rotational layout count\n", 25947870Skarels optarg); 26047870Skarels break; 26147870Skarels case 'o': 26247870Skarels if (strcmp(optarg, "space") == 0) 26347870Skarels opt = FS_OPTSPACE; 26447870Skarels else if (strcmp(optarg, "time") == 0) 26547870Skarels opt = FS_OPTTIME; 26647870Skarels else 26747870Skarels fatal("%s: bad optimization preference %s", 26847870Skarels optarg, "(options are `space' or `time')"); 26947870Skarels break; 27047870Skarels case 'p': 27147870Skarels if ((trackspares = atoi(optarg)) < 0) 27247870Skarels fatal("%s: bad spare sectors per track", 27347870Skarels optarg); 27447870Skarels break; 27547870Skarels case 'r': 27647870Skarels if ((rpm = atoi(optarg)) <= 0) 27747870Skarels fatal("%s: bad revs/minute\n", optarg); 27847870Skarels break; 27947870Skarels case 's': 28047870Skarels if ((fssize = atoi(optarg)) <= 0) 28147870Skarels fatal("%s: bad file system size", optarg); 28247870Skarels break; 28347870Skarels case 't': 28447870Skarels if ((ntracks = atoi(optarg)) <= 0) 28547870Skarels fatal("%s: bad total tracks", optarg); 28647870Skarels break; 28747870Skarels case 'u': 28847870Skarels if ((nsectors = atoi(optarg)) <= 0) 28947870Skarels fatal("%s: bad sectors/track", optarg); 29047870Skarels break; 29147870Skarels case 'x': 29247870Skarels if ((cylspares = atoi(optarg)) < 0) 29347870Skarels fatal("%s: bad spare sectors per cylinder", 29447870Skarels optarg); 29547870Skarels break; 29647870Skarels case '?': 29747870Skarels default: 29847870Skarels usage(); 29947870Skarels } 30047870Skarels argc -= optind; 30147870Skarels argv += optind; 30243314Smckusick 30347870Skarels if (argc != 2 && (mfs || argc != 1)) 30447870Skarels usage(); 30510762Ssam 30610762Ssam special = argv[0]; 30714064Smckusick cp = rindex(special, '/'); 30847870Skarels if (cp == 0) { 30947870Skarels /* 31047870Skarels * No path prefix; try /dev/r%s then /dev/%s. 31147870Skarels */ 31247870Skarels (void)sprintf(device, "%sr%s", _PATH_DEV, special); 31347870Skarels if (stat(device, &st) == -1) 31447870Skarels (void)sprintf(device, "%s%s", _PATH_DEV, special); 31547870Skarels special = device; 31647870Skarels } 317*53709Smckusick if (Nflag) { 318*53709Smckusick fso = -1; 319*53709Smckusick } else { 32030380Smckusick fso = open(special, O_WRONLY); 32147870Skarels if (fso < 0) 32247870Skarels fatal("%s: %s", special, strerror(errno)); 323*53709Smckusick 324*53709Smckusick /* Bail if target special is mounted */ 325*53709Smckusick n = getmntinfo(&mp, MNT_NOWAIT); 326*53709Smckusick if (n == 0) 327*53709Smckusick fatal("%s: getmntinfo: %s", special, strerror(errno)); 328*53709Smckusick 329*53709Smckusick len = sizeof(_PATH_DEV) - 1; 330*53709Smckusick s1 = special; 331*53709Smckusick if (strncmp(_PATH_DEV, s1, len) == 0) 332*53709Smckusick s1 += len; 333*53709Smckusick 334*53709Smckusick while (--n >= 0) { 335*53709Smckusick s2 = mp->f_mntfromname; 336*53709Smckusick if (strncmp(_PATH_DEV, s2, len) == 0) { 337*53709Smckusick s2 += len - 1; 338*53709Smckusick *s2 = 'r'; 339*53709Smckusick } 340*53709Smckusick if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) 341*53709Smckusick fatal("%s is mounted on %s", 342*53709Smckusick special, mp->f_mntonname); 343*53709Smckusick ++mp; 344*53709Smckusick } 345*53709Smckusick } 34630380Smckusick fsi = open(special, O_RDONLY); 34747870Skarels if (fsi < 0) 34847870Skarels fatal("%s: %s", special, strerror(errno)); 34947870Skarels if (fstat(fsi, &st) < 0) 35047870Skarels fatal("%s: %s", special, strerror(errno)); 35147870Skarels if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs) 35247870Skarels printf("%s: %s: not a character-special device\n", 35347870Skarels progname, special); 35410762Ssam cp = index(argv[0], '\0') - 1; 35530380Smckusick if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp)) 35610762Ssam fatal("%s: can't figure out file system partition", argv[0]); 35731680Skarels #ifdef COMPAT 35843314Smckusick if (!mfs && disktype == NULL) 35943314Smckusick disktype = argv[1]; 36043314Smckusick #endif 36130380Smckusick lp = getdisklabel(special, fsi); 36230380Smckusick if (isdigit(*cp)) 36330380Smckusick pp = &lp->d_partitions[0]; 36430380Smckusick else 36530380Smckusick pp = &lp->d_partitions[*cp - 'a']; 36630380Smckusick if (pp->p_size == 0) 36730380Smckusick fatal("%s: `%c' partition is unavailable", argv[0], *cp); 36830380Smckusick if (fssize == 0) 36910762Ssam fssize = pp->p_size; 37039325Smckusick if (fssize > pp->p_size && !mfs) 37130380Smckusick fatal("%s: maximum file system size on the `%c' partition is %d", 37230380Smckusick argv[0], *cp, pp->p_size); 37330380Smckusick if (rpm == 0) { 37430380Smckusick rpm = lp->d_rpm; 37530380Smckusick if (rpm <= 0) 37630743Skarels rpm = 3600; 37710762Ssam } 37830380Smckusick if (ntracks == 0) { 37930380Smckusick ntracks = lp->d_ntracks; 38030380Smckusick if (ntracks <= 0) 38130743Skarels fatal("%s: no default #tracks", argv[0]); 38230380Smckusick } 38310762Ssam if (nsectors == 0) { 38430380Smckusick nsectors = lp->d_nsectors; 38530380Smckusick if (nsectors <= 0) 38630743Skarels fatal("%s: no default #sectors/track", argv[0]); 38710762Ssam } 38810762Ssam if (sectorsize == 0) { 38930380Smckusick sectorsize = lp->d_secsize; 39030380Smckusick if (sectorsize <= 0) 39130743Skarels fatal("%s: no default sector size", argv[0]); 39210762Ssam } 39330386Smckusick if (trackskew == -1) { 39430386Smckusick trackskew = lp->d_trackskew; 39530386Smckusick if (trackskew < 0) 39630743Skarels trackskew = 0; 39730386Smckusick } 39830386Smckusick if (interleave == 0) { 39930386Smckusick interleave = lp->d_interleave; 40030386Smckusick if (interleave <= 0) 40130743Skarels interleave = 1; 40230386Smckusick } 40310762Ssam if (fsize == 0) { 40410762Ssam fsize = pp->p_fsize; 40530380Smckusick if (fsize <= 0) 40630380Smckusick fsize = MAX(DFL_FRAGSIZE, lp->d_secsize); 40710762Ssam } 40830380Smckusick if (bsize == 0) { 40930380Smckusick bsize = pp->p_frag * pp->p_fsize; 41030380Smckusick if (bsize <= 0) 41130380Smckusick bsize = MIN(DFL_BLKSIZE, 8 * fsize); 41211069Ssam } 41339049Smckusick if (density == 0) 41439049Smckusick density = NFPI * fsize; 41524702Smckusick if (minfree < 10 && opt != FS_OPTSPACE) { 41630380Smckusick fprintf(stderr, "Warning: changing optimization to space "); 41730380Smckusick fprintf(stderr, "because minfree is less than 10%%\n"); 41824702Smckusick opt = FS_OPTSPACE; 41924702Smckusick } 42030386Smckusick if (trackspares == -1) { 42130386Smckusick trackspares = lp->d_sparespertrack; 42230386Smckusick if (trackspares < 0) 42330743Skarels trackspares = 0; 42430386Smckusick } 42530386Smckusick nphyssectors = nsectors + trackspares; 42630386Smckusick if (cylspares == -1) { 42730386Smckusick cylspares = lp->d_sparespercyl; 42830386Smckusick if (cylspares < 0) 42930743Skarels cylspares = 0; 43030386Smckusick } 43130386Smckusick secpercyl = nsectors * ntracks - cylspares; 43230380Smckusick if (secpercyl != lp->d_secpercyl) 43347870Skarels fprintf(stderr, "%s (%d) %s (%lu)\n", 43430380Smckusick "Warning: calculated sectors per cylinder", secpercyl, 43530380Smckusick "disagrees with disk label", lp->d_secpercyl); 43632321Smckusick if (maxbpg == 0) 43732321Smckusick maxbpg = MAXBLKPG(bsize); 43830386Smckusick headswitch = lp->d_headswitch; 43930386Smckusick trackseek = lp->d_trkseek; 44047870Skarels #ifdef notdef /* label may be 0 if faked up by kernel */ 44130691Skarels bbsize = lp->d_bbsize; 44230691Skarels sbsize = lp->d_sbsize; 44345836Skarels #endif 44430380Smckusick oldpartition = *pp; 44530691Skarels #ifdef tahoe 44630691Skarels realsectorsize = sectorsize; 44730862Skarels if (sectorsize != DEV_BSIZE) { /* XXX */ 44830691Skarels int secperblk = DEV_BSIZE / sectorsize; 44930691Skarels 45030691Skarels sectorsize = DEV_BSIZE; 45130691Skarels nsectors /= secperblk; 45230691Skarels nphyssectors /= secperblk; 45330691Skarels secpercyl /= secperblk; 45430691Skarels fssize /= secperblk; 45530691Skarels pp->p_size /= secperblk; 45630691Skarels } 45730691Skarels #endif 45830380Smckusick mkfs(pp, special, fsi, fso); 45930691Skarels #ifdef tahoe 46030691Skarels if (realsectorsize != DEV_BSIZE) 46130691Skarels pp->p_size *= DEV_BSIZE / realsectorsize; 46230691Skarels #endif 46330380Smckusick if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition))) 46430380Smckusick rewritelabel(special, fso, lp); 46539049Smckusick if (!Nflag) 46639049Smckusick close(fso); 46739049Smckusick close(fsi); 46849063Sbostic #ifdef MFS 46939325Smckusick if (mfs) { 47049063Sbostic struct mfs_args args; 47149063Sbostic 47239325Smckusick sprintf(buf, "mfs:%d", getpid()); 47339049Smckusick args.name = buf; 47439049Smckusick args.base = membase; 47539049Smckusick args.size = fssize * sectorsize; 47647870Skarels if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0) 47747870Skarels fatal("%s: %s", argv[1], strerror(errno)); 47839049Smckusick } 47949063Sbostic #endif 48030380Smckusick exit(0); 48130380Smckusick } 48230380Smckusick 48331680Skarels #ifdef COMPAT 48443314Smckusick char lmsg[] = "%s: can't read disk label; disk type must be specified"; 48543314Smckusick #else 48643314Smckusick char lmsg[] = "%s: can't read disk label"; 48743314Smckusick #endif 48831680Skarels 48931680Skarels struct disklabel * 49030380Smckusick getdisklabel(s, fd) 49130380Smckusick char *s; 49231680Skarels int fd; 49330380Smckusick { 49430380Smckusick static struct disklabel lab; 49530380Smckusick 49630380Smckusick if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 49743314Smckusick #ifdef COMPAT 49843314Smckusick if (disktype) { 49947870Skarels struct disklabel *lp, *getdiskbyname(); 50043314Smckusick 50147870Skarels unlabeled++; 50247870Skarels lp = getdiskbyname(disktype); 50347870Skarels if (lp == NULL) 50447870Skarels fatal("%s: unknown disk type", disktype); 50547870Skarels return (lp); 50643314Smckusick } 50743314Smckusick #endif 50847870Skarels (void)fprintf(stderr, 50947870Skarels "%s: ioctl (GDINFO): %s\n", progname, strerror(errno)); 51043314Smckusick fatal(lmsg, s); 51110762Ssam } 51230380Smckusick return (&lab); 51330380Smckusick } 51410762Ssam 51530380Smckusick rewritelabel(s, fd, lp) 51630380Smckusick char *s; 51730380Smckusick int fd; 51830380Smckusick register struct disklabel *lp; 51930380Smckusick { 52031680Skarels #ifdef COMPAT 52147870Skarels if (unlabeled) 52231680Skarels return; 52331680Skarels #endif 52430380Smckusick lp->d_checksum = 0; 52530380Smckusick lp->d_checksum = dkcksum(lp); 52630380Smckusick if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) { 52747870Skarels (void)fprintf(stderr, 52847870Skarels "%s: ioctl (WDINFO): %s\n", progname, strerror(errno)); 52930380Smckusick fatal("%s: can't rewrite disk label", s); 53010762Ssam } 53130446Skarels #if vax 53230446Skarels if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) { 53330446Skarels register i; 53430691Skarels int cfd; 53530446Skarels daddr_t alt; 53630691Skarels char specname[64]; 53730691Skarels char blk[1024]; 53831045Ssam char *cp; 53930446Skarels 54030691Skarels /* 54130691Skarels * Make name for 'c' partition. 54230691Skarels */ 54330691Skarels strcpy(specname, s); 54430691Skarels cp = specname + strlen(specname) - 1; 54530691Skarels if (!isdigit(*cp)) 54630691Skarels *cp = 'c'; 54730691Skarels cfd = open(specname, O_WRONLY); 54847870Skarels if (cfd < 0) 54947870Skarels fatal("%s: %s", specname, strerror(errno)); 55030691Skarels bzero(blk, sizeof(blk)); 55130691Skarels *(struct disklabel *)(blk + LABELOFFSET) = *lp; 55230446Skarels alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors; 55330446Skarels for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) { 55447870Skarels if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize, 55547870Skarels L_SET) == -1) 55647870Skarels fatal("lseek to badsector area: %s", 55747870Skarels strerror(errno)); 55847870Skarels if (write(cfd, blk, lp->d_secsize) < lp->d_secsize) 55947870Skarels fprintf(stderr, 56047870Skarels "%s: alternate label %d write: %s\n", 56147870Skarels progname, i/2, strerror(errno)); 56230380Smckusick } 56339049Smckusick close(cfd); 56430380Smckusick } 56530446Skarels #endif 56610762Ssam } 56710762Ssam 56810762Ssam /*VARARGS*/ 56950787Storek void 57050787Storek #if __STDC__ 57150787Storek fatal(const char *fmt, ...) 57250787Storek #else 57350787Storek fatal(fmt, va_alist) 57410762Ssam char *fmt; 57550787Storek va_dcl 57650787Storek #endif 57710762Ssam { 57847870Skarels va_list ap; 57910762Ssam 58050787Storek #if __STDC__ 58150787Storek va_start(ap, fmt); 58250787Storek #else 58350787Storek va_start(ap); 58450787Storek #endif 58539049Smckusick fprintf(stderr, "%s: ", progname); 58647870Skarels (void)vfprintf(stderr, fmt, ap); 58747870Skarels va_end(ap); 58810762Ssam putc('\n', stderr); 58947870Skarels exit(1); 59010762Ssam } 59147870Skarels 59247870Skarels usage() 59347870Skarels { 59447870Skarels if (mfs) { 59547870Skarels fprintf(stderr, 596*53709Smckusick "usage: %s [ -fsoptions ] special-device mount-point\n", 597*53709Smckusick progname); 59847870Skarels } else 59947870Skarels fprintf(stderr, 600*53709Smckusick "usage: %s [ -fsoptions ] special-device%s\n", 601*53709Smckusick progname, 60247870Skarels #ifdef COMPAT 60347870Skarels " [device-type]"); 60447870Skarels #else 60547870Skarels ""); 60647870Skarels #endif 60747870Skarels fprintf(stderr, "where fsoptions are:\n"); 60847870Skarels fprintf(stderr, 60947870Skarels "\t-N do not create file system, just print out parameters\n"); 61047870Skarels fprintf(stderr, "\t-S sector size\n"); 61147870Skarels #ifdef COMPAT 61247870Skarels fprintf(stderr, "\t-T disktype\n"); 61347870Skarels #endif 61447870Skarels fprintf(stderr, "\t-a maximum contiguous blocks\n"); 61547870Skarels fprintf(stderr, "\t-b block size\n"); 61647870Skarels fprintf(stderr, "\t-c cylinders/group\n"); 61747870Skarels fprintf(stderr, "\t-d rotational delay between contiguous blocks\n"); 61847870Skarels fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 61947870Skarels fprintf(stderr, "\t-f frag size\n"); 62047870Skarels fprintf(stderr, "\t-i number of bytes per inode\n"); 62147870Skarels fprintf(stderr, "\t-k sector 0 skew, per track\n"); 62247870Skarels fprintf(stderr, "\t-l hardware sector interleave\n"); 62347870Skarels fprintf(stderr, "\t-m minimum free space %%\n"); 62447870Skarels fprintf(stderr, "\t-n number of distinguished rotational positions\n"); 62547870Skarels fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 62647870Skarels fprintf(stderr, "\t-p spare sectors per track\n"); 62747870Skarels fprintf(stderr, "\t-s file system size (sectors)\n"); 62847870Skarels fprintf(stderr, "\t-r revolutions/minute\n"); 62947870Skarels fprintf(stderr, "\t-t tracks/cylinder\n"); 63047870Skarels fprintf(stderr, "\t-u sectors/track\n"); 63147870Skarels fprintf(stderr, "\t-x spare sectors per cylinder\n"); 63247870Skarels exit(1); 63347870Skarels } 634