* Copyright (c) 1983, 1989 The Regents of the University of California.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)newfs.c 6.28 (Berkeley) %G%";
"@(#) Copyright (c) 1983, 1989 Regents of the University of California.\n\
* newfs: friendly front end to mkfs
#include <sys/disklabel.h>
void fatal(const char *fmt
, ...);
#define COMPAT /* allow non-labeled disks */
* The following two constants set the default block and fragment sizes.
* Both constants must be a power of 2 and meet the following constraints:
* MINBSIZE <= DESBLKSIZE <= MAXBSIZE
* sectorsize <= DESFRAGSIZE <= DESBLKSIZE
* DESBLKSIZE / DESFRAGSIZE <= 8
#define DFL_FRAGSIZE 1024
* Cylinder groups may have up to many cylinders. The actual
* number used depends upon how much information can be stored
* on a single cylinder. The default is to use 16 cylinders
#define DESCPG 16 /* desired fs_cpg */
* MINFREE gives the minimum acceptable percentage of file system
* blocks which may be free. If the freelist drops below this level
* only the superuser may continue to allocate blocks. This may
* be set to 0 if no reserve of free blocks is deemed necessary,
* however throughput drops by fifty percent if the file system
* is run at between 90% and 100% full; thus the default value of
* fs_minfree is 10%. With 10% free space, fragmentation is not a
* problem, so we choose to optimize for time.
#define DEFAULTOPT FS_OPTTIME
* ROTDELAY gives the minimum number of milliseconds to initiate
* another disk transfer on the same cylinder. It is used in
* determining the rotationally optimal layout for disk blocks
* within a file; the default of fs_rotdelay is 4ms.
* MAXCONTIG sets the default for the maximum number of blocks
* that may be allocated sequentially. Since UNIX drivers are
* not capable of scheduling multi-block transfers, this defaults
* to 1 (ie no contiguous blocks are allocated).
* MAXBLKPG determines the maximum number of data blocks which are
* placed in a single cylinder group. The default is one indirect
* block worth of data blocks.
#define MAXBLKPG(bsize) ((bsize) / sizeof(daddr_t))
* Each file system has a number of inodes statically allocated.
* We allocate one inode slot per NFPI fragments, expecting this
* to be far more than we will ever need.
* For each cylinder we keep track of the availability of blocks at different
* rotational positions, so that we can lay out the data to be picked
* up with minimum rotational latency. NRPOS is the default number of
* rotational positions that we distinguish. With NRPOS of 8 the resolution
* of our summary information is 2ms for a typical 3600 rpm drive.
#define NRPOS 8 /* number distinct rotational positions */
int mfs
; /* run as the memory based filesystem */
int Nflag
; /* run without writing file system */
int fssize
; /* file system size */
int ntracks
; /* # tracks/cylinder */
int nsectors
; /* # sectors/track */
int nphyssectors
; /* # sectors/track including spares */
int secpercyl
; /* sectors per cylinder */
int trackspares
= -1; /* spare sectors per track */
int cylspares
= -1; /* spare sectors per cylinder */
int sectorsize
; /* bytes/sector */
int realsectorsize
; /* bytes/sector in hardware */
int rpm
; /* revolutions/minute of drive */
int interleave
; /* hardware sector interleave */
int trackskew
= -1; /* sector 0 skew, per track */
int headswitch
; /* head switch time, usec */
int trackseek
; /* track-to-track seek, usec */
int fsize
= 0; /* fragment size */
int bsize
= 0; /* block size */
int cpg
= DESCPG
; /* cylinders/cylinder group */
int cpgflg
; /* cylinders/cylinder group flag was given */
int minfree
= MINFREE
; /* free space threshold */
int opt
= DEFAULTOPT
; /* optimization preference (space or time) */
int density
; /* number of bytes per inode */
int maxcontig
= MAXCONTIG
; /* max contiguous blocks to allocate */
int rotdelay
= ROTDELAY
; /* rotational delay between blocks */
int maxbpg
; /* maximum blocks per file in a cyl group */
int nrpos
= NRPOS
; /* # of distinguished rotational positions */
int bbsize
= BBSIZE
; /* boot block size */
int sbsize
= SBSIZE
; /* superblock size */
int mntflags
; /* flags to be passed to mount */
u_long memleft
; /* virtual memory available */
caddr_t membase
; /* start address of memory based filesystem */
register struct partition
*pp
;
register struct disklabel
*lp
;
struct disklabel
*getdisklabel();
struct partition oldpartition
;
char *cp
, *special
, *opstring
, buf
[BUFSIZ
];
if (progname
= rindex(*argv
, '/'))
if (strstr(progname
, "mfs")) {
opstring
= "F:NS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
opstring
+= 2; /* -F is mfs only */
while ((ch
= getopt(argc
, argv
, opstring
)) != EOF
)
if ((mntflags
= atoi(optarg
)) == 0)
fatal("%s: bad mount flags", optarg
);
if ((sectorsize
= atoi(optarg
)) <= 0)
fatal("%s: bad sector size", optarg
);
if ((maxcontig
= atoi(optarg
)) <= 0)
fatal("%s: bad max contiguous blocks\n",
if ((bsize
= atoi(optarg
)) < MINBSIZE
)
fatal("%s: bad block size", optarg
);
if ((cpg
= atoi(optarg
)) <= 0)
fatal("%s: bad cylinders/group", optarg
);
if ((rotdelay
= atoi(optarg
)) < 0)
fatal("%s: bad rotational delay\n", optarg
);
if ((maxbpg
= atoi(optarg
)) <= 0)
fatal("%s: bad blocks per file in a cyl group\n",
if ((fsize
= atoi(optarg
)) <= 0)
fatal("%s: bad frag size", optarg
);
if ((density
= atoi(optarg
)) <= 0)
fatal("%s: bad bytes per inode\n", optarg
);
if ((trackskew
= atoi(optarg
)) < 0)
fatal("%s: bad track skew", optarg
);
if ((interleave
= atoi(optarg
)) <= 0)
fatal("%s: bad interleave", optarg
);
if ((minfree
= atoi(optarg
)) < 0 || minfree
> 99)
fatal("%s: bad free space %%\n", optarg
);
if ((nrpos
= atoi(optarg
)) <= 0)
fatal("%s: bad rotational layout count\n",
if (strcmp(optarg
, "space") == 0)
else if (strcmp(optarg
, "time") == 0)
fatal("%s: bad optimization preference %s",
optarg
, "(options are `space' or `time')");
if ((trackspares
= atoi(optarg
)) < 0)
fatal("%s: bad spare sectors per track",
if ((rpm
= atoi(optarg
)) <= 0)
fatal("%s: bad revs/minute\n", optarg
);
if ((fssize
= atoi(optarg
)) <= 0)
fatal("%s: bad file system size", optarg
);
if ((ntracks
= atoi(optarg
)) <= 0)
fatal("%s: bad total tracks", optarg
);
if ((nsectors
= atoi(optarg
)) <= 0)
fatal("%s: bad sectors/track", optarg
);
if ((cylspares
= atoi(optarg
)) < 0)
fatal("%s: bad spare sectors per cylinder",
if (argc
!= 2 && (mfs
|| argc
!= 1))
cp
= rindex(special
, '/');
* No path prefix; try /dev/r%s then /dev/%s.
(void)sprintf(device
, "%sr%s", _PATH_DEV
, special
);
if (stat(device
, &st
) == -1)
(void)sprintf(device
, "%s%s", _PATH_DEV
, special
);
fso
= open(special
, O_WRONLY
);
fatal("%s: %s", special
, strerror(errno
));
fsi
= open(special
, O_RDONLY
);
fatal("%s: %s", special
, strerror(errno
));
fatal("%s: %s", special
, strerror(errno
));
if ((st
.st_mode
& S_IFMT
) != S_IFCHR
&& !mfs
)
printf("%s: %s: not a character-special device\n",
cp
= index(argv
[0], '\0') - 1;
if (cp
== 0 || (*cp
< 'a' || *cp
> 'h') && !isdigit(*cp
))
fatal("%s: can't figure out file system partition", argv
[0]);
if (!mfs
&& disktype
== NULL
)
lp
= getdisklabel(special
, fsi
);
pp
= &lp
->d_partitions
[0];
pp
= &lp
->d_partitions
[*cp
- 'a'];
fatal("%s: `%c' partition is unavailable", argv
[0], *cp
);
if (fssize
> pp
->p_size
&& !mfs
)
fatal("%s: maximum file system size on the `%c' partition is %d",
argv
[0], *cp
, pp
->p_size
);
fatal("%s: no default #tracks", argv
[0]);
nsectors
= lp
->d_nsectors
;
fatal("%s: no default #sectors/track", argv
[0]);
sectorsize
= lp
->d_secsize
;
fatal("%s: no default sector size", argv
[0]);
trackskew
= lp
->d_trackskew
;
interleave
= lp
->d_interleave
;
fsize
= MAX(DFL_FRAGSIZE
, lp
->d_secsize
);
bsize
= pp
->p_frag
* pp
->p_fsize
;
bsize
= MIN(DFL_BLKSIZE
, 8 * fsize
);
if (minfree
< 10 && opt
!= FS_OPTSPACE
) {
fprintf(stderr
, "Warning: changing optimization to space ");
fprintf(stderr
, "because minfree is less than 10%%\n");
trackspares
= lp
->d_sparespertrack
;
nphyssectors
= nsectors
+ trackspares
;
cylspares
= lp
->d_sparespercyl
;
secpercyl
= nsectors
* ntracks
- cylspares
;
if (secpercyl
!= lp
->d_secpercyl
)
fprintf(stderr
, "%s (%d) %s (%lu)\n",
"Warning: calculated sectors per cylinder", secpercyl
,
"disagrees with disk label", lp
->d_secpercyl
);
maxbpg
= MAXBLKPG(bsize
);
headswitch
= lp
->d_headswitch
;
trackseek
= lp
->d_trkseek
;
/* Reno fix: label may be 0 if faked up by kernel */
#ifdef notdef /* label may be 0 if faked up by kernel */
realsectorsize
= sectorsize
;
if (sectorsize
!= DEV_BSIZE
) { /* XXX */
int secperblk
= DEV_BSIZE
/ sectorsize
;
nphyssectors
/= secperblk
;
mkfs(pp
, special
, fsi
, fso
);
if (realsectorsize
!= DEV_BSIZE
)
pp
->p_size
*= DEV_BSIZE
/ realsectorsize
;
if (!Nflag
&& bcmp(pp
, &oldpartition
, sizeof(oldpartition
)))
rewritelabel(special
, fso
, lp
);
sprintf(buf
, "mfs:%d", getpid());
args
.size
= fssize
* sectorsize
;
if (mount(MOUNT_MFS
, argv
[1], mntflags
, &args
) < 0)
fatal("%s: %s", argv
[1], strerror(errno
));
char lmsg
[] = "%s: can't read disk label; disk type must be specified";
char lmsg
[] = "%s: can't read disk label";
static struct disklabel lab
;
if (ioctl(fd
, DIOCGDINFO
, (char *)&lab
) < 0) {
struct disklabel
*lp
, *getdiskbyname();
lp
= getdiskbyname(disktype
);
fatal("%s: unknown disk type", disktype
);
"%s: ioctl (GDINFO): %s\n", progname
, strerror(errno
));
register struct disklabel
*lp
;
lp
->d_checksum
= dkcksum(lp
);
if (ioctl(fd
, DIOCWDINFO
, (char *)lp
) < 0) {
"%s: ioctl (WDINFO): %s\n", progname
, strerror(errno
));
fatal("%s: can't rewrite disk label", s
);
if (lp
->d_type
== DTYPE_SMD
&& lp
->d_flags
& D_BADSECT
) {
* Make name for 'c' partition.
cp
= specname
+ strlen(specname
) - 1;
cfd
= open(specname
, O_WRONLY
);
fatal("%s: %s", specname
, strerror(errno
));
*(struct disklabel
*)(blk
+ LABELOFFSET
) = *lp
;
alt
= lp
->d_ncylinders
* lp
->d_secpercyl
- lp
->d_nsectors
;
for (i
= 1; i
< 11 && i
< lp
->d_nsectors
; i
+= 2) {
if (lseek(cfd
, (off_t
)(alt
+ i
) * lp
->d_secsize
,
fatal("lseek to badsector area: %s",
if (write(cfd
, blk
, lp
->d_secsize
) < lp
->d_secsize
)
"%s: alternate label %d write: %s\n",
progname
, i
/2, strerror(errno
));
fatal(const char *fmt
, ...)
fprintf(stderr
, "%s: ", progname
);
(void)vfprintf(stderr
, fmt
, ap
);
"usage: mfs [ -fsoptions ] special-device mount-point\n");
"usage: newfs [ -fsoptions ] special-device%s\n",
fprintf(stderr
, "where fsoptions are:\n");
"\t-N do not create file system, just print out parameters\n");
fprintf(stderr
, "\t-S sector size\n");
fprintf(stderr
, "\t-T disktype\n");
fprintf(stderr
, "\t-a maximum contiguous blocks\n");
fprintf(stderr
, "\t-b block size\n");
fprintf(stderr
, "\t-c cylinders/group\n");
fprintf(stderr
, "\t-d rotational delay between contiguous blocks\n");
fprintf(stderr
, "\t-e maximum blocks per file in a cylinder group\n");
fprintf(stderr
, "\t-f frag size\n");
fprintf(stderr
, "\t-i number of bytes per inode\n");
fprintf(stderr
, "\t-k sector 0 skew, per track\n");
fprintf(stderr
, "\t-l hardware sector interleave\n");
fprintf(stderr
, "\t-m minimum free space %%\n");
fprintf(stderr
, "\t-n number of distinguished rotational positions\n");
fprintf(stderr
, "\t-o optimization preference (`space' or `time')\n");
fprintf(stderr
, "\t-p spare sectors per track\n");
fprintf(stderr
, "\t-s file system size (sectors)\n");
fprintf(stderr
, "\t-r revolutions/minute\n");
fprintf(stderr
, "\t-t tracks/cylinder\n");
fprintf(stderr
, "\t-u sectors/track\n");
fprintf(stderr
, "\t-x spare sectors per cylinder\n");