* Copyright (c) 1983 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
"@(#) Copyright (c) 1983 Regents of the University of California.\n\
static char sccsid
[] = "@(#)newfs.c 6.17 (Berkeley) %G%";
* newfs: friendly front end to mkfs
#include <sys/disklabel.h>
#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 NBPI bytes, 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 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
= NBPI
; /* 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 */
register struct partition
*pp
;
register struct disklabel
*lp
;
struct disklabel
*getdisklabel();
struct partition oldpartition
;
while (argc
> 0 && argv
[0][0] == '-') {
for (cp
= &argv
[0][1]; *cp
; cp
++)
fatal("-S: missing sector size");
sectorsize
= atoi(*argv
);
fatal("%s: bad sector size", *argv
);
fatal("-a: missing max contiguous blocks\n");
fatal("%s: bad max contiguous blocks\n",
fatal("-b: missing block size");
fatal("%s: bad block size", *argv
);
fatal("-c: missing cylinders/group");
fatal("%s: bad cylinders/group", *argv
);
fatal("-d: missing rotational delay\n");
fatal("%s: bad rotational delay\n",
fatal("-e: missing blocks pre file in a cyl group\n");
fatal("%s: bad blocks per file in a cyl group\n",
fatal("-f: missing frag size");
fatal("%s: bad frag size", *argv
);
fatal("-i: missing bytes per inode\n");
fatal("%s: bad bytes per inode\n",
fatal("%s: bad track skew", *argv
);
interleave
= atoi(*argv
);
fatal("%s: bad interleave", *argv
);
fatal("-m: missing free space %%\n");
if (minfree
< 0 || minfree
> 99)
fatal("%s: bad free space %%\n",
fatal("-n: missing rotational layout count\n");
fatal("%s: bad rotational layout count\n",
fatal("-o: missing optimization preference");
if (strcmp(*argv
, "space") == 0)
else if (strcmp(*argv
, "time") == 0)
fatal("%s: bad optimization preference %s",
"(options are `space' or `time')");
fatal("-p: spare sectors per track");
trackspares
= atoi(*argv
);
fatal("%s: bad spare sectors per track", *argv
);
fatal("-r: missing revs/minute\n");
fatal("%s: bad revs/minute\n", *argv
);
fatal("-s: missing file system size");
fatal("%s: bad file system size",
fatal("-t: missing track total");
fatal("%s: bad total tracks", *argv
);
fatal("-u: missing sectors/track");
fatal("%s: bad sectors/track", *argv
);
fatal("-x: spare sectors per cylinder");
fatal("%s: bad spare sectors per cylinder", *argv
);
fatal("-%c: unknown flag", *cp
);
"usage: newfs [ fsoptions ] special-device [device-type]\n");
fprintf(stderr
, "usage: newfs [ fsoptions ] special-device\n");
fprintf(stderr
, "where fsoptions are:\n");
fprintf(stderr
, "\t-N do not create file system, %s\n",
"just print out parameters");
fprintf(stderr
, "\t-b block size\n");
fprintf(stderr
, "\t-f frag size\n");
fprintf(stderr
, "\t-m minimum free space %%\n");
fprintf(stderr
, "\t-o optimization preference %s\n",
fprintf(stderr
, "\t-a maximum contiguous blocks\n");
fprintf(stderr
, "\t-d rotational delay between %s\n",
fprintf(stderr
, "\t-e maximum blocks per file in a %s\n",
fprintf(stderr
, "\t-i number of bytes per inode\n");
fprintf(stderr
, "\t-c cylinders/group\n");
fprintf(stderr
, "\t-n number of distinguished %s\n",
fprintf(stderr
, "\t-s file system size (sectors)\n");
fprintf(stderr
, "\t-r revolutions/minute\n");
fprintf(stderr
, "\t-S sector size\n");
fprintf(stderr
, "\t-u sectors/track\n");
fprintf(stderr
, "\t-t tracks/cylinder\n");
fprintf(stderr
, "\t-p spare sectors per track\n");
fprintf(stderr
, "\t-x spare sectors per cylinder\n");
fprintf(stderr
, "\t-l hardware sector interleave\n");
fprintf(stderr
, "\t-k sector 0 skew, per track\n");
cp
= rindex(special
, '/');
if (*special
== 'r' && special
[1] != 'a' && special
[1] != 'b')
(void)sprintf(device
, "%s/r%s", _PATH_DEV
, special
);
fso
= open(special
, O_WRONLY
);
fsi
= open(special
, O_RDONLY
);
if (fstat(fsi
, &st
) < 0) {
fprintf(stderr
, "newfs: "); perror(special
);
if ((st
.st_mode
& S_IFMT
) != S_IFCHR
)
fatal("%s: not a character device", special
);
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]);
lp
= getdisklabel(special
, fsi
, argv
[1]);
lp
= getdisklabel(special
, fsi
);
pp
= &lp
->d_partitions
[0];
pp
= &lp
->d_partitions
[*cp
- 'a'];
fatal("%s: `%c' partition is unavailable", argv
[0], *cp
);
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 (%d)\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
;
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
);
getdisklabel(s
, fd
, type
)
static struct disklabel lab
;
struct disklabel
*getdiskbyname();
if (ioctl(fd
, DIOCGDINFO
, (char *)&lab
) < 0) {
perror("ioctl (GDINFO)");
"%s: can't read disk label; disk type must be specified", s
);
return (getdiskbyname(type
));
static struct disklabel lab
;
if (ioctl(fd
, DIOCGDINFO
, (char *)&lab
) < 0) {
perror("ioctl (GDINFO)");
fatal("%s: can't read disk label", s
);
register struct disklabel
*lp
;
lp
->d_checksum
= dkcksum(lp
);
if (ioctl(fd
, DIOCWDINFO
, (char *)lp
) < 0) {
perror("ioctl (WDINFO)");
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
);
*(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
, L_SET
) == -1) {
perror("lseek to badsector area");
if (write(cfd
, blk
, lp
->d_secsize
) < lp
->d_secsize
) {
fprintf(stderr
, "alternate label %d ", i
/2);
fprintf(stderr
, "newfs: ");
fprintf(stderr
, fmt
, arg1
, arg2
);