--- sbin/newfs/newfs.c.orig Fri Feb 16 13:10:43 2007 +++ sbin/newfs/newfs.c Fri Feb 16 13:20:07 2007 @@ -2,6 +2,15 @@ /* $NetBSD: newfs.c,v 1.20 1996/05/16 07:13:03 thorpej Exp $ */ /* + * Copyright (c) 2002 Networks Associates Technology, Inc. + * All rights reserved. + * + * This software was developed for the FreeBSD Project by Marshall + * Kirk McKusick and Network Associates Laboratories, the Security + * Research Division of Network Associates, Inc. under DARPA/SPAWAR + * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS + * research program. + * * Copyright (c) 1983, 1989, 1993, 1994 * The Regents of the University of California. All rights reserved. * @@ -40,6 +49,7 @@ #include #include +#include #include #include @@ -84,14 +94,6 @@ u_short dkcksum(struct disklabel *); #define DFL_BLKSIZE 16384 /* - * 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 as many as - * possible. - */ -#define DESCPG 65536 /* desired fs_cpg */ - -/* * 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. @@ -101,19 +103,15 @@ u_short dkcksum(struct disklabel *); 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 secpercyl; /* sectors per cylinder */ int sectorsize; /* bytes/sector */ int realsectorsize; /* bytes/sector in hardware */ int fsize = 0; /* fragment size */ int bsize = 0; /* block size */ -int cpg; /* cylinders/cylinder group */ int cpgflg; /* cylinders/cylinder group flag was given */ -int reqopt = -1; /* opt preference has not been specified */ int density; /* number of bytes per inode */ int mntflags = MNT_ASYNC; /* flags to be passed to mount */ int quiet = 0; /* quiet flag */ +int _ffs = 1; /* FFS type (1 or 2) */ u_long memleft; /* virtual memory available */ caddr_t membase; /* start address of memory based filesystem */ char *disktype; @@ -142,7 +140,7 @@ main(int argc, char *argv[]) struct stat st; struct statfs *mp; struct rlimit rl; - int fsi = -1, fso, len, n, ncyls, maxpartitions; + int fsi = -1, fso, len, n, maxpartitions; char *cp = NULL, *s1, *s2, *special, *opstring; #ifdef MFS char mountfromname[BUFSIZ]; @@ -157,7 +155,6 @@ main(int argc, char *argv[]) mode_t mfsmode = 0; char *fstype = NULL; char **saveargv = argv; - int ffs = 1; if (strstr(__progname, "mfs")) mfs = Nflag = quiet = 1; @@ -204,7 +201,9 @@ main(int argc, char *argv[]) case 't': fstype = optarg; if (strcmp(fstype, "ffs")) - ffs = 0; + _ffs = 0; + if (strcmp(fstype, "ffs2") == 0) + _ffs = 2; break; #ifdef MFS case 'P': @@ -215,13 +214,13 @@ main(int argc, char *argv[]) default: usage(); } - if (!ffs) + if (!_ffs) break; } argc -= optind; argv += optind; - if (ffs && argc - mfs != 1) + if (_ffs && argc - mfs != 1) usage(); /* Increase our data size to the max */ @@ -236,7 +235,8 @@ main(int argc, char *argv[]) if (fstype == NULL) fstype = readlabelfs(special, 0); - if (fstype != NULL && strcmp(fstype, "ffs")) { + if (fstype != NULL && strcmp(fstype, "ffs") && + strcmp(fstype, "ffs2")) { snprintf(name, sizeof name, "newfs_%s", fstype); saveargv[0] = name; snprintf(execname, sizeof execname, "%s/newfs_%s", @@ -354,16 +354,6 @@ havelabel: if (fssize > pp->p_size && !mfs) fatal("%s: maximum file system size on the `%c' partition is %d", argv[0], *cp, pp->p_size); - if (ntracks == 0) { - ntracks = lp->d_ntracks; - if (ntracks <= 0) - fatal("%s: no default #tracks", argv[0]); - } - if (nsectors == 0) { - nsectors = lp->d_nsectors; - if (nsectors <= 0) - fatal("%s: no default #sectors/track", argv[0]); - } if (sectorsize == 0) { sectorsize = lp->d_secsize; if (sectorsize <= 0) @@ -381,40 +371,20 @@ havelabel: } if (density == 0) density = NFPI * fsize; - secpercyl = nsectors * ntracks; - if (secpercyl != lp->d_secpercyl) - fprintf(stderr, "%s (%d) %s (%lu)\n", - "Warning: calculated sectors per cylinder", secpercyl, - "disagrees with disk label", - (unsigned long)lp->d_secpercyl); oldpartition = *pp; realsectorsize = sectorsize; if (sectorsize < DEV_BSIZE) { int secperblk = DEV_BSIZE / sectorsize; sectorsize = DEV_BSIZE; - nsectors /= secperblk; - secpercyl /= secperblk; fssize /= secperblk; pp->p_size /= secperblk; } else if (sectorsize > DEV_BSIZE) { int blkpersec = sectorsize / DEV_BSIZE; sectorsize = DEV_BSIZE; - nsectors *= blkpersec; - secpercyl *= blkpersec; fssize *= blkpersec; pp->p_size *= blkpersec; - } - ncyls = fssize / secpercyl; - if (ncyls < 2) - ncyls = 2; - if (cpg == 0) - cpg = DESCPG < ncyls ? DESCPG : ncyls; - else if (cpg > ncyls) { - cpg = ncyls; - printf("Number of cylinders restricts cylinders per group " - "to %d.\n", cpg); } #ifdef MFS if (mfs) { --- sbin/newfs/mkfs.c.orig Fri Mar 16 14:50:51 2007 +++ sbin/newfs/mkfs.c Fri Mar 16 15:02:50 2007 @@ -2,6 +2,15 @@ /* $NetBSD: mkfs.c,v 1.25 1995/06/18 21:35:38 cgd Exp $ */ /* + * Copyright (c) 2002 Networks Associates Technology, Inc. + * All rights reserved. + * + * This software was developed for the FreeBSD Project by Marshall + * Kirk McKusick and Network Associates Laboratories, the Security + * Research Division of Network Associates, Inc. under DARPA/SPAWAR + * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS + * research program. + * * Copyright (c) 1980, 1989, 1993 * The Regents of the University of California. All rights reserved. * @@ -32,16 +41,18 @@ #include #include -#include -#include -#include #include #include #include +#include +#include +#include + #include #include #include +#include #include #ifndef STANDALONE @@ -51,24 +62,33 @@ #endif /* - * We limit the size of the inode map to be no more than a - * third of the cylinder group space, since we must leave at - * least an equal amount of space for the block map. - * - * N.B.: MAXIPG must be a multiple of INOPB(fs). + * Default directory umask. */ -#define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs)) - #define UMASK 0755 -#define MAXINOPB (MAXBSIZE / sizeof(struct ufs1_dinode)) + #define POWEROF2(num) (((num) & ((num) - 1)) == 0) /* + * 'Standard' bad FFS magic. + */ +#define FS_BAD_MAGIC 0x19960408 + +/* + * Desired number of fragments per cylinder group ("infinity"). + */ +#define MAXBLKSPERCG 0x7fffffff + +/* + * The minimum number of cylinder groups that should be created. + */ +#define MINCYLGRPS 4 + +/* * 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)) +#define MAXBLKPG(bsize) ((bsize) / sizeof(ufs2_daddr_t)) /* * variables set up by front end. @@ -76,23 +96,20 @@ extern int mfs; /* run as the memory based filesystem */ extern int Nflag; /* run mkfs without writing file system */ extern int fssize; /* file system size */ -extern int ntracks; /* # tracks/cylinder */ -extern int nsectors; /* # sectors/track */ -extern int secpercyl; /* sectors per cylinder */ extern int sectorsize; /* bytes/sector */ extern int fsize; /* fragment size */ extern int bsize; /* block size */ -extern int cpg; /* cylinders/cylinder group */ extern int cpgflg; /* cylinders/cylinder group flag was given */ extern int density; /* number of bytes per inode */ -extern int maxbpg; /* maximum blocks per file in a cyl group */ extern int quiet; /* quiet flag */ +extern int _ffs; /* FFS type (1 or 2) */ extern caddr_t membase; /* start address of memory based filesystem */ union fs_u { struct fs fs; char pad[SBSIZE]; } *fsun; + #define sblock fsun->fs struct csum *fscs; @@ -101,20 +118,28 @@ union cg_u { struct cg cg; char pad[MAXBSIZE]; } *cgun; + #define acg cgun->cg -struct ufs1_dinode *zino; -char *buf; +union dinode { + struct ufs1_dinode dp1; + struct ufs2_dinode dp2; +}; int fsi, fso; +static caddr_t iobuf; +static long iobufsize; + daddr_t alloc(int, int); static int charsperline(void); +static int ilog2(int); void initcg(int, time_t); void wtfs(daddr_t, int, void *); -int fsinit(time_t, mode_t, uid_t, gid_t); +int fsinit1(time_t, mode_t, uid_t, gid_t); +int fsinit2(time_t); int makedir(struct direct *, int); -void iput(struct ufs1_dinode *, ino_t); +void iput(union dinode *, ino_t); void setblock(struct fs *, unsigned char *, int); void clrblock(struct fs *, unsigned char *, int); int isblock(struct fs *, unsigned char *, int); @@ -140,25 +165,19 @@ siginfo(int sig) #endif void -mkfs(struct partition *pp, char *fsys, int fi, int fo, - mode_t mfsmode, uid_t mfsuid, gid_t mfsgid) +mkfs(struct partition *pp, char *fsys, int fi, int fo, mode_t mfsmode, + uid_t mfsuid, gid_t mfsgid) { - long i, mincpc, mincpg, inospercg; - long cylno, j, warn = 0; - long used, mincpgcnt, bpcg; - long mapcramped, inodecramped; - long postblsize, rotblsize, totalsbsize; time_t utime; quad_t sizepb; - int width; + int i, j, width, origdensity, fragsperinode, minfpg, optimalfpg; + int lastminfpg; + long cylno, csfrags; char tmpbuf[100]; /* XXX this will break in about 2,500 years */ if ((fsun = calloc(1, sizeof (union fs_u))) == NULL || - (cgun = calloc(1, sizeof (union cg_u))) == NULL || - (zino = calloc(1, MAXBSIZE)) == NULL || - (buf = calloc(1, MAXBSIZE)) == NULL) { + (cgun = calloc(1, sizeof (union cg_u))) == NULL) err(1, "calloc"); - } #ifndef STANDALONE time(&utime); @@ -177,416 +196,358 @@ mkfs(struct partition *pp, char *fsys, i } fsi = fi; fso = fo; - sblock.fs_inodefmt = FS_44INODEFMT; - sblock.fs_maxsymlinklen = MAXSYMLINKLEN_UFS1; /* * Validate the given file system size. * Verify that its last block can actually be accessed. */ if (fssize <= 0) errx(13, "preposterous size %u, max is %u", fssize, INT_MAX); + wtfs(fssize - 1, sectorsize, (char *)&sblock); -recalc: - /* - * collect and verify the sector and track info - */ - sblock.fs_nsect = nsectors; - sblock.fs_ntrak = ntracks; - if (sblock.fs_ntrak <= 0) - errx(14, "preposterous ntrak %d", sblock.fs_ntrak); - if (sblock.fs_nsect <= 0) - errx(15, "preposterous nsect %d", sblock.fs_nsect); - /* - * collect and verify the filesystem density info - */ + + sblock.fs_inodefmt = FS_44INODEFMT; + sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; sblock.fs_avgfilesize = AVFILESIZ; sblock.fs_avgfpdir = AFPDIR; + /* - * collect and verify the block and fragment sizes + * Collect and verify the block and fragment sizes. */ + sblock.fs_bsize = bsize; - sblock.fs_fsize = fsize; if (!POWEROF2(sblock.fs_bsize)) { errx(16, "block size must be a power of 2, not %d", sblock.fs_bsize); } + + sblock.fs_fsize = fsize; if (!POWEROF2(sblock.fs_fsize)) { errx(17, "fragment size must be a power of 2, not %d", sblock.fs_fsize); } + if (sblock.fs_fsize < sectorsize) { errx(18, "fragment size %d is too small, minimum is %d", sblock.fs_fsize, sectorsize); } + if (sblock.fs_bsize < MINBSIZE) { errx(19, "block size %d is too small, minimum is %d", sblock.fs_bsize, MINBSIZE); } + if (sblock.fs_bsize > MAXBSIZE) { errx(19, "block size %d is too large, maximum is %d", sblock.fs_bsize, MAXBSIZE); } + if (sblock.fs_bsize < sblock.fs_fsize) { errx(20, "block size (%d) cannot be smaller than fragment size (%d)", sblock.fs_bsize, sblock.fs_fsize); } + + /* + * Calculate the superblock bitmasks and shifts. + */ + sblock.fs_bmask = ~(sblock.fs_bsize - 1); sblock.fs_fmask = ~(sblock.fs_fsize - 1); sblock.fs_qbmask = ~sblock.fs_bmask; sblock.fs_qfmask = ~sblock.fs_fmask; - for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1) - sblock.fs_bshift++; - for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1) - sblock.fs_fshift++; + + sblock.fs_bshift = ilog2(sblock.fs_bsize); + sblock.fs_fshift = ilog2(sblock.fs_fsize); + sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize); - for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1) - sblock.fs_fragshift++; if (sblock.fs_frag > MAXFRAG) { - errx(21, "fragment size %d is too small, minimum with block size %d is %d", - sblock.fs_fsize, sblock.fs_bsize, + errx(21, "fragment size %d is too small, minimum with block " + "size %d is %d", sblock.fs_fsize, sblock.fs_bsize, sblock.fs_bsize / MAXFRAG); } - sblock.fs_nrpos = 1; - sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t); - sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode); + + sblock.fs_fragshift = ilog2(sblock.fs_frag); + sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize); + sblock.fs_size = dbtofsb(&sblock, fssize); sblock.fs_nspf = sblock.fs_fsize / sectorsize; - for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1) - sblock.fs_fsbtodb++; + sblock.fs_maxcontig = 1; + sblock.fs_nrpos = 1; + sblock.fs_cpg = 1; + + /* + * Before the file system is fully initialized, mark it as invalid. + */ + + sblock.fs_magic = FS_BAD_MAGIC; + + /* + * Set the remaining superblock fields. Note that for FFS1, media + * geometry fields are set to fake values. This is for compatibility + * with really ancient kernels that might still inspect these values. + */ + + if (_ffs == 1) { + sblock.fs_sblockloc = SBLOCK_UFS1; + sblock.fs_nindir = sblock.fs_bsize / sizeof(ufs1_daddr_t); + sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode); + sblock.fs_maxsymlinklen = MAXSYMLINKLEN_UFS1; + sblock.fs_inodefmt = FS_44INODEFMT; + sblock.fs_cgoffset = 0; + sblock.fs_cgmask = 0xffffffff; + sblock.fs_ffs1_size = sblock.fs_size; + sblock.fs_rotdelay = 0; + sblock.fs_rps = 60; + sblock.fs_interleave = 1; + sblock.fs_trackskew = 0; + sblock.fs_cpc = 0; + } else { + sblock.fs_sblockloc = SBLOCK_UFS2; + sblock.fs_nindir = sblock.fs_bsize / sizeof(ufs2_daddr_t); + sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode); + sblock.fs_maxsymlinklen = MAXSYMLINKLEN_UFS2; + } + sblock.fs_sblkno = - roundup(howmany(BBSIZE + SBSIZE, sblock.fs_fsize), sblock.fs_frag); + roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize), + sblock.fs_frag); sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno + roundup(howmany(SBSIZE, sblock.fs_fsize), sblock.fs_frag)); sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag; - sblock.fs_cgoffset = roundup( - howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag); - for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1) - sblock.fs_cgmask <<= 1; - if (!POWEROF2(sblock.fs_ntrak)) - sblock.fs_cgmask <<= 1; sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1; + for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) { sizepb *= NINDIR(&sblock); sblock.fs_maxfilesize += sizepb; } + +#ifdef notyet /* - * Validate specified/determined secpercyl - * and calculate minimum cylinders per group. + * It is impossible to create a snapshot in case fs_maxfilesize is + * smaller than fssize. */ - sblock.fs_spc = secpercyl; - for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc; - sblock.fs_cpc > 1 && (i & 1) == 0; - sblock.fs_cpc >>= 1, i >>= 1) - /* void */; - mincpc = sblock.fs_cpc; - bpcg = sblock.fs_spc * sectorsize; - inospercg = roundup(bpcg / sizeof(struct ufs1_dinode), INOPB(&sblock)); - if (inospercg > MAXIPG(&sblock)) - inospercg = MAXIPG(&sblock); - used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock); - mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used, - sblock.fs_spc); - mincpg = roundup(mincpgcnt, mincpc); + + if (sblock.fs_maxfilesize < (u_quad_t)fssize) { + warnx("WARNING: You will be unable to create snapshots on this " + "file system. Correct by using a larger blocksize."); + } +#endif + /* - * Ensure that cylinder group with mincpg has enough space - * for block maps. + * Calculate the number of blocks to put into each cylinder group. The + * first goal is to have at least enough data blocks in each cylinder + * group to meet the density requirement. Once this goal is achieved + * we try to expand to have at least MINCYLGRPS cylinder groups. Once + * this goal is achieved, we pack as many blocks into each cylinder + * group map as will fit. */ - sblock.fs_cpg = mincpg; - sblock.fs_ipg = inospercg; - mapcramped = 0; - /* A cylinder group *must* fit inside one block so force it if not. */ - if (CGSIZE(&sblock) > sblock.fs_bsize && secpercyl > 1024 && ntracks > 1) { - secpercyl /= 2; - ntracks /= 2; - goto recalc; + + /* + * We start by calculating the smallest number of blocks that we can + * put into each cylinder group. If this is too big, we reduce the + * density until it fits. + */ + + origdensity = density; + + for (;;) { + fragsperinode = MAX(numfrags(&sblock, density), 1); + + minfpg = fragsperinode * INOPB(&sblock); + if (minfpg > sblock.fs_size) + minfpg = sblock.fs_size; + + sblock.fs_ipg = INOPB(&sblock); + sblock.fs_fpg = roundup(sblock.fs_iblkno + + sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag); + if (sblock.fs_fpg < minfpg) + sblock.fs_fpg = minfpg; + + sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), + INOPB(&sblock)); + sblock.fs_fpg = roundup(sblock.fs_iblkno + + sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag); + if (sblock.fs_fpg < minfpg) + sblock.fs_fpg = minfpg; + + sblock.fs_spc = sblock.fs_fpg * sblock.fs_nspf; + sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), + INOPB(&sblock)); + + if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize) + break; + + density -= sblock.fs_fsize; } - while (CGSIZE(&sblock) > sblock.fs_bsize) { - mapcramped = 1; - if (sblock.fs_bsize < MAXBSIZE) { - sblock.fs_bsize <<= 1; - if ((i & 1) == 0) { - i >>= 1; - } else { - sblock.fs_cpc <<= 1; - mincpc <<= 1; - mincpg = roundup(mincpgcnt, mincpc); - sblock.fs_cpg = mincpg; - } - sblock.fs_frag <<= 1; - sblock.fs_fragshift += 1; - if (sblock.fs_frag <= MAXFRAG) - continue; - } - if (sblock.fs_fsize == sblock.fs_bsize) { - errx(22, "no block size to support this disk"); - } - sblock.fs_frag >>= 1; - sblock.fs_fragshift -= 1; - sblock.fs_fsize <<= 1; - sblock.fs_nspf <<= 1; - } + + if (density != origdensity) + printf("Density reduced from %d to %d bytes per inode\n", + origdensity, density); + /* - * Ensure that cylinder group with mincpg has enough space for inodes. + * Start packing more blocks into the cylinder group until it cannot + * grow any larger, the number of cylinder groups drops below + * MINCYLGRPS, or we reach the requested size. */ - inodecramped = 0; - used *= sectorsize; - inospercg = roundup(((int64_t)mincpg * bpcg - used) / density, - INOPB(&sblock)); - sblock.fs_ipg = inospercg; - while (inospercg > MAXIPG(&sblock)) { - inodecramped = 1; - if (mincpc == 1 || sblock.fs_frag == 1 || - sblock.fs_bsize == MINBSIZE) + + for (; sblock.fs_fpg < MAXBLKSPERCG; sblock.fs_fpg += sblock.fs_frag) { + sblock.fs_spc = sblock.fs_fpg * sblock.fs_nspf; + sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), + INOPB(&sblock)); + + if (sblock.fs_size / sblock.fs_fpg < MINCYLGRPS) break; - printf("With a block size of %d %s %ld\n", - sblock.fs_bsize, "minimum bytes per inode is", - (mincpg * bpcg - used) / MAXIPG(&sblock) + 1); - sblock.fs_bsize >>= 1; - sblock.fs_frag >>= 1; - sblock.fs_fragshift -= 1; - mincpc >>= 1; - sblock.fs_cpg = roundup(mincpgcnt, mincpc); - if (CGSIZE(&sblock) > sblock.fs_bsize) { - sblock.fs_bsize <<= 1; + + if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize) + continue; + + if (CGSIZE(&sblock) == (unsigned long)sblock.fs_bsize) break; - } - mincpg = sblock.fs_cpg; - inospercg = roundup(((int64_t)mincpg * bpcg - used) / density, + + sblock.fs_fpg -= sblock.fs_frag; + sblock.fs_spc = sblock.fs_fpg * sblock.fs_nspf; + sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), INOPB(&sblock)); - sblock.fs_ipg = inospercg; + + break; } - if (inodecramped) { - if (inospercg > MAXIPG(&sblock)) { - printf("Minimum bytes per inode is %ld\n", - (mincpg * bpcg - used) / MAXIPG(&sblock) + 1); - } else if (!mapcramped) { - printf("With %d bytes per inode, ", density); - printf("minimum cylinders per group is %ld\n", mincpg); - } - } - if (mapcramped) { - printf("With %d sectors per cylinder, ", sblock.fs_spc); - printf("minimum cylinders per group is %ld\n", mincpg); - } - if (inodecramped || mapcramped) { - if (sblock.fs_bsize != bsize) - printf("%s to be changed from %d to %d\n", - "This requires the block size", - bsize, sblock.fs_bsize); - if (sblock.fs_fsize != fsize) - printf("\t%s to be changed from %d to %d\n", - "and the fragment size", - fsize, sblock.fs_fsize); - exit(23); - } + /* - * Calculate the number of cylinders per group + * Check to be sure that the last cylinder group has enough blocks to + * be viable. If it is too small, reduce the number of blocks per + * cylinder group which will have the effect of moving more blocks into + * the last cylinder group. */ - sblock.fs_cpg = cpg; - if (sblock.fs_cpg % mincpc != 0) { - if (!quiet) - printf("%s groups must have a multiple of %ld " - "cylinders\n", cpgflg ? "Cylinder" : - "Warning: cylinder", mincpc); - sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc); - if (!cpgflg) - cpg = sblock.fs_cpg; + + optimalfpg = sblock.fs_fpg; + + for (;;) { + sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg); + lastminfpg = roundup(sblock.fs_iblkno + + sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag); + if (sblock.fs_size < lastminfpg) { + printf("File system size %jd < minimum size of %d\n", + (intmax_t)sblock.fs_size, lastminfpg); + exit(28); + } + + if (sblock.fs_size % sblock.fs_fpg >= lastminfpg || + sblock.fs_size % sblock.fs_fpg == 0) + break; + + sblock.fs_fpg -= sblock.fs_frag; + sblock.fs_spc = sblock.fs_fpg * sblock.fs_nspf; + sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), + INOPB(&sblock)); } + + if (optimalfpg != sblock.fs_fpg) + printf("Reduced number of fragments per cylinder group from %d" + " to %d to enlarge last cylinder group\n", optimalfpg, + sblock.fs_fpg); + /* - * Must ensure there is enough space for inodes. + * Back to filling superblock fields. */ - sblock.fs_ipg = roundup(((int64_t)sblock.fs_cpg * bpcg - used) / - density, INOPB(&sblock)); - while (sblock.fs_ipg > MAXIPG(&sblock)) { - inodecramped = 1; - sblock.fs_cpg -= mincpc; - sblock.fs_ipg = roundup(((int64_t)sblock.fs_cpg * bpcg - used) / - density, INOPB(&sblock)); + + if (_ffs == 1) { + sblock.fs_nsect = sblock.fs_spc; + sblock.fs_npsect = sblock.fs_spc; + sblock.fs_ncyl = sblock.fs_ncg; } - /* - * Must ensure there is enough space to hold block map. - */ - if (CGSIZE(&sblock) > sblock.fs_bsize && secpercyl > 1024 && ntracks > 1) { - secpercyl /= 2; - ntracks /= 2; - goto recalc; - } - while (CGSIZE(&sblock) > sblock.fs_bsize) { - mapcramped = 1; - sblock.fs_cpg -= mincpc; - sblock.fs_ipg = roundup(((int64_t)sblock.fs_cpg * bpcg - used) / - density, INOPB(&sblock)); - } - sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock); - if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) { - errx(24, "panic (fs_cpg * fs_spc) %% NSPF != 0"); - } - if (sblock.fs_cpg < mincpg) { - errx(25, "cylinder groups must have at least %ld cylinders", - mincpg); - } else if (cpgflg && sblock.fs_cpg != cpg) { - if (!mapcramped && !inodecramped) - errx(26, "!mapcramped && !inodecramped"); - if (mapcramped && inodecramped) - printf("Block size and bytes per inode restrict"); - else if (mapcramped) - printf("Block size restricts"); - else - printf("Bytes per inode restrict"); - printf(" cylinders per group to %d.\n", sblock.fs_cpg); - if (cpgflg) - exit(27); - } + sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock)); - /* - * Now have size for file system and nsect and ntrak. - * Determine number of cylinders and blocks in the file system. - */ - sblock.fs_ffs1_size = dbtofsb(&sblock, fssize); - sblock.fs_ncyl = sblock.fs_ffs1_size * NSPF(&sblock) / sblock.fs_spc; - if (sblock.fs_ffs1_size * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) { - sblock.fs_ncyl++; - warn = 1; - } - if (sblock.fs_ncyl < 1) { - errx(28, - "file systems must have at least one cylinder (not %d)", - sblock.fs_ncyl); - } - /* - * Determine feasability/values of rotational layout tables. - * - * The size of the rotational layout tables is limited by the - * size of the superblock, SBSIZE. The amount of space available - * for tables is calculated as (SBSIZE - sizeof (struct fs)). - * The size of these tables is inversely proportional to the block - * size of the file system. The size increases if sectors per track - * are not powers of two, because more cylinders must be described - * by the tables before the rotational pattern repeats (fs_cpc). - */ - sblock.fs_interleave = 1; - sblock.fs_trackskew = 0; - sblock.fs_npsect = sblock.fs_spc; - sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; - sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs)); - if (sblock.fs_sbsize > SBSIZE) - sblock.fs_sbsize = SBSIZE; - if (sblock.fs_ntrak == 1) { - sblock.fs_cpc = 0; - goto next; - } - postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(int16_t); - rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock); - totalsbsize = sizeof(struct fs) + rotblsize; - /* use dynamic table space */ - sblock.fs_postbloff = &sblock.fs_space[0] - - (u_char *)(&sblock.fs_firstfield); - sblock.fs_rotbloff = sblock.fs_postbloff + postblsize; - totalsbsize += postblsize; - if (totalsbsize > SBSIZE || fragroundup(&sblock, totalsbsize) > SBSIZE - || sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) { - printf("%s %s %d %s %d.%s", - "Warning: insufficient space in super block for\n", - "rotational layout tables with nsect", sblock.fs_nsect, - "and ntrak", sblock.fs_ntrak, - "\nFile system performance may be impaired.\n"); - sblock.fs_cpc = 0; - goto next; - } - sblock.fs_sbsize = fragroundup(&sblock, totalsbsize); -next: - /* - * Compute/validate number of cylinder groups. - */ - sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg; - if (sblock.fs_ncyl % sblock.fs_cpg) - sblock.fs_ncg++; sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock); - i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1); - if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) { - printf("inode blocks/cyl group (%ld) >= data blocks (%d)\n", - cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag, - sblock.fs_fpg / sblock.fs_frag); - printf("number of cylinders per cylinder group (%d) %s.\n", - sblock.fs_cpg, "must be increased"); - exit(29); - } - j = sblock.fs_ncg - 1; - if ((i = sblock.fs_ffs1_size - j * sblock.fs_fpg) < sblock.fs_fpg && - cgdmin(&sblock, j) - cgbase(&sblock, j) > i) { - if (j == 0) { - errx(30, "filesystem must have at least %d sectors", - NSPF(&sblock) * - (cgdmin(&sblock, 0) + 3 * sblock.fs_frag)); - } - printf("Warning: inode blocks/cyl group (%ld) >= data blocks (%ld) in last\n", - (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag, - i / sblock.fs_frag); - printf(" cylinder group. This implies %ld sector(s) cannot be allocated.\n", - i * NSPF(&sblock)); - sblock.fs_ncg--; - sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg; - sblock.fs_ffs1_size = sblock.fs_ncyl * sblock.fs_spc / NSPF(&sblock); - fssize = fsbtodb(&sblock, sblock.fs_ffs1_size); - warn = 0; - } - if (!quiet && warn && !mfs) { - printf("Warning: %d sector(s) in last cylinder unallocated\n", - sblock.fs_spc - - (dbtofsb(&sblock, fssize) * NSPF(&sblock) - - (sblock.fs_ncyl - 1) * sblock.fs_spc)); - } - /* - * fill in remaining fields of the super block - */ - sblock.fs_ffs1_csaddr = cgdmin(&sblock, 0); + sblock.fs_csaddr = cgdmin(&sblock, 0); sblock.fs_cssize = fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum)); - /* - * The superblock fields 'fs_csmask' and 'fs_csshift' are no - * longer used. However, we still initialise them so that the - * filesystem remains compatible with old kernels. - */ - i = sblock.fs_bsize / sizeof(struct csum); - sblock.fs_csmask = ~(i - 1); - for (sblock.fs_csshift = 0; i > 1; i >>= 1) - sblock.fs_csshift++; - if ((fscs = calloc(1, sblock.fs_cssize)) == NULL) - err(1, "cg summary"); - sblock.fs_magic = FS_MAGIC; - sblock.fs_rotdelay = 0; + fscs = (struct csum *)calloc(1, sblock.fs_cssize); + if (fscs == NULL) + errx(31, "calloc failed"); + + sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs)); + if (sblock.fs_sbsize > SBLOCKSIZE) + sblock.fs_sbsize = SBLOCKSIZE; + sblock.fs_minfree = MINFREE; - sblock.fs_maxcontig = 0; sblock.fs_maxbpg = MAXBLKPG(bsize); - sblock.fs_rps = 60; sblock.fs_optim = DEFAULTOPT; sblock.fs_cgrotor = 0; - sblock.fs_ffs1_cstotal.cs_ndir = 0; - sblock.fs_ffs1_cstotal.cs_nbfree = 0; - sblock.fs_ffs1_cstotal.cs_nifree = 0; - sblock.fs_ffs1_cstotal.cs_nffree = 0; + sblock.fs_pendingblocks = 0; + sblock.fs_pendinginodes = 0; sblock.fs_fmod = 0; sblock.fs_ronly = 0; - sblock.fs_clean = FS_ISCLEAN; + sblock.fs_state = 0; + sblock.fs_clean = 1; sblock.fs_id[0] = (u_int32_t)utime; sblock.fs_id[1] = (u_int32_t)arc4random(); + sblock.fs_fsmnt[0] = '\0'; + csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize); + sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno - + sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno); + + sblock.fs_cstotal.cs_nbfree = + fragstoblks(&sblock, sblock.fs_dsize) - + howmany(csfrags, sblock.fs_frag); + sblock.fs_cstotal.cs_nffree = + fragnum(&sblock, sblock.fs_size) + + (fragnum(&sblock, csfrags) > 0 ? + sblock.fs_frag - fragnum(&sblock, csfrags) : 0); + sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - ROOTINO; + sblock.fs_cstotal.cs_ndir = 0; + + sblock.fs_dsize -= csfrags; + sblock.fs_time = utime; + + if (_ffs == 1) { + sblock.fs_ffs1_time = sblock.fs_time; + sblock.fs_ffs1_dsize = sblock.fs_dsize; + sblock.fs_ffs1_csaddr = sblock.fs_csaddr; + sblock.fs_ffs1_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir; + sblock.fs_ffs1_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree; + sblock.fs_ffs1_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree; + sblock.fs_ffs1_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree; + } + /* * Dump out summary information about file system. */ + if (!mfs) { - printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n", - fsys, sblock.fs_ffs1_size * NSPF(&sblock), sblock.fs_ncyl, - "cylinders", sblock.fs_ntrak, sblock.fs_nsect); #define B2MBFACTOR (1 / (1024.0 * 1024.0)) - printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n", - (float)sblock.fs_ffs1_size * sblock.fs_fsize * B2MBFACTOR, - sblock.fs_ncg, sblock.fs_cpg, + printf("%s: %.1fMB in %jd sectors of %d bytes\n", fsys, + (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR, + (intmax_t)fsbtodb(&sblock, sblock.fs_size), sectorsize); + printf("%d cylinder groups of %.2fMB, %d blocks, %d" + " inodes each\n", sblock.fs_ncg, (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR, - sblock.fs_ipg); + sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg); #undef B2MBFACTOR } + /* + * Wipe out old FFS1 superblock if necessary. + */ + if (_ffs == 2) { + union fs_u *fsun1; + struct fs *fs1; + fsun1 = calloc(1, sizeof(union fs_u)); + if (fsun1 == NULL) + err(39, "calloc"); + fs1 = &fsun1->fs; + rdfs(SBLOCK_UFS1 / sectorsize, SBSIZE, (char *)fs1); + if (fs1->fs_magic == FS_UFS1_MAGIC) { + fs1->fs_magic = FS_BAD_MAGIC; + wtfs(SBLOCK_UFS1 / sectorsize, SBSIZE, (char *)fs1); + } + free(fsun1); + } + + wtfs((int)sblock.fs_sblockloc / sectorsize, SBSIZE, (char *)&sblock); + + sblock.fs_magic = (_ffs == 2) ? FS_UFS2_MAGIC : FS_UFS1_MAGIC; + + /* * Now build the cylinders group blocks and * then print out indices of cylinder groups. */ @@ -600,6 +561,24 @@ next: #endif i = 0; width = charsperline(); + /* + * Allocate space for superblock, cylinder group map, and two sets of + * inode blocks. + */ + if (sblock.fs_bsize < SBLOCKSIZE) + iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize; + else + iobufsize = 4 * sblock.fs_bsize; + if ((iobuf = malloc(iobufsize)) == 0) { + printf("Cannot allocate I/O buffer\n"); + exit(38); + } + bzero(iobuf, iobufsize); + /* + * Make a copy of the superblock into the buffer that we will be + * writing out in each cylinder group. + */ + bcopy((char *)&sblock, iobuf, SBLOCKSIZE); for (cylno = 0; cylno < sblock.fs_ncg; cylno++) { cur_cylno = (sig_atomic_t)cylno; initcg(cylno, utime); @@ -617,38 +596,43 @@ next: printf("%s", tmpbuf); fflush(stdout); } + if (!quiet) printf("\n"); + if (Nflag && !mfs) exit(0); + /* - * Now construct the initial file system, - * then write out the super-block. + * Now construct the initial file system, then write out the superblock. */ - if (fsinit(utime, mfsmode, mfsuid, mfsgid) != 0) - errx(32, "fsinit failed"); - sblock.fs_ffs1_time = utime; - /* don't write magic until we are done */ - sblock.fs_magic = 0; - wtfs((int)SBOFF / sectorsize, SBSIZE, (char *)&sblock); - sblock.fs_magic = FS_MAGIC; + + if (_ffs == 1) { + if (fsinit1(utime, mfsmode, mfsuid, mfsgid)) + errx(32, "fsinit1 failed"); + + sblock.fs_ffs1_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir; + sblock.fs_ffs1_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree; + sblock.fs_ffs1_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree; + sblock.fs_ffs1_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree; + } else { + if (fsinit2(utime)) + errx(32, "fsinit2 failed"); + } + + wtfs((int)sblock.fs_sblockloc / sectorsize, SBSIZE, (char *)&sblock); + for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) - wtfs(fsbtodb(&sblock, sblock.fs_ffs1_csaddr + numfrags(&sblock, i)), - sblock.fs_cssize - i < sblock.fs_bsize ? - sblock.fs_cssize - i : sblock.fs_bsize, - ((char *)fscs) + i); + wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)), + sblock.fs_cssize - i < sblock.fs_bsize ? + sblock.fs_cssize - i : sblock.fs_bsize, + ((char *)fscs) + i); + /* - * Write out the duplicate super blocks + * Update information about this partion in pack label, to that it may + * be updated on disk. */ - for (cylno = 0; cylno < sblock.fs_ncg; cylno++) - wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), - SBSIZE, (char *)&sblock); - /* done, can write with magic now */ - wtfs((int)SBOFF / sectorsize, SBSIZE, (char *)&sblock); - /* - * Update information about this partion in pack - * label, to that it may be updated on disk. - */ + pp->p_fstype = FS_BSDFFS; pp->p_fsize = sblock.fs_fsize; pp->p_frag = sblock.fs_frag; @@ -661,71 +645,93 @@ next: void initcg(int cylno, time_t utime) { - daddr_t cbase, d, dlower, dupper, dmax, blkno; - long i, j; + int i, j, d, dlower, dupper, blkno, start; + ufs2_daddr_t cbase, dmax; + struct ufs1_dinode *dp1; + struct ufs2_dinode *dp2; struct csum *cs; /* - * Determine block bounds for cylinder group. - * Allow space for super block summary information in first - * cylinder group. + * Determine block bounds for cylinder group. Allow space for + * superblock summary information in first cylinder group. */ + cbase = cgbase(&sblock, cylno); dmax = cbase + sblock.fs_fpg; - if (dmax > sblock.fs_ffs1_size) - dmax = sblock.fs_ffs1_size; + if (dmax > sblock.fs_size) + dmax = sblock.fs_size; + dlower = cgsblock(&sblock, cylno) - cbase; dupper = cgdmin(&sblock, cylno) - cbase; + if (cylno == 0) dupper += howmany(sblock.fs_cssize, sblock.fs_fsize); - cs = fscs + cylno; + + cs = &fscs[cylno]; memset(&acg, 0, sblock.fs_cgsize); - acg.cg_time = utime; + + acg.cg_ffs2_time = utime; acg.cg_magic = CG_MAGIC; acg.cg_cgx = cylno; - if (cylno == sblock.fs_ncg - 1) - acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg; - else - acg.cg_ncyl = sblock.fs_cpg; - acg.cg_niblk = sblock.fs_ipg; + acg.cg_ffs2_niblk = sblock.fs_ipg; + acg.cg_initediblk = sblock.fs_ipg < 2 * INOPB(&sblock) ? + sblock.fs_ipg : 2 * INOPB(&sblock); acg.cg_ndblk = dmax - cbase; - acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield); - acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t); - acg.cg_iusedoff = acg.cg_boff + - sblock.fs_cpg * sblock.fs_nrpos * sizeof(int16_t); - acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY); - acg.cg_nextfreeoff = acg.cg_freeoff + - howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY); - if (acg.cg_nextfreeoff > sblock.fs_cgsize) + + start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield); + + if (_ffs == 2) { + acg.cg_iusedoff = start; + } else { + /* Hack to maintain compatibility with old fsck. */ + if (cylno == sblock.fs_ncg - 1) + acg.cg_ncyl = 0; + else + acg.cg_ncyl = sblock.fs_cpg; + acg.cg_time = acg.cg_ffs2_time; + acg.cg_ffs2_time = 0; + acg.cg_niblk = acg.cg_ffs2_niblk; + acg.cg_ffs2_niblk = 0; + acg.cg_initediblk = 0; + acg.cg_btotoff = start; + acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t); + acg.cg_iusedoff = acg.cg_boff + + sblock.fs_cpg * sizeof(u_int16_t); + } + + acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT); + acg.cg_nextfreeoff = acg.cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT); + + if (acg.cg_nextfreeoff > sblock.fs_cgsize) { + printf("%d > %d\n", acg.cg_nextfreeoff, sblock.fs_cgsize); errx(37, "panic: cylinder group too big"); + } + acg.cg_cs.cs_nifree += sblock.fs_ipg; - if (cylno == 0) + + if (cylno == 0) { for (i = 0; i < ROOTINO; i++) { setbit(cg_inosused(&acg), i); acg.cg_cs.cs_nifree--; } - for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) { - for (j = 0; j < sblock.fs_bsize / sizeof(struct ufs1_dinode); j++) - zino[j].di_gen = (u_int32_t)arc4random(); - wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i), - sblock.fs_bsize, (char *)zino); } + if (cylno > 0) { /* - * In cylno 0, beginning space is reserved - * for boot and super blocks. + * In cylno 0, space is reserved for boot and superblocks. */ for (d = 0; d < dlower; d += sblock.fs_frag) { blkno = d / sblock.fs_frag; setblock(&sblock, cg_blksfree(&acg), blkno); acg.cg_cs.cs_nbfree++; - cg_blktot(&acg)[cbtocylno(&sblock, d)]++; - cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) - [cbtorpos(&sblock, d)]++; + if (_ffs == 1) { + cg_blktot(&acg)[cbtocylno(&sblock, d)]++; + cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) + [cbtorpos(&sblock, d)]++; + } } - sblock.fs_ffs1_dsize += dlower; } - sblock.fs_ffs1_dsize += acg.cg_ndblk - dupper; + if ((i = dupper % sblock.fs_frag)) { acg.cg_frsum[sblock.fs_frag - i]++; for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) { @@ -733,43 +739,75 @@ initcg(int cylno, time_t utime) acg.cg_cs.cs_nffree++; } } - for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) { + + for (d = dupper; + d + sblock.fs_frag <= acg.cg_ndblk; + d += sblock.fs_frag) { blkno = d / sblock.fs_frag; setblock(&sblock, cg_blksfree(&acg), blkno); acg.cg_cs.cs_nbfree++; - cg_blktot(&acg)[cbtocylno(&sblock, d)]++; - cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) - [cbtorpos(&sblock, d)]++; - d += sblock.fs_frag; + if (_ffs == 1) { + cg_blktot(&acg)[cbtocylno(&sblock, d)]++; + cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) + [cbtorpos(&sblock, d)]++; + } } - if (d < dmax - cbase) { - acg.cg_frsum[dmax - cbase - d]++; - for (; d < dmax - cbase; d++) { + + if (d < acg.cg_ndblk) { + acg.cg_frsum[acg.cg_ndblk - d]++; + for (; d < acg.cg_ndblk; d++) { setbit(cg_blksfree(&acg), d); acg.cg_cs.cs_nffree++; } } - sblock.fs_ffs1_cstotal.cs_ndir += acg.cg_cs.cs_ndir; - sblock.fs_ffs1_cstotal.cs_nffree += acg.cg_cs.cs_nffree; - sblock.fs_ffs1_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree; - sblock.fs_ffs1_cstotal.cs_nifree += acg.cg_cs.cs_nifree; + *cs = acg.cg_cs; - wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), - sblock.fs_bsize, (char *)&acg); -} -/* - * initialize the file system - */ -struct ufs1_dinode node; + /* + * Write out the duplicate super block, the cylinder group map + * and two blocks worth of inodes in a single write. + */ + start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE; + bcopy((char *)&acg, &iobuf[start], sblock.fs_cgsize); + start += sblock.fs_bsize; + dp1 = (struct ufs1_dinode *)(&iobuf[start]); + dp2 = (struct ufs2_dinode *)(&iobuf[start]); + + for (i = 0; i < acg.cg_initediblk; i++) { + if (sblock.fs_magic == FS_UFS1_MAGIC) { + dp1->di_gen = (u_int32_t)arc4random(); + dp1++; + } else { + dp2->di_gen = (u_int32_t)arc4random(); + dp2++; + } + } + + wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf); + + if (_ffs == 1) { + /* Initialize inodes for FFS1. */ + for (i = 2 * sblock.fs_frag; + i < sblock.fs_ipg / INOPF(&sblock); + i += sblock.fs_frag) { + dp1 = (struct ufs1_dinode *)(&iobuf[start]); + for (j = 0; j < INOPB(&sblock); j++) { + dp1->di_gen = (u_int32_t)arc4random(); + dp1++; + } + wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i), + sblock.fs_bsize, &iobuf[start]); + } + } +} + #define PREDEFDIR 2 struct direct root_dir[] = { { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." }, { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, }; - struct odirect { u_int32_t d_ino; u_int16_t d_reclen; @@ -781,36 +819,123 @@ struct odirect { }; int -fsinit(time_t utime, mode_t mfsmode, uid_t mfsuid, gid_t mfsgid) +fsinit1(time_t utime, mode_t mfsmode, uid_t mfsuid, gid_t mfsgid) { + union dinode node; + + memset(&node, 0, sizeof(node)); + /* - * initialize the node + * Initialize the node. */ - node.di_atime = utime; - node.di_mtime = utime; - node.di_ctime = utime; + + node.dp1.di_atime = utime; + node.dp1.di_mtime = utime; + node.dp1.di_ctime = utime; + /* - * create the root directory + * Create the root directory. */ + if (mfs) { - node.di_mode = IFDIR | mfsmode; - node.di_uid = mfsuid; - node.di_gid = mfsgid; + node.dp1.di_mode = IFDIR | mfsmode; + node.dp1.di_uid = mfsuid; + node.dp1.di_gid = mfsgid; } else { - node.di_mode = IFDIR | UMASK; - node.di_uid = geteuid(); - node.di_gid = getegid(); + node.dp1.di_mode = IFDIR | UMASK; + node.dp1.di_uid = geteuid(); + node.dp1.di_gid = getegid(); } - node.di_nlink = PREDEFDIR; - node.di_size = makedir(root_dir, PREDEFDIR); - if ((node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode)) == 0) + + node.dp1.di_nlink = PREDEFDIR; + node.dp1.di_size = makedir(root_dir, PREDEFDIR); + node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode); + if (node.dp1.di_db[0] == 0) return (1); - node.di_blocks = btodb(fragroundup(&sblock, node.di_size)); - wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf); + + node.dp1.di_blocks = btodb(fragroundup(&sblock, node.dp1.di_size)); + + wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, iobuf); iput(&node, ROOTINO); + +#ifdef notyet + /* + * Create the .snap directory. + */ + + node.dp1.di_mode |= 020; + node.dp1.di_gid = gid; + node.dp1.di_nlink = SNAPLINKCNT; + node.dp1.di_size = makedir(snap_dir, SNAPLINKCNT); + + node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode); + if (node.dp1.di_db[0] == 0) + return (1); + + node.dp1.di_blocks = btodb(fragroundup(&sblock, node.dp1.di_size)); + + wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, iobuf); + iput(&node, ROOTINO + 1); +#endif return (0); } +int +fsinit2(time_t utime) +{ + union dinode node; + + memset(&node, 0, sizeof(node)); + + /* + * Initialize the node. + */ + + node.dp2.di_atime = utime; + node.dp2.di_mtime = utime; + node.dp2.di_ctime = utime; + + /* + * Create the root directory. + */ + + node.dp2.di_mode = IFDIR | UMASK; + node.dp2.di_uid = geteuid(); + node.dp2.di_gid = getegid(); + node.dp2.di_nlink = PREDEFDIR; + node.dp2.di_size = makedir(root_dir, PREDEFDIR); + + node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode); + if (node.dp2.di_db[0] == 0) + return (1); + + node.dp2.di_blocks = btodb(fragroundup(&sblock, node.dp2.di_size)); + + wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, iobuf); + iput(&node, ROOTINO); + +#ifdef notyet + /* + * Create the .snap directory. + */ + + node.dp2.di_mode |= 020; + node.dp2.di_gid = gid; + node.dp2.di_nlink = SNAPLINKCNT; + node.dp2.di_size = makedir(snap_dir, SNAPLINKCNT); + + node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode); + if (node.dp2.di_db[0] == 0) + return (1); + + node.dp2.di_blocks = btodb(fragroundup(&sblock, node.dp2.di_size)); + + wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, iobuf); + iput(&node, ROOTINO + 1); +#endif + return (0); +} + /* * construct a set of directory entries in "buf". * return size of directory. @@ -822,7 +947,7 @@ makedir(struct direct *protodir, int ent int i, spcleft; spcleft = DIRBLKSIZ; - for (cp = buf, i = 0; i < entries - 1; i++) { + for (cp = iobuf, i = 0; i < entries - 1; i++) { protodir[i].d_reclen = DIRSIZ(0, &protodir[i]); memcpy(cp, &protodir[i], protodir[i].d_reclen); cp += protodir[i].d_reclen; @@ -861,19 +986,22 @@ goth: blkno = fragstoblks(&sblock, d); clrblock(&sblock, cg_blksfree(&acg), blkno); acg.cg_cs.cs_nbfree--; - sblock.fs_ffs1_cstotal.cs_nbfree--; + sblock.fs_cstotal.cs_nbfree--; fscs[0].cs_nbfree--; if (mode & IFDIR) { acg.cg_cs.cs_ndir++; - sblock.fs_ffs1_cstotal.cs_ndir++; + sblock.fs_cstotal.cs_ndir++; fscs[0].cs_ndir++; } - cg_blktot(&acg)[cbtocylno(&sblock, d)]--; - cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--; + if (_ffs == 1) { + cg_blktot(&acg)[cbtocylno(&sblock, d)]--; + cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) + [cbtorpos(&sblock, d)]--; + } if (size != sblock.fs_bsize) { frag = howmany(size, sblock.fs_fsize); fscs[0].cs_nffree += sblock.fs_frag - frag; - sblock.fs_ffs1_cstotal.cs_nffree += sblock.fs_frag - frag; + sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag; acg.cg_cs.cs_nffree += sblock.fs_frag - frag; acg.cg_frsum[sblock.fs_frag - frag]++; for (i = frag; i < sblock.fs_frag; i++) @@ -888,31 +1016,43 @@ goth: * Allocate an inode on the disk */ void -iput(struct ufs1_dinode *ip, ino_t ino) +iput(union dinode *ip, ino_t ino) { - struct ufs1_dinode buf[MAXINOPB]; - daddr_t d; + ufs2_daddr_t d; - ip->di_gen = (u_int32_t)arc4random(); + if (_ffs == 2) + ip->dp2.di_gen = (u_int32_t)arc4random(); + else + ip->dp1.di_gen = (u_int32_t)arc4random(); + rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, (char *)&acg); - if (acg.cg_magic != CG_MAGIC) { - errx(41, "cg 0: bad magic number (0x%06x != 0x%06x)", - acg.cg_magic, CG_MAGIC); - } + if (acg.cg_magic != CG_MAGIC) + errx(41, "cg 0: bad magic number"); + acg.cg_cs.cs_nifree--; setbit(cg_inosused(&acg), ino); + wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, (char *)&acg); - sblock.fs_ffs1_cstotal.cs_nifree--; + + sblock.fs_cstotal.cs_nifree--; fscs[0].cs_nifree--; - if (ino >= sblock.fs_ipg * sblock.fs_ncg) { + + if (ino >= sblock.fs_ipg * sblock.fs_ncg) errx(32, "fsinit: inode value %d out of range", ino); - } + d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino)); - rdfs(d, sblock.fs_bsize, buf); - buf[ino_to_fsbo(&sblock, ino)] = *ip; - wtfs(d, sblock.fs_bsize, buf); + rdfs(d, sblock.fs_bsize, iobuf); + + if (_ffs == 2) + ((struct ufs2_dinode *)iobuf)[ino_to_fsbo(&sblock, ino)] = + ip->dp2; + else + ((struct ufs1_dinode *)iobuf)[ino_to_fsbo(&sblock, ino)] = + ip->dp1; + + wtfs(d, sblock.fs_bsize, iobuf); } /* @@ -1060,4 +1200,16 @@ charsperline(void) if (columns == 0) columns = 80; /* last resort */ return columns; +} + +static int +ilog2(int val) +{ + int n; + + for (n = 0; n < sizeof(n) * CHAR_BIT; n++) + if (1 << n == val) + return (n); + + errx(1, "ilog2: %d is not a power of 2\n", val); }