mkfs.c revision 75124
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1980, 1989, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 3537664Scharnier#if 0 3623682Speterstatic char sccsid[] = "@(#)mkfs.c 8.11 (Berkeley) 5/3/95"; 3737664Scharnier#endif 3837664Scharnierstatic const char rcsid[] = 3950476Speter "$FreeBSD: head/sbin/newfs/mkfs.c 75124 2001-04-03 09:35:36Z bde $"; 401558Srgrimes#endif /* not lint */ 411558Srgrimes 4237664Scharnier#include <err.h> 4337664Scharnier#include <signal.h> 4437664Scharnier#include <string.h> 4537664Scharnier#include <stdio.h> 461558Srgrimes#include <unistd.h> 471558Srgrimes#include <sys/param.h> 481558Srgrimes#include <sys/time.h> 4937664Scharnier#include <sys/types.h> 501558Srgrimes#include <sys/wait.h> 511558Srgrimes#include <sys/resource.h> 5243804Sdillon#include <sys/stat.h> 531558Srgrimes#include <ufs/ufs/dinode.h> 541558Srgrimes#include <ufs/ufs/dir.h> 551558Srgrimes#include <ufs/ffs/fs.h> 561558Srgrimes#include <sys/disklabel.h> 573550Sphk#include <sys/file.h> 583550Sphk#include <sys/mman.h> 5913637Sjoerg#include <sys/ioctl.h> 601558Srgrimes 611558Srgrimes#ifndef STANDALONE 6224215Sache#include <stdlib.h> 6337664Scharnier#else 6437664Scharnierextern int atoi __P((char *)); 6537664Scharnierextern char * getenv __P((char *)); 661558Srgrimes#endif 671558Srgrimes 6837664Scharnier#ifdef FSIRAND 6937664Scharnierextern long random __P((void)); 7037664Scharnierextern void srandomdev __P((void)); 7137664Scharnier#endif 7237664Scharnier 731558Srgrimes/* 741558Srgrimes * make file system for cylinder-group style file systems 751558Srgrimes */ 761558Srgrimes 771558Srgrimes/* 781558Srgrimes * We limit the size of the inode map to be no more than a 791558Srgrimes * third of the cylinder group space, since we must leave at 801558Srgrimes * least an equal amount of space for the block map. 811558Srgrimes * 821558Srgrimes * N.B.: MAXIPG must be a multiple of INOPB(fs). 831558Srgrimes */ 841558Srgrimes#define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs)) 851558Srgrimes 861558Srgrimes#define UMASK 0755 871558Srgrimes#define MAXINOPB (MAXBSIZE / sizeof(struct dinode)) 881558Srgrimes#define POWEROF2(num) (((num) & ((num) - 1)) == 0) 891558Srgrimes 901558Srgrimes/* 911558Srgrimes * variables set up by front end. 921558Srgrimes */ 931558Srgrimesextern int mfs; /* run as the memory based filesystem */ 9443804Sdillonextern char *mfs_mtpt; /* mount point for mfs */ 9543804Sdillonextern struct stat mfs_mtstat; /* stat prior to mount */ 961558Srgrimesextern int Nflag; /* run mkfs without writing file system */ 971558Srgrimesextern int Oflag; /* format as an 4.3BSD file system */ 9875078Sobrienextern int Uflag; /* enable soft updates for file system */ 991558Srgrimesextern int fssize; /* file system size */ 1001558Srgrimesextern int ntracks; /* # tracks/cylinder */ 1011558Srgrimesextern int nsectors; /* # sectors/track */ 1021558Srgrimesextern int nphyssectors; /* # sectors/track including spares */ 1031558Srgrimesextern int secpercyl; /* sectors per cylinder */ 1041558Srgrimesextern int sectorsize; /* bytes/sector */ 10520061Ssosextern int realsectorsize; /* bytes/sector in hardware*/ 1061558Srgrimesextern int rpm; /* revolutions/minute of drive */ 1071558Srgrimesextern int interleave; /* hardware sector interleave */ 1081558Srgrimesextern int trackskew; /* sector 0 skew, per track */ 1091558Srgrimesextern int fsize; /* fragment size */ 1101558Srgrimesextern int bsize; /* block size */ 1111558Srgrimesextern int cpg; /* cylinders/cylinder group */ 1121558Srgrimesextern int cpgflg; /* cylinders/cylinder group flag was given */ 1131558Srgrimesextern int minfree; /* free space threshold */ 1141558Srgrimesextern int opt; /* optimization preference (space or time) */ 1151558Srgrimesextern int density; /* number of bytes per inode */ 1161558Srgrimesextern int maxcontig; /* max contiguous blocks to allocate */ 1171558Srgrimesextern int rotdelay; /* rotational delay between blocks */ 1181558Srgrimesextern int maxbpg; /* maximum blocks per file in a cyl group */ 1191558Srgrimesextern int nrpos; /* # of distinguished rotational positions */ 1201558Srgrimesextern int bbsize; /* boot block size */ 1211558Srgrimesextern int sbsize; /* superblock size */ 1221558Srgrimesextern u_long memleft; /* virtual memory available */ 1231558Srgrimesextern caddr_t membase; /* start address of memory based filesystem */ 1243550Sphkextern char * filename; 1251558Srgrimes 1261558Srgrimesunion { 1271558Srgrimes struct fs fs; 1281558Srgrimes char pad[SBSIZE]; 1291558Srgrimes} fsun; 1301558Srgrimes#define sblock fsun.fs 1311558Srgrimesstruct csum *fscs; 1321558Srgrimes 1331558Srgrimesunion { 1341558Srgrimes struct cg cg; 1351558Srgrimes char pad[MAXBSIZE]; 1361558Srgrimes} cgun; 1371558Srgrimes#define acg cgun.cg 1381558Srgrimes 1391558Srgrimesstruct dinode zino[MAXBSIZE / sizeof(struct dinode)]; 1401558Srgrimes 14124215Sacheint fsi, fso; 14224149Sguido#ifdef FSIRAND 14324215Sacheint randinit; 14424149Sguido#endif 1451558Srgrimesdaddr_t alloc(); 14627372Sbdelong calcipg(); 14713769Sjoergstatic int charsperline(); 14837664Scharniervoid clrblock __P((struct fs *, unsigned char *, int)); 14937664Scharniervoid fsinit __P((time_t)); 15037664Scharniervoid initcg __P((int, time_t)); 15137664Scharnierint isblock __P((struct fs *, unsigned char *, int)); 15237664Scharniervoid iput __P((struct dinode *, ino_t)); 15337664Scharnierint makedir __P((struct direct *, int)); 15437664Scharniervoid rdfs __P((daddr_t, int, char *)); 15537664Scharniervoid setblock __P((struct fs *, unsigned char *, int)); 15637664Scharniervoid wtfs __P((daddr_t, int, char *)); 15767478Sjwdvoid wtfsflush __P((void)); 1581558Srgrimes 15937664Scharnier#ifndef STANDALONE 16037664Scharniervoid get_memleft __P((void)); 16137664Scharniervoid raise_data_limit __P((void)); 16237664Scharnier#else 16337664Scharniervoid free __P((char *)); 16437664Scharnierchar * calloc __P((u_long, u_long)); 16537664Scharniercaddr_t malloc __P((u_long)); 16637664Scharniercaddr_t realloc __P((char *, u_long)); 16737664Scharnier#endif 16837664Scharnier 16943804Sdillonint mfs_ppid = 0; 17043804Sdillon 17137664Scharniervoid 1721558Srgrimesmkfs(pp, fsys, fi, fo) 1731558Srgrimes struct partition *pp; 1741558Srgrimes char *fsys; 1751558Srgrimes int fi, fo; 1761558Srgrimes{ 1771558Srgrimes register long i, mincpc, mincpg, inospercg; 1781558Srgrimes long cylno, rpos, blk, j, warn = 0; 1791558Srgrimes long used, mincpgcnt, bpcg; 18023682Speter off_t usedb; 1811558Srgrimes long mapcramped, inodecramped; 1821558Srgrimes long postblsize, rotblsize, totalsbsize; 18343804Sdillon int status, fd; 1841558Srgrimes time_t utime; 1851558Srgrimes quad_t sizepb; 1861558Srgrimes void started(); 18713769Sjoerg int width; 18813769Sjoerg char tmpbuf[100]; /* XXX this will break in about 2,500 years */ 1891558Srgrimes 1901558Srgrimes#ifndef STANDALONE 1911558Srgrimes time(&utime); 1921558Srgrimes#endif 19324215Sache#ifdef FSIRAND 19424215Sache if (!randinit) { 19524215Sache randinit = 1; 19626625Sache srandomdev(); 19724215Sache } 19824215Sache#endif 1991558Srgrimes if (mfs) { 20043804Sdillon mfs_ppid = getpid(); 2011558Srgrimes (void) signal(SIGUSR1, started); 20237664Scharnier if ((i = fork())) { 20337664Scharnier if (i == -1) 20437664Scharnier err(10, "mfs"); 2051558Srgrimes if (waitpid(i, &status, 0) != -1 && WIFEXITED(status)) 2061558Srgrimes exit(WEXITSTATUS(status)); 2071558Srgrimes exit(11); 2081558Srgrimes /* NOTREACHED */ 2091558Srgrimes } 21024215Sache#ifdef STANDALONE 2111558Srgrimes (void)malloc(0); 21224215Sache#else 21324215Sache raise_data_limit(); 21424215Sache#endif 2153550Sphk if(filename) { 2163550Sphk unsigned char buf[BUFSIZ]; 2173550Sphk unsigned long l,l1; 2183550Sphk fd = open(filename,O_RDWR|O_TRUNC|O_CREAT,0644); 21937664Scharnier if(fd < 0) 22037664Scharnier err(12, "%s", filename); 2213550Sphk for(l=0;l< fssize * sectorsize;l += l1) { 2223550Sphk l1 = fssize * sectorsize; 2233550Sphk if (BUFSIZ < l1) 2243550Sphk l1 = BUFSIZ; 22537664Scharnier if (l1 != write(fd,buf,l1)) 22637664Scharnier err(12, "%s", filename); 2273550Sphk } 2283550Sphk membase = mmap( 2293550Sphk 0, 2303550Sphk fssize * sectorsize, 2313550Sphk PROT_READ|PROT_WRITE, 2323550Sphk MAP_SHARED, 2333550Sphk fd, 2343550Sphk 0); 23537664Scharnier if(membase == MAP_FAILED) 23637664Scharnier err(12, "mmap"); 2373550Sphk close(fd); 2383550Sphk } else { 23924457Speter#ifndef STANDALONE 24024457Speter get_memleft(); 24124457Speter#endif 24229321Speter if (fssize * sectorsize > (memleft - 131072)) 24329321Speter fssize = (memleft - 131072) / sectorsize; 24437664Scharnier if ((membase = malloc(fssize * sectorsize)) == NULL) 24537664Scharnier errx(13, "malloc failed"); 2463550Sphk } 2471558Srgrimes } 2481558Srgrimes fsi = fi; 2491558Srgrimes fso = fo; 2501558Srgrimes if (Oflag) { 2511558Srgrimes sblock.fs_inodefmt = FS_42INODEFMT; 2521558Srgrimes sblock.fs_maxsymlinklen = 0; 2531558Srgrimes } else { 2541558Srgrimes sblock.fs_inodefmt = FS_44INODEFMT; 2551558Srgrimes sblock.fs_maxsymlinklen = MAXSYMLINKLEN; 2561558Srgrimes } 25775124Sbde if (Uflag) 25875078Sobrien sblock.fs_flags |= FS_DOSOFTDEP; 2591558Srgrimes /* 2601558Srgrimes * Validate the given file system size. 2611558Srgrimes * Verify that its last block can actually be accessed. 2621558Srgrimes */ 2631558Srgrimes if (fssize <= 0) 2641558Srgrimes printf("preposterous size %d\n", fssize), exit(13); 26520061Ssos wtfs(fssize - (realsectorsize / DEV_BSIZE), realsectorsize, 26620061Ssos (char *)&sblock); 2671558Srgrimes /* 2681558Srgrimes * collect and verify the sector and track info 2691558Srgrimes */ 2701558Srgrimes sblock.fs_nsect = nsectors; 2711558Srgrimes sblock.fs_ntrak = ntracks; 2721558Srgrimes if (sblock.fs_ntrak <= 0) 2731558Srgrimes printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14); 2741558Srgrimes if (sblock.fs_nsect <= 0) 2751558Srgrimes printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15); 2761558Srgrimes /* 2771558Srgrimes * collect and verify the block and fragment sizes 2781558Srgrimes */ 2791558Srgrimes sblock.fs_bsize = bsize; 2801558Srgrimes sblock.fs_fsize = fsize; 2811558Srgrimes if (!POWEROF2(sblock.fs_bsize)) { 2821558Srgrimes printf("block size must be a power of 2, not %d\n", 2831558Srgrimes sblock.fs_bsize); 2841558Srgrimes exit(16); 2851558Srgrimes } 2861558Srgrimes if (!POWEROF2(sblock.fs_fsize)) { 2871558Srgrimes printf("fragment size must be a power of 2, not %d\n", 2881558Srgrimes sblock.fs_fsize); 2891558Srgrimes exit(17); 2901558Srgrimes } 2911558Srgrimes if (sblock.fs_fsize < sectorsize) { 2921558Srgrimes printf("fragment size %d is too small, minimum is %d\n", 2931558Srgrimes sblock.fs_fsize, sectorsize); 2941558Srgrimes exit(18); 2951558Srgrimes } 2961558Srgrimes if (sblock.fs_bsize < MINBSIZE) { 2971558Srgrimes printf("block size %d is too small, minimum is %d\n", 2981558Srgrimes sblock.fs_bsize, MINBSIZE); 2991558Srgrimes exit(19); 3001558Srgrimes } 3011558Srgrimes if (sblock.fs_bsize < sblock.fs_fsize) { 3021558Srgrimes printf("block size (%d) cannot be smaller than fragment size (%d)\n", 3031558Srgrimes sblock.fs_bsize, sblock.fs_fsize); 3041558Srgrimes exit(20); 3051558Srgrimes } 3061558Srgrimes sblock.fs_bmask = ~(sblock.fs_bsize - 1); 3071558Srgrimes sblock.fs_fmask = ~(sblock.fs_fsize - 1); 3081558Srgrimes sblock.fs_qbmask = ~sblock.fs_bmask; 3091558Srgrimes sblock.fs_qfmask = ~sblock.fs_fmask; 3101558Srgrimes for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1) 3111558Srgrimes sblock.fs_bshift++; 3121558Srgrimes for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1) 3131558Srgrimes sblock.fs_fshift++; 3141558Srgrimes sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize); 3151558Srgrimes for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1) 3161558Srgrimes sblock.fs_fragshift++; 3171558Srgrimes if (sblock.fs_frag > MAXFRAG) { 3181558Srgrimes printf("fragment size %d is too small, minimum with block size %d is %d\n", 3191558Srgrimes sblock.fs_fsize, sblock.fs_bsize, 3201558Srgrimes sblock.fs_bsize / MAXFRAG); 3211558Srgrimes exit(21); 3221558Srgrimes } 3231558Srgrimes sblock.fs_nrpos = nrpos; 3241558Srgrimes sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t); 3251558Srgrimes sblock.fs_inopb = sblock.fs_bsize / sizeof(struct dinode); 3261558Srgrimes sblock.fs_nspf = sblock.fs_fsize / sectorsize; 3271558Srgrimes for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1) 3281558Srgrimes sblock.fs_fsbtodb++; 3291558Srgrimes sblock.fs_sblkno = 3301558Srgrimes roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag); 3311558Srgrimes sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno + 3321558Srgrimes roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag)); 3331558Srgrimes sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag; 3341558Srgrimes sblock.fs_cgoffset = roundup( 3351558Srgrimes howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag); 3361558Srgrimes for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1) 3371558Srgrimes sblock.fs_cgmask <<= 1; 3381558Srgrimes if (!POWEROF2(sblock.fs_ntrak)) 3391558Srgrimes sblock.fs_cgmask <<= 1; 3401558Srgrimes sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1; 3411558Srgrimes for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) { 3421558Srgrimes sizepb *= NINDIR(&sblock); 3431558Srgrimes sblock.fs_maxfilesize += sizepb; 3441558Srgrimes } 3451558Srgrimes /* 3461558Srgrimes * Validate specified/determined secpercyl 3471558Srgrimes * and calculate minimum cylinders per group. 3481558Srgrimes */ 3491558Srgrimes sblock.fs_spc = secpercyl; 3501558Srgrimes for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc; 3511558Srgrimes sblock.fs_cpc > 1 && (i & 1) == 0; 3521558Srgrimes sblock.fs_cpc >>= 1, i >>= 1) 3531558Srgrimes /* void */; 3541558Srgrimes mincpc = sblock.fs_cpc; 3551558Srgrimes bpcg = sblock.fs_spc * sectorsize; 3561558Srgrimes inospercg = roundup(bpcg / sizeof(struct dinode), INOPB(&sblock)); 3571558Srgrimes if (inospercg > MAXIPG(&sblock)) 3581558Srgrimes inospercg = MAXIPG(&sblock); 3591558Srgrimes used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock); 3601558Srgrimes mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used, 3611558Srgrimes sblock.fs_spc); 3621558Srgrimes mincpg = roundup(mincpgcnt, mincpc); 3631558Srgrimes /* 3641558Srgrimes * Ensure that cylinder group with mincpg has enough space 3651558Srgrimes * for block maps. 3661558Srgrimes */ 3671558Srgrimes sblock.fs_cpg = mincpg; 3681558Srgrimes sblock.fs_ipg = inospercg; 3691558Srgrimes if (maxcontig > 1) 3701558Srgrimes sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG); 3711558Srgrimes mapcramped = 0; 3721558Srgrimes while (CGSIZE(&sblock) > sblock.fs_bsize) { 3731558Srgrimes mapcramped = 1; 3741558Srgrimes if (sblock.fs_bsize < MAXBSIZE) { 3751558Srgrimes sblock.fs_bsize <<= 1; 3761558Srgrimes if ((i & 1) == 0) { 3771558Srgrimes i >>= 1; 3781558Srgrimes } else { 3791558Srgrimes sblock.fs_cpc <<= 1; 3801558Srgrimes mincpc <<= 1; 3811558Srgrimes mincpg = roundup(mincpgcnt, mincpc); 3821558Srgrimes sblock.fs_cpg = mincpg; 3831558Srgrimes } 3841558Srgrimes sblock.fs_frag <<= 1; 3851558Srgrimes sblock.fs_fragshift += 1; 3861558Srgrimes if (sblock.fs_frag <= MAXFRAG) 3871558Srgrimes continue; 3881558Srgrimes } 3891558Srgrimes if (sblock.fs_fsize == sblock.fs_bsize) { 3901558Srgrimes printf("There is no block size that"); 3911558Srgrimes printf(" can support this disk\n"); 3921558Srgrimes exit(22); 3931558Srgrimes } 3941558Srgrimes sblock.fs_frag >>= 1; 3951558Srgrimes sblock.fs_fragshift -= 1; 3961558Srgrimes sblock.fs_fsize <<= 1; 3971558Srgrimes sblock.fs_nspf <<= 1; 3981558Srgrimes } 3991558Srgrimes /* 4001558Srgrimes * Ensure that cylinder group with mincpg has enough space for inodes. 4011558Srgrimes */ 4021558Srgrimes inodecramped = 0; 40323682Speter inospercg = calcipg(mincpg, bpcg, &usedb); 4041558Srgrimes sblock.fs_ipg = inospercg; 4051558Srgrimes while (inospercg > MAXIPG(&sblock)) { 4061558Srgrimes inodecramped = 1; 4071558Srgrimes if (mincpc == 1 || sblock.fs_frag == 1 || 4081558Srgrimes sblock.fs_bsize == MINBSIZE) 4091558Srgrimes break; 4101558Srgrimes printf("With a block size of %d %s %d\n", sblock.fs_bsize, 41123682Speter "minimum bytes per inode is", 41223682Speter (int)((mincpg * (off_t)bpcg - usedb) 41323682Speter / MAXIPG(&sblock) + 1)); 4141558Srgrimes sblock.fs_bsize >>= 1; 4151558Srgrimes sblock.fs_frag >>= 1; 4161558Srgrimes sblock.fs_fragshift -= 1; 4171558Srgrimes mincpc >>= 1; 4181558Srgrimes sblock.fs_cpg = roundup(mincpgcnt, mincpc); 4191558Srgrimes if (CGSIZE(&sblock) > sblock.fs_bsize) { 4201558Srgrimes sblock.fs_bsize <<= 1; 4211558Srgrimes break; 4221558Srgrimes } 4231558Srgrimes mincpg = sblock.fs_cpg; 42423682Speter inospercg = calcipg(mincpg, bpcg, &usedb); 4251558Srgrimes sblock.fs_ipg = inospercg; 4261558Srgrimes } 4271558Srgrimes if (inodecramped) { 4281558Srgrimes if (inospercg > MAXIPG(&sblock)) { 4291558Srgrimes printf("Minimum bytes per inode is %d\n", 43023682Speter (int)((mincpg * (off_t)bpcg - usedb) 43123682Speter / MAXIPG(&sblock) + 1)); 4321558Srgrimes } else if (!mapcramped) { 4331558Srgrimes printf("With %d bytes per inode, ", density); 43437239Sbde printf("minimum cylinders per group is %ld\n", mincpg); 4351558Srgrimes } 4361558Srgrimes } 4371558Srgrimes if (mapcramped) { 4381558Srgrimes printf("With %d sectors per cylinder, ", sblock.fs_spc); 43937239Sbde printf("minimum cylinders per group is %ld\n", mincpg); 4401558Srgrimes } 4411558Srgrimes if (inodecramped || mapcramped) { 4421558Srgrimes if (sblock.fs_bsize != bsize) 4431558Srgrimes printf("%s to be changed from %d to %d\n", 4441558Srgrimes "This requires the block size", 4451558Srgrimes bsize, sblock.fs_bsize); 4461558Srgrimes if (sblock.fs_fsize != fsize) 4471558Srgrimes printf("\t%s to be changed from %d to %d\n", 4481558Srgrimes "and the fragment size", 4491558Srgrimes fsize, sblock.fs_fsize); 4501558Srgrimes exit(23); 4511558Srgrimes } 4528871Srgrimes /* 4531558Srgrimes * Calculate the number of cylinders per group 4541558Srgrimes */ 4551558Srgrimes sblock.fs_cpg = cpg; 4561558Srgrimes if (sblock.fs_cpg % mincpc != 0) { 45737239Sbde printf("%s groups must have a multiple of %ld cylinders\n", 4581558Srgrimes cpgflg ? "Cylinder" : "Warning: cylinder", mincpc); 4591558Srgrimes sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc); 4601558Srgrimes if (!cpgflg) 4611558Srgrimes cpg = sblock.fs_cpg; 4621558Srgrimes } 4631558Srgrimes /* 4641558Srgrimes * Must ensure there is enough space for inodes. 4651558Srgrimes */ 46623682Speter sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb); 4671558Srgrimes while (sblock.fs_ipg > MAXIPG(&sblock)) { 4681558Srgrimes inodecramped = 1; 4691558Srgrimes sblock.fs_cpg -= mincpc; 47023682Speter sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb); 4711558Srgrimes } 4721558Srgrimes /* 4731558Srgrimes * Must ensure there is enough space to hold block map. 4741558Srgrimes */ 4751558Srgrimes while (CGSIZE(&sblock) > sblock.fs_bsize) { 4761558Srgrimes mapcramped = 1; 4771558Srgrimes sblock.fs_cpg -= mincpc; 47823682Speter sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb); 4791558Srgrimes } 4801558Srgrimes sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock); 4811558Srgrimes if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) { 48237239Sbde printf("panic (fs_cpg * fs_spc) %% NSPF != 0"); 4831558Srgrimes exit(24); 4841558Srgrimes } 4851558Srgrimes if (sblock.fs_cpg < mincpg) { 48637239Sbde printf("cylinder groups must have at least %ld cylinders\n", 4871558Srgrimes mincpg); 4881558Srgrimes exit(25); 4891558Srgrimes } else if (sblock.fs_cpg != cpg) { 4901558Srgrimes if (!cpgflg) 4911558Srgrimes printf("Warning: "); 4921558Srgrimes else if (!mapcramped && !inodecramped) 4931558Srgrimes exit(26); 4941558Srgrimes if (mapcramped && inodecramped) 4951558Srgrimes printf("Block size and bytes per inode restrict"); 4961558Srgrimes else if (mapcramped) 4971558Srgrimes printf("Block size restricts"); 4981558Srgrimes else 4991558Srgrimes printf("Bytes per inode restrict"); 5001558Srgrimes printf(" cylinders per group to %d.\n", sblock.fs_cpg); 5011558Srgrimes if (cpgflg) 5021558Srgrimes exit(27); 5031558Srgrimes } 5041558Srgrimes sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock)); 5051558Srgrimes /* 5061558Srgrimes * Now have size for file system and nsect and ntrak. 5071558Srgrimes * Determine number of cylinders and blocks in the file system. 5081558Srgrimes */ 5091558Srgrimes sblock.fs_size = fssize = dbtofsb(&sblock, fssize); 5101558Srgrimes sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc; 5111558Srgrimes if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) { 5121558Srgrimes sblock.fs_ncyl++; 5131558Srgrimes warn = 1; 5141558Srgrimes } 5151558Srgrimes if (sblock.fs_ncyl < 1) { 5161558Srgrimes printf("file systems must have at least one cylinder\n"); 5171558Srgrimes exit(28); 5181558Srgrimes } 5191558Srgrimes /* 5201558Srgrimes * Determine feasability/values of rotational layout tables. 5211558Srgrimes * 5221558Srgrimes * The size of the rotational layout tables is limited by the 5231558Srgrimes * size of the superblock, SBSIZE. The amount of space available 5241558Srgrimes * for tables is calculated as (SBSIZE - sizeof (struct fs)). 5251558Srgrimes * The size of these tables is inversely proportional to the block 5261558Srgrimes * size of the file system. The size increases if sectors per track 5271558Srgrimes * are not powers of two, because more cylinders must be described 5281558Srgrimes * by the tables before the rotational pattern repeats (fs_cpc). 5291558Srgrimes */ 5301558Srgrimes sblock.fs_interleave = interleave; 5311558Srgrimes sblock.fs_trackskew = trackskew; 5321558Srgrimes sblock.fs_npsect = nphyssectors; 5331558Srgrimes sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 5341558Srgrimes sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs)); 53532623Sbde if (sblock.fs_sbsize > SBSIZE) 53632623Sbde sblock.fs_sbsize = SBSIZE; 5373467Sdg if (sblock.fs_ntrak == 1) { 5381558Srgrimes sblock.fs_cpc = 0; 5391558Srgrimes goto next; 5401558Srgrimes } 54138579Sdfr postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(int16_t); 5421558Srgrimes rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock); 5431558Srgrimes totalsbsize = sizeof(struct fs) + rotblsize; 5441558Srgrimes if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) { 5451558Srgrimes /* use old static table space */ 5461558Srgrimes sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) - 54723682Speter (char *)(&sblock.fs_firstfield); 5481558Srgrimes sblock.fs_rotbloff = &sblock.fs_space[0] - 54923682Speter (u_char *)(&sblock.fs_firstfield); 5501558Srgrimes } else { 5511558Srgrimes /* use dynamic table space */ 5521558Srgrimes sblock.fs_postbloff = &sblock.fs_space[0] - 55323682Speter (u_char *)(&sblock.fs_firstfield); 5541558Srgrimes sblock.fs_rotbloff = sblock.fs_postbloff + postblsize; 5551558Srgrimes totalsbsize += postblsize; 5561558Srgrimes } 5571558Srgrimes if (totalsbsize > SBSIZE || 5581558Srgrimes sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) { 5591558Srgrimes printf("%s %s %d %s %d.%s", 5601558Srgrimes "Warning: insufficient space in super block for\n", 5611558Srgrimes "rotational layout tables with nsect", sblock.fs_nsect, 5621558Srgrimes "and ntrak", sblock.fs_ntrak, 5631558Srgrimes "\nFile system performance may be impaired.\n"); 5641558Srgrimes sblock.fs_cpc = 0; 5651558Srgrimes goto next; 5661558Srgrimes } 5671558Srgrimes sblock.fs_sbsize = fragroundup(&sblock, totalsbsize); 56832623Sbde if (sblock.fs_sbsize > SBSIZE) 56932623Sbde sblock.fs_sbsize = SBSIZE; 5701558Srgrimes /* 5711558Srgrimes * calculate the available blocks for each rotational position 5721558Srgrimes */ 5731558Srgrimes for (cylno = 0; cylno < sblock.fs_cpc; cylno++) 5741558Srgrimes for (rpos = 0; rpos < sblock.fs_nrpos; rpos++) 5751558Srgrimes fs_postbl(&sblock, cylno)[rpos] = -1; 5761558Srgrimes for (i = (rotblsize - 1) * sblock.fs_frag; 5771558Srgrimes i >= 0; i -= sblock.fs_frag) { 5781558Srgrimes cylno = cbtocylno(&sblock, i); 5791558Srgrimes rpos = cbtorpos(&sblock, i); 5801558Srgrimes blk = fragstoblks(&sblock, i); 5811558Srgrimes if (fs_postbl(&sblock, cylno)[rpos] == -1) 5821558Srgrimes fs_rotbl(&sblock)[blk] = 0; 5831558Srgrimes else 5841558Srgrimes fs_rotbl(&sblock)[blk] = 5851558Srgrimes fs_postbl(&sblock, cylno)[rpos] - blk; 5861558Srgrimes fs_postbl(&sblock, cylno)[rpos] = blk; 5871558Srgrimes } 5881558Srgrimesnext: 5891558Srgrimes /* 5901558Srgrimes * Compute/validate number of cylinder groups. 5911558Srgrimes */ 5921558Srgrimes sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg; 5931558Srgrimes if (sblock.fs_ncyl % sblock.fs_cpg) 5941558Srgrimes sblock.fs_ncg++; 5951558Srgrimes sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock); 5961558Srgrimes i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1); 5971558Srgrimes if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) { 59837239Sbde printf("inode blocks/cyl group (%ld) >= data blocks (%ld)\n", 5991558Srgrimes cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag, 60037239Sbde (long)(sblock.fs_fpg / sblock.fs_frag)); 6011558Srgrimes printf("number of cylinders per cylinder group (%d) %s.\n", 6021558Srgrimes sblock.fs_cpg, "must be increased"); 6031558Srgrimes exit(29); 6041558Srgrimes } 6051558Srgrimes j = sblock.fs_ncg - 1; 6061558Srgrimes if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg && 6071558Srgrimes cgdmin(&sblock, j) - cgbase(&sblock, j) > i) { 6081558Srgrimes if (j == 0) { 6091558Srgrimes printf("Filesystem must have at least %d sectors\n", 6101558Srgrimes NSPF(&sblock) * 6111558Srgrimes (cgdmin(&sblock, 0) + 3 * sblock.fs_frag)); 6121558Srgrimes exit(30); 6131558Srgrimes } 61437239Sbde printf( 61537239Sbde"Warning: inode blocks/cyl group (%ld) >= data blocks (%ld) in last\n", 6161558Srgrimes (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag, 6171558Srgrimes i / sblock.fs_frag); 61837239Sbde printf( 61937239Sbde" cylinder group. This implies %ld sector(s) cannot be allocated.\n", 6201558Srgrimes i * NSPF(&sblock)); 6211558Srgrimes sblock.fs_ncg--; 6221558Srgrimes sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg; 6231558Srgrimes sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc / 6241558Srgrimes NSPF(&sblock); 6251558Srgrimes warn = 0; 6261558Srgrimes } 6271558Srgrimes if (warn && !mfs) { 6281558Srgrimes printf("Warning: %d sector(s) in last cylinder unallocated\n", 6291558Srgrimes sblock.fs_spc - 6301558Srgrimes (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1) 6311558Srgrimes * sblock.fs_spc)); 6321558Srgrimes } 6331558Srgrimes /* 6341558Srgrimes * fill in remaining fields of the super block 6351558Srgrimes */ 6361558Srgrimes sblock.fs_csaddr = cgdmin(&sblock, 0); 6371558Srgrimes sblock.fs_cssize = 6381558Srgrimes fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum)); 63971073Siedowse /* 64071073Siedowse * The superblock fields 'fs_csmask' and 'fs_csshift' are no 64171073Siedowse * longer used. However, we still initialise them so that the 64271073Siedowse * filesystem remains compatible with old kernels. 64371073Siedowse */ 6441558Srgrimes i = sblock.fs_bsize / sizeof(struct csum); 6451558Srgrimes sblock.fs_csmask = ~(i - 1); 6461558Srgrimes for (sblock.fs_csshift = 0; i > 1; i >>= 1) 6471558Srgrimes sblock.fs_csshift++; 6481558Srgrimes fscs = (struct csum *)calloc(1, sblock.fs_cssize); 64937664Scharnier if (fscs == NULL) 65037664Scharnier errx(31, "calloc failed"); 6511558Srgrimes sblock.fs_magic = FS_MAGIC; 6521558Srgrimes sblock.fs_rotdelay = rotdelay; 6531558Srgrimes sblock.fs_minfree = minfree; 6541558Srgrimes sblock.fs_maxcontig = maxcontig; 6551558Srgrimes sblock.fs_maxbpg = maxbpg; 6561558Srgrimes sblock.fs_rps = rpm / 60; 6571558Srgrimes sblock.fs_optim = opt; 6581558Srgrimes sblock.fs_cgrotor = 0; 6591558Srgrimes sblock.fs_cstotal.cs_ndir = 0; 6601558Srgrimes sblock.fs_cstotal.cs_nbfree = 0; 6611558Srgrimes sblock.fs_cstotal.cs_nifree = 0; 6621558Srgrimes sblock.fs_cstotal.cs_nffree = 0; 6631558Srgrimes sblock.fs_fmod = 0; 6641558Srgrimes sblock.fs_ronly = 0; 6652294Sdg sblock.fs_clean = 1; 66624149Sguido#ifdef FSIRAND 66724149Sguido sblock.fs_id[0] = (long)utime; 66824215Sache sblock.fs_id[1] = random(); 66924149Sguido#endif 67024149Sguido 6711558Srgrimes /* 6721558Srgrimes * Dump out summary information about file system. 6731558Srgrimes */ 6741558Srgrimes if (!mfs) { 6751558Srgrimes printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n", 6761558Srgrimes fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl, 6771558Srgrimes "cylinders", sblock.fs_ntrak, sblock.fs_nsect); 6781558Srgrimes#define B2MBFACTOR (1 / (1024.0 * 1024.0)) 67975124Sbde printf( 68075124Sbde "\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)%s\n", 6811558Srgrimes (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR, 6821558Srgrimes sblock.fs_ncg, sblock.fs_cpg, 6831558Srgrimes (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR, 68475078Sobrien sblock.fs_ipg, 68575124Sbde sblock.fs_flags & FS_DOSOFTDEP ? " SOFTUPDATES" : ""); 6861558Srgrimes#undef B2MBFACTOR 6871558Srgrimes } 6881558Srgrimes /* 6891558Srgrimes * Now build the cylinders group blocks and 6901558Srgrimes * then print out indices of cylinder groups. 6911558Srgrimes */ 6921558Srgrimes if (!mfs) 69313769Sjoerg printf("super-block backups (for fsck -b #) at:\n"); 69413769Sjoerg i = 0; 69513769Sjoerg width = charsperline(); 6961558Srgrimes for (cylno = 0; cylno < sblock.fs_ncg; cylno++) { 6971558Srgrimes initcg(cylno, utime); 6981558Srgrimes if (mfs) 6991558Srgrimes continue; 70050134Sbillf j = sprintf(tmpbuf, " %ld%s", 70150134Sbillf fsbtodb(&sblock, cgsblock(&sblock, cylno)), 70250134Sbillf cylno < (sblock.fs_ncg-1) ? "," : "" ); 70337239Sbde if (i + j >= width) { 7041558Srgrimes printf("\n"); 70513769Sjoerg i = 0; 70613769Sjoerg } 70713769Sjoerg i += j; 70813769Sjoerg printf("%s", tmpbuf); 7098218Sdg fflush(stdout); 7101558Srgrimes } 7111558Srgrimes if (!mfs) 7121558Srgrimes printf("\n"); 7131558Srgrimes if (Nflag && !mfs) 7141558Srgrimes exit(0); 7151558Srgrimes /* 7161558Srgrimes * Now construct the initial file system, 7171558Srgrimes * then write out the super-block. 7181558Srgrimes */ 7191558Srgrimes fsinit(utime); 7201558Srgrimes sblock.fs_time = utime; 7211558Srgrimes wtfs((int)SBOFF / sectorsize, sbsize, (char *)&sblock); 7221558Srgrimes for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) 7231558Srgrimes wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)), 7241558Srgrimes sblock.fs_cssize - i < sblock.fs_bsize ? 7251558Srgrimes sblock.fs_cssize - i : sblock.fs_bsize, 7261558Srgrimes ((char *)fscs) + i); 7278871Srgrimes /* 7281558Srgrimes * Write out the duplicate super blocks 7291558Srgrimes */ 7301558Srgrimes for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 7311558Srgrimes wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), 7321558Srgrimes sbsize, (char *)&sblock); 73367478Sjwd wtfsflush(); 7341558Srgrimes /* 7351558Srgrimes * Update information about this partion in pack 7361558Srgrimes * label, to that it may be updated on disk. 7371558Srgrimes */ 7381558Srgrimes pp->p_fstype = FS_BSDFFS; 7391558Srgrimes pp->p_fsize = sblock.fs_fsize; 7401558Srgrimes pp->p_frag = sblock.fs_frag; 7411558Srgrimes pp->p_cpg = sblock.fs_cpg; 7421558Srgrimes /* 7431558Srgrimes * Notify parent process of success. 7441558Srgrimes * Dissociate from session and tty. 7451558Srgrimes */ 7461558Srgrimes if (mfs) { 74743804Sdillon kill(mfs_ppid, SIGUSR1); 7481558Srgrimes (void) setsid(); 7491558Srgrimes (void) close(0); 7501558Srgrimes (void) close(1); 7511558Srgrimes (void) close(2); 7521558Srgrimes (void) chdir("/"); 7531558Srgrimes } 7541558Srgrimes} 7551558Srgrimes 7561558Srgrimes/* 7571558Srgrimes * Initialize a cylinder group. 7581558Srgrimes */ 75937664Scharniervoid 7601558Srgrimesinitcg(cylno, utime) 7611558Srgrimes int cylno; 7621558Srgrimes time_t utime; 7631558Srgrimes{ 7641558Srgrimes daddr_t cbase, d, dlower, dupper, dmax, blkno; 76537664Scharnier long i; 7661558Srgrimes register struct csum *cs; 76737664Scharnier#ifdef FSIRAND 76837664Scharnier long j; 76937664Scharnier#endif 7701558Srgrimes 7711558Srgrimes /* 7721558Srgrimes * Determine block bounds for cylinder group. 7731558Srgrimes * Allow space for super block summary information in first 7741558Srgrimes * cylinder group. 7751558Srgrimes */ 7761558Srgrimes cbase = cgbase(&sblock, cylno); 7771558Srgrimes dmax = cbase + sblock.fs_fpg; 7781558Srgrimes if (dmax > sblock.fs_size) 7791558Srgrimes dmax = sblock.fs_size; 7801558Srgrimes dlower = cgsblock(&sblock, cylno) - cbase; 7811558Srgrimes dupper = cgdmin(&sblock, cylno) - cbase; 7821558Srgrimes if (cylno == 0) 7831558Srgrimes dupper += howmany(sblock.fs_cssize, sblock.fs_fsize); 7841558Srgrimes cs = fscs + cylno; 78523682Speter memset(&acg, 0, sblock.fs_cgsize); 7861558Srgrimes acg.cg_time = utime; 7871558Srgrimes acg.cg_magic = CG_MAGIC; 7881558Srgrimes acg.cg_cgx = cylno; 7891558Srgrimes if (cylno == sblock.fs_ncg - 1) 7901558Srgrimes acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg; 7911558Srgrimes else 7921558Srgrimes acg.cg_ncyl = sblock.fs_cpg; 7931558Srgrimes acg.cg_niblk = sblock.fs_ipg; 7941558Srgrimes acg.cg_ndblk = dmax - cbase; 7951558Srgrimes if (sblock.fs_contigsumsize > 0) 7961558Srgrimes acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag; 79723682Speter acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield); 79838579Sdfr acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t); 7998871Srgrimes acg.cg_iusedoff = acg.cg_boff + 80038579Sdfr sblock.fs_cpg * sblock.fs_nrpos * sizeof(u_int16_t); 8011558Srgrimes acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY); 8021558Srgrimes if (sblock.fs_contigsumsize <= 0) { 8031558Srgrimes acg.cg_nextfreeoff = acg.cg_freeoff + 8041558Srgrimes howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY); 8051558Srgrimes } else { 8061558Srgrimes acg.cg_clustersumoff = acg.cg_freeoff + howmany 8071558Srgrimes (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) - 80838579Sdfr sizeof(u_int32_t); 8091558Srgrimes acg.cg_clustersumoff = 81038579Sdfr roundup(acg.cg_clustersumoff, sizeof(u_int32_t)); 8111558Srgrimes acg.cg_clusteroff = acg.cg_clustersumoff + 81238579Sdfr (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t); 8131558Srgrimes acg.cg_nextfreeoff = acg.cg_clusteroff + howmany 8141558Srgrimes (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY); 8151558Srgrimes } 81623682Speter if (acg.cg_nextfreeoff - (long)(&acg.cg_firstfield) > sblock.fs_cgsize) { 8171558Srgrimes printf("Panic: cylinder group too big\n"); 8181558Srgrimes exit(37); 8191558Srgrimes } 8201558Srgrimes acg.cg_cs.cs_nifree += sblock.fs_ipg; 8211558Srgrimes if (cylno == 0) 8221558Srgrimes for (i = 0; i < ROOTINO; i++) { 8231558Srgrimes setbit(cg_inosused(&acg), i); 8241558Srgrimes acg.cg_cs.cs_nifree--; 8251558Srgrimes } 82624149Sguido for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) { 82724149Sguido#ifdef FSIRAND 82824149Sguido for (j = 0; j < sblock.fs_bsize / sizeof(struct dinode); j++) 82924215Sache zino[j].di_gen = random(); 83024149Sguido#endif 8311558Srgrimes wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i), 8321558Srgrimes sblock.fs_bsize, (char *)zino); 83324149Sguido } 8341558Srgrimes if (cylno > 0) { 8351558Srgrimes /* 8361558Srgrimes * In cylno 0, beginning space is reserved 8371558Srgrimes * for boot and super blocks. 8381558Srgrimes */ 8391558Srgrimes for (d = 0; d < dlower; d += sblock.fs_frag) { 8401558Srgrimes blkno = d / sblock.fs_frag; 8411558Srgrimes setblock(&sblock, cg_blksfree(&acg), blkno); 8421558Srgrimes if (sblock.fs_contigsumsize > 0) 8431558Srgrimes setbit(cg_clustersfree(&acg), blkno); 8441558Srgrimes acg.cg_cs.cs_nbfree++; 8451558Srgrimes cg_blktot(&acg)[cbtocylno(&sblock, d)]++; 8461558Srgrimes cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) 8471558Srgrimes [cbtorpos(&sblock, d)]++; 8481558Srgrimes } 8491558Srgrimes sblock.fs_dsize += dlower; 8501558Srgrimes } 8511558Srgrimes sblock.fs_dsize += acg.cg_ndblk - dupper; 85237664Scharnier if ((i = dupper % sblock.fs_frag)) { 8531558Srgrimes acg.cg_frsum[sblock.fs_frag - i]++; 8541558Srgrimes for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) { 8551558Srgrimes setbit(cg_blksfree(&acg), dupper); 8561558Srgrimes acg.cg_cs.cs_nffree++; 8571558Srgrimes } 8581558Srgrimes } 8591558Srgrimes for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) { 8601558Srgrimes blkno = d / sblock.fs_frag; 8611558Srgrimes setblock(&sblock, cg_blksfree(&acg), blkno); 8621558Srgrimes if (sblock.fs_contigsumsize > 0) 8631558Srgrimes setbit(cg_clustersfree(&acg), blkno); 8641558Srgrimes acg.cg_cs.cs_nbfree++; 8651558Srgrimes cg_blktot(&acg)[cbtocylno(&sblock, d)]++; 8661558Srgrimes cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) 8671558Srgrimes [cbtorpos(&sblock, d)]++; 8681558Srgrimes d += sblock.fs_frag; 8691558Srgrimes } 8701558Srgrimes if (d < dmax - cbase) { 8711558Srgrimes acg.cg_frsum[dmax - cbase - d]++; 8721558Srgrimes for (; d < dmax - cbase; d++) { 8731558Srgrimes setbit(cg_blksfree(&acg), d); 8741558Srgrimes acg.cg_cs.cs_nffree++; 8751558Srgrimes } 8761558Srgrimes } 8771558Srgrimes if (sblock.fs_contigsumsize > 0) { 87823682Speter int32_t *sump = cg_clustersum(&acg); 8791558Srgrimes u_char *mapp = cg_clustersfree(&acg); 8801558Srgrimes int map = *mapp++; 8811558Srgrimes int bit = 1; 8821558Srgrimes int run = 0; 8831558Srgrimes 8841558Srgrimes for (i = 0; i < acg.cg_nclusterblks; i++) { 8851558Srgrimes if ((map & bit) != 0) { 8861558Srgrimes run++; 8871558Srgrimes } else if (run != 0) { 8881558Srgrimes if (run > sblock.fs_contigsumsize) 8891558Srgrimes run = sblock.fs_contigsumsize; 8901558Srgrimes sump[run]++; 8911558Srgrimes run = 0; 8921558Srgrimes } 8931558Srgrimes if ((i & (NBBY - 1)) != (NBBY - 1)) { 8941558Srgrimes bit <<= 1; 8951558Srgrimes } else { 8961558Srgrimes map = *mapp++; 8971558Srgrimes bit = 1; 8981558Srgrimes } 8991558Srgrimes } 9001558Srgrimes if (run != 0) { 9011558Srgrimes if (run > sblock.fs_contigsumsize) 9021558Srgrimes run = sblock.fs_contigsumsize; 9031558Srgrimes sump[run]++; 9041558Srgrimes } 9051558Srgrimes } 9061558Srgrimes sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir; 9071558Srgrimes sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree; 9081558Srgrimes sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree; 9091558Srgrimes sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree; 9101558Srgrimes *cs = acg.cg_cs; 9111558Srgrimes wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), 9121558Srgrimes sblock.fs_bsize, (char *)&acg); 9131558Srgrimes} 9141558Srgrimes 9151558Srgrimes/* 9161558Srgrimes * initialize the file system 9171558Srgrimes */ 9181558Srgrimesstruct dinode node; 9191558Srgrimes 9201558Srgrimes#ifdef LOSTDIR 9211558Srgrimes#define PREDEFDIR 3 9221558Srgrimes#else 9231558Srgrimes#define PREDEFDIR 2 9241558Srgrimes#endif 9251558Srgrimes 9261558Srgrimesstruct direct root_dir[] = { 9271558Srgrimes { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." }, 9281558Srgrimes { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, 9291558Srgrimes#ifdef LOSTDIR 9301558Srgrimes { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" }, 9311558Srgrimes#endif 9321558Srgrimes}; 9331558Srgrimesstruct odirect { 9341558Srgrimes u_long d_ino; 9351558Srgrimes u_short d_reclen; 9361558Srgrimes u_short d_namlen; 9371558Srgrimes u_char d_name[MAXNAMLEN + 1]; 9381558Srgrimes} oroot_dir[] = { 9391558Srgrimes { ROOTINO, sizeof(struct direct), 1, "." }, 9401558Srgrimes { ROOTINO, sizeof(struct direct), 2, ".." }, 9411558Srgrimes#ifdef LOSTDIR 9421558Srgrimes { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" }, 9431558Srgrimes#endif 9441558Srgrimes}; 9451558Srgrimes#ifdef LOSTDIR 9461558Srgrimesstruct direct lost_found_dir[] = { 9471558Srgrimes { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." }, 9481558Srgrimes { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, 9491558Srgrimes { 0, DIRBLKSIZ, 0, 0, 0 }, 9501558Srgrimes}; 9511558Srgrimesstruct odirect olost_found_dir[] = { 9521558Srgrimes { LOSTFOUNDINO, sizeof(struct direct), 1, "." }, 9531558Srgrimes { ROOTINO, sizeof(struct direct), 2, ".." }, 9541558Srgrimes { 0, DIRBLKSIZ, 0, 0 }, 9551558Srgrimes}; 9561558Srgrimes#endif 9571558Srgrimeschar buf[MAXBSIZE]; 9581558Srgrimes 95937664Scharniervoid 9601558Srgrimesfsinit(utime) 9611558Srgrimes time_t utime; 9621558Srgrimes{ 96337664Scharnier#ifdef LOSTDIR 9641558Srgrimes int i; 96537664Scharnier#endif 9661558Srgrimes 9671558Srgrimes /* 9681558Srgrimes * initialize the node 9691558Srgrimes */ 97023682Speter node.di_atime = utime; 97123682Speter node.di_mtime = utime; 97223682Speter node.di_ctime = utime; 9731558Srgrimes#ifdef LOSTDIR 9741558Srgrimes /* 9751558Srgrimes * create the lost+found directory 9761558Srgrimes */ 9771558Srgrimes if (Oflag) { 9781558Srgrimes (void)makedir((struct direct *)olost_found_dir, 2); 9791558Srgrimes for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ) 98023682Speter memmove(&buf[i], &olost_found_dir[2], 9811558Srgrimes DIRSIZ(0, &olost_found_dir[2])); 9821558Srgrimes } else { 9831558Srgrimes (void)makedir(lost_found_dir, 2); 9841558Srgrimes for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ) 98523682Speter memmove(&buf[i], &lost_found_dir[2], 9861558Srgrimes DIRSIZ(0, &lost_found_dir[2])); 9871558Srgrimes } 9881558Srgrimes node.di_mode = IFDIR | UMASK; 9891558Srgrimes node.di_nlink = 2; 9901558Srgrimes node.di_size = sblock.fs_bsize; 9911558Srgrimes node.di_db[0] = alloc(node.di_size, node.di_mode); 9921558Srgrimes node.di_blocks = btodb(fragroundup(&sblock, node.di_size)); 9931558Srgrimes wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf); 9941558Srgrimes iput(&node, LOSTFOUNDINO); 9951558Srgrimes#endif 9961558Srgrimes /* 9971558Srgrimes * create the root directory 9981558Srgrimes */ 9991558Srgrimes if (mfs) 10001558Srgrimes node.di_mode = IFDIR | 01777; 10011558Srgrimes else 10021558Srgrimes node.di_mode = IFDIR | UMASK; 10031558Srgrimes node.di_nlink = PREDEFDIR; 10041558Srgrimes if (Oflag) 10051558Srgrimes node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR); 10061558Srgrimes else 10071558Srgrimes node.di_size = makedir(root_dir, PREDEFDIR); 10081558Srgrimes node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode); 10091558Srgrimes node.di_blocks = btodb(fragroundup(&sblock, node.di_size)); 10101558Srgrimes wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf); 10111558Srgrimes iput(&node, ROOTINO); 10121558Srgrimes} 10131558Srgrimes 10141558Srgrimes/* 10151558Srgrimes * construct a set of directory entries in "buf". 10161558Srgrimes * return size of directory. 10171558Srgrimes */ 101837664Scharnierint 10191558Srgrimesmakedir(protodir, entries) 10201558Srgrimes register struct direct *protodir; 10211558Srgrimes int entries; 10221558Srgrimes{ 10231558Srgrimes char *cp; 10241558Srgrimes int i, spcleft; 10251558Srgrimes 10261558Srgrimes spcleft = DIRBLKSIZ; 10271558Srgrimes for (cp = buf, i = 0; i < entries - 1; i++) { 10281558Srgrimes protodir[i].d_reclen = DIRSIZ(0, &protodir[i]); 102923682Speter memmove(cp, &protodir[i], protodir[i].d_reclen); 10301558Srgrimes cp += protodir[i].d_reclen; 10311558Srgrimes spcleft -= protodir[i].d_reclen; 10321558Srgrimes } 10331558Srgrimes protodir[i].d_reclen = spcleft; 103423682Speter memmove(cp, &protodir[i], DIRSIZ(0, &protodir[i])); 10351558Srgrimes return (DIRBLKSIZ); 10361558Srgrimes} 10371558Srgrimes 10381558Srgrimes/* 10391558Srgrimes * allocate a block or frag 10401558Srgrimes */ 10411558Srgrimesdaddr_t 10421558Srgrimesalloc(size, mode) 10431558Srgrimes int size; 10441558Srgrimes int mode; 10451558Srgrimes{ 10461558Srgrimes int i, frag; 10471558Srgrimes daddr_t d, blkno; 10481558Srgrimes 10491558Srgrimes rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 10501558Srgrimes (char *)&acg); 10511558Srgrimes if (acg.cg_magic != CG_MAGIC) { 10521558Srgrimes printf("cg 0: bad magic number\n"); 10531558Srgrimes return (0); 10541558Srgrimes } 10551558Srgrimes if (acg.cg_cs.cs_nbfree == 0) { 10561558Srgrimes printf("first cylinder group ran out of space\n"); 10571558Srgrimes return (0); 10581558Srgrimes } 10591558Srgrimes for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag) 10601558Srgrimes if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag)) 10611558Srgrimes goto goth; 10621558Srgrimes printf("internal error: can't find block in cyl 0\n"); 10631558Srgrimes return (0); 10641558Srgrimesgoth: 10651558Srgrimes blkno = fragstoblks(&sblock, d); 10661558Srgrimes clrblock(&sblock, cg_blksfree(&acg), blkno); 106723682Speter if (sblock.fs_contigsumsize > 0) 106823682Speter clrbit(cg_clustersfree(&acg), blkno); 10691558Srgrimes acg.cg_cs.cs_nbfree--; 10701558Srgrimes sblock.fs_cstotal.cs_nbfree--; 10711558Srgrimes fscs[0].cs_nbfree--; 10721558Srgrimes if (mode & IFDIR) { 10731558Srgrimes acg.cg_cs.cs_ndir++; 10741558Srgrimes sblock.fs_cstotal.cs_ndir++; 10751558Srgrimes fscs[0].cs_ndir++; 10761558Srgrimes } 10771558Srgrimes cg_blktot(&acg)[cbtocylno(&sblock, d)]--; 10781558Srgrimes cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--; 10791558Srgrimes if (size != sblock.fs_bsize) { 10801558Srgrimes frag = howmany(size, sblock.fs_fsize); 10811558Srgrimes fscs[0].cs_nffree += sblock.fs_frag - frag; 10821558Srgrimes sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag; 10831558Srgrimes acg.cg_cs.cs_nffree += sblock.fs_frag - frag; 10841558Srgrimes acg.cg_frsum[sblock.fs_frag - frag]++; 10851558Srgrimes for (i = frag; i < sblock.fs_frag; i++) 10861558Srgrimes setbit(cg_blksfree(&acg), d + i); 10871558Srgrimes } 10881558Srgrimes wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 10891558Srgrimes (char *)&acg); 10901558Srgrimes return (d); 10911558Srgrimes} 10921558Srgrimes 10931558Srgrimes/* 109423682Speter * Calculate number of inodes per group. 109523682Speter */ 109623682Speterlong 109723682Spetercalcipg(cpg, bpcg, usedbp) 109823682Speter long cpg; 109923682Speter long bpcg; 110023682Speter off_t *usedbp; 110123682Speter{ 110223682Speter int i; 110323682Speter long ipg, new_ipg, ncg, ncyl; 110423682Speter off_t usedb; 110523682Speter 110623682Speter /* 110723682Speter * Prepare to scale by fssize / (number of sectors in cylinder groups). 110823682Speter * Note that fssize is still in sectors, not filesystem blocks. 110923682Speter */ 111036498Sbde ncyl = howmany(fssize, (u_int)secpercyl); 111123682Speter ncg = howmany(ncyl, cpg); 111223682Speter /* 111323682Speter * Iterate a few times to allow for ipg depending on itself. 111423682Speter */ 111523682Speter ipg = 0; 111623682Speter for (i = 0; i < 10; i++) { 111723682Speter usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock)) 111823682Speter * NSPF(&sblock) * (off_t)sectorsize; 111923682Speter new_ipg = (cpg * (quad_t)bpcg - usedb) / density * fssize 112023682Speter / ncg / secpercyl / cpg; 112123682Speter new_ipg = roundup(new_ipg, INOPB(&sblock)); 112223682Speter if (new_ipg == ipg) 112323682Speter break; 112423682Speter ipg = new_ipg; 112523682Speter } 112623682Speter *usedbp = usedb; 112723682Speter return (ipg); 112823682Speter} 112923682Speter 113023682Speter/* 11311558Srgrimes * Allocate an inode on the disk 11321558Srgrimes */ 113337664Scharniervoid 11341558Srgrimesiput(ip, ino) 11351558Srgrimes register struct dinode *ip; 11361558Srgrimes register ino_t ino; 11371558Srgrimes{ 11381558Srgrimes struct dinode buf[MAXINOPB]; 11391558Srgrimes daddr_t d; 11401558Srgrimes int c; 11411558Srgrimes 114224149Sguido#ifdef FSIRAND 114324215Sache ip->di_gen = random(); 114424149Sguido#endif 11451558Srgrimes c = ino_to_cg(&sblock, ino); 11461558Srgrimes rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 11471558Srgrimes (char *)&acg); 11481558Srgrimes if (acg.cg_magic != CG_MAGIC) { 11491558Srgrimes printf("cg 0: bad magic number\n"); 11501558Srgrimes exit(31); 11511558Srgrimes } 11521558Srgrimes acg.cg_cs.cs_nifree--; 11531558Srgrimes setbit(cg_inosused(&acg), ino); 11541558Srgrimes wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 11551558Srgrimes (char *)&acg); 11561558Srgrimes sblock.fs_cstotal.cs_nifree--; 11571558Srgrimes fscs[0].cs_nifree--; 11581558Srgrimes if (ino >= sblock.fs_ipg * sblock.fs_ncg) { 11591558Srgrimes printf("fsinit: inode value out of range (%d).\n", ino); 11601558Srgrimes exit(32); 11611558Srgrimes } 11621558Srgrimes d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino)); 116337664Scharnier rdfs(d, sblock.fs_bsize, (char *)buf); 11641558Srgrimes buf[ino_to_fsbo(&sblock, ino)] = *ip; 116537664Scharnier wtfs(d, sblock.fs_bsize, (char *)buf); 11661558Srgrimes} 11671558Srgrimes 11681558Srgrimes/* 11691558Srgrimes * Notify parent process that the filesystem has created itself successfully. 117043804Sdillon * 117143804Sdillon * We have to wait until the mount has actually completed! 11721558Srgrimes */ 11731558Srgrimesvoid 11741558Srgrimesstarted() 11751558Srgrimes{ 117643804Sdillon int retry = 100; /* 10 seconds, 100ms */ 11771558Srgrimes 117843804Sdillon while (mfs_ppid && retry) { 117943804Sdillon struct stat st; 118043804Sdillon 118143804Sdillon if ( 118243804Sdillon stat(mfs_mtpt, &st) < 0 || 118343804Sdillon st.st_dev != mfs_mtstat.st_dev 118443804Sdillon ) { 118543804Sdillon break; 118643804Sdillon } 118743804Sdillon usleep(100*1000); 118843804Sdillon --retry; 118943804Sdillon } 119043804Sdillon if (retry == 0) { 119143804Sdillon fatal("mfs mount failed waiting for mount to go active"); 119243804Sdillon } 11931558Srgrimes exit(0); 11941558Srgrimes} 11951558Srgrimes 119624215Sache#ifdef STANDALONE 11971558Srgrimes/* 11981558Srgrimes * Replace libc function with one suited to our needs. 11991558Srgrimes */ 12001558Srgrimescaddr_t 12011558Srgrimesmalloc(size) 12021558Srgrimes register u_long size; 12031558Srgrimes{ 12041558Srgrimes char *base, *i; 12051558Srgrimes static u_long pgsz; 12061558Srgrimes struct rlimit rlp; 12071558Srgrimes 12081558Srgrimes if (pgsz == 0) { 12091558Srgrimes base = sbrk(0); 12101558Srgrimes pgsz = getpagesize() - 1; 12111558Srgrimes i = (char *)((u_long)(base + pgsz) &~ pgsz); 12121558Srgrimes base = sbrk(i - base); 12131558Srgrimes if (getrlimit(RLIMIT_DATA, &rlp) < 0) 121437664Scharnier warn("getrlimit"); 12151558Srgrimes rlp.rlim_cur = rlp.rlim_max; 12161558Srgrimes if (setrlimit(RLIMIT_DATA, &rlp) < 0) 121737664Scharnier warn("setrlimit"); 12181558Srgrimes memleft = rlp.rlim_max - (u_long)base; 12191558Srgrimes } 12201558Srgrimes size = (size + pgsz) &~ pgsz; 12211558Srgrimes if (size > memleft) 12221558Srgrimes size = memleft; 12231558Srgrimes memleft -= size; 12241558Srgrimes if (size == 0) 12251558Srgrimes return (0); 12261558Srgrimes return ((caddr_t)sbrk(size)); 12271558Srgrimes} 12281558Srgrimes 12291558Srgrimes/* 12301558Srgrimes * Replace libc function with one suited to our needs. 12311558Srgrimes */ 12321558Srgrimescaddr_t 12331558Srgrimesrealloc(ptr, size) 12341558Srgrimes char *ptr; 12351558Srgrimes u_long size; 12361558Srgrimes{ 12371558Srgrimes void *p; 12381558Srgrimes 12391558Srgrimes if ((p = malloc(size)) == NULL) 12401558Srgrimes return (NULL); 124123682Speter memmove(p, ptr, size); 12421558Srgrimes free(ptr); 12431558Srgrimes return (p); 12441558Srgrimes} 12451558Srgrimes 12461558Srgrimes/* 12471558Srgrimes * Replace libc function with one suited to our needs. 12481558Srgrimes */ 12491558Srgrimeschar * 12501558Srgrimescalloc(size, numelm) 12511558Srgrimes u_long size, numelm; 12521558Srgrimes{ 12531558Srgrimes caddr_t base; 12541558Srgrimes 12551558Srgrimes size *= numelm; 125638255Scharnier if ((base = malloc(size)) == NULL) 125737664Scharnier return (NULL); 125823682Speter memset(base, 0, size); 12591558Srgrimes return (base); 12601558Srgrimes} 12611558Srgrimes 12621558Srgrimes/* 12631558Srgrimes * Replace libc function with one suited to our needs. 12641558Srgrimes */ 126537664Scharniervoid 12661558Srgrimesfree(ptr) 12671558Srgrimes char *ptr; 12681558Srgrimes{ 12698871Srgrimes 12701558Srgrimes /* do not worry about it for now */ 12711558Srgrimes} 12721558Srgrimes 127324215Sache#else /* !STANDALONE */ 127424215Sache 127537664Scharniervoid 127624215Sacheraise_data_limit() 127724215Sache{ 127824215Sache struct rlimit rlp; 127924215Sache 128024215Sache if (getrlimit(RLIMIT_DATA, &rlp) < 0) 128137664Scharnier warn("getrlimit"); 128224215Sache rlp.rlim_cur = rlp.rlim_max; 128324215Sache if (setrlimit(RLIMIT_DATA, &rlp) < 0) 128437664Scharnier warn("setrlimit"); 128524215Sache} 128624215Sache 128729321Speter#ifdef __ELF__ 128829321Speterextern char *_etext; 128929321Speter#define etext _etext 129029321Speter#else 129129321Speterextern char *etext; 129229321Speter#endif 129329321Speter 129437664Scharniervoid 129524457Speterget_memleft() 129624457Speter{ 129729321Speter static u_long pgsz; 129824457Speter struct rlimit rlp; 129929321Speter u_long freestart; 130029321Speter u_long dstart; 130129321Speter u_long memused; 130224457Speter 130324457Speter pgsz = getpagesize() - 1; 130429321Speter dstart = ((u_long)&etext) &~ pgsz; 130529321Speter freestart = ((u_long)(sbrk(0) + pgsz) &~ pgsz); 130624457Speter if (getrlimit(RLIMIT_DATA, &rlp) < 0) 130737664Scharnier warn("getrlimit"); 130829321Speter memused = freestart - dstart; 130929321Speter memleft = rlp.rlim_cur - memused; 131024457Speter} 131124215Sache#endif /* STANDALONE */ 131224215Sache 13131558Srgrimes/* 13141558Srgrimes * read a block from the file system 13151558Srgrimes */ 131637664Scharniervoid 13171558Srgrimesrdfs(bno, size, bf) 13181558Srgrimes daddr_t bno; 13191558Srgrimes int size; 13201558Srgrimes char *bf; 13211558Srgrimes{ 13221558Srgrimes int n; 13231558Srgrimes 132467478Sjwd wtfsflush(); 13251558Srgrimes if (mfs) { 132623682Speter memmove(bf, membase + bno * sectorsize, size); 13271558Srgrimes return; 13281558Srgrimes } 13291558Srgrimes if (lseek(fsi, (off_t)bno * sectorsize, 0) < 0) { 133037239Sbde printf("seek error: %ld\n", (long)bno); 133137664Scharnier err(33, "rdfs"); 13321558Srgrimes } 13331558Srgrimes n = read(fsi, bf, size); 13341558Srgrimes if (n != size) { 133537239Sbde printf("read error: %ld\n", (long)bno); 133637664Scharnier err(34, "rdfs"); 13371558Srgrimes } 13381558Srgrimes} 13391558Srgrimes 134067239Speter#define WCSIZE (128 * 1024) 134167239Speterdaddr_t wc_sect; /* units of sectorsize */ 134267239Speterint wc_end; /* bytes */ 134367239Speterstatic char wc[WCSIZE]; /* bytes */ 134467239Speter 13451558Srgrimes/* 134667478Sjwd * Flush dirty write behind buffer. 134767478Sjwd */ 134867478Sjwdvoid 134967478Sjwdwtfsflush() 135067478Sjwd{ 135167478Sjwd int n; 135267478Sjwd if (wc_end) { 135367478Sjwd if (lseek(fso, (off_t)wc_sect * sectorsize, SEEK_SET) < 0) { 135467478Sjwd printf("seek error: %ld\n", (long)wc_sect); 135567478Sjwd err(35, "wtfs - writecombine"); 135667478Sjwd } 135767478Sjwd n = write(fso, wc, wc_end); 135867478Sjwd if (n != wc_end) { 135967478Sjwd printf("write error: %ld\n", (long)wc_sect); 136067478Sjwd err(36, "wtfs - writecombine"); 136167478Sjwd } 136267478Sjwd wc_end = 0; 136367478Sjwd } 136467478Sjwd} 136567478Sjwd 136667478Sjwd/* 13671558Srgrimes * write a block to the file system 13681558Srgrimes */ 136937664Scharniervoid 13701558Srgrimeswtfs(bno, size, bf) 13711558Srgrimes daddr_t bno; 13721558Srgrimes int size; 13731558Srgrimes char *bf; 13741558Srgrimes{ 13751558Srgrimes int n; 137667239Speter int done; 13771558Srgrimes 13781558Srgrimes if (mfs) { 137923682Speter memmove(membase + bno * sectorsize, bf, size); 13801558Srgrimes return; 13811558Srgrimes } 13821558Srgrimes if (Nflag) 13831558Srgrimes return; 138467239Speter done = 0; 138567239Speter if (wc_end == 0 && size <= WCSIZE) { 138667239Speter wc_sect = bno; 138767239Speter bcopy(bf, wc, size); 138867239Speter wc_end = size; 138967239Speter if (wc_end < WCSIZE) 139067239Speter return; 139167239Speter done = 1; 139267239Speter } 139367479Sjwd if ((off_t)wc_sect * sectorsize + wc_end == (off_t)bno * sectorsize && 139467239Speter wc_end + size <= WCSIZE) { 139567239Speter bcopy(bf, wc + wc_end, size); 139667239Speter wc_end += size; 139767239Speter if (wc_end < WCSIZE) 139867239Speter return; 139967239Speter done = 1; 140067239Speter } 140167478Sjwd wtfsflush(); 140267239Speter if (done) 140367239Speter return; 14041558Srgrimes if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) { 140537239Sbde printf("seek error: %ld\n", (long)bno); 140637664Scharnier err(35, "wtfs"); 14071558Srgrimes } 14081558Srgrimes n = write(fso, bf, size); 14091558Srgrimes if (n != size) { 141037239Sbde printf("write error: %ld\n", (long)bno); 141137664Scharnier err(36, "wtfs"); 14121558Srgrimes } 14131558Srgrimes} 14141558Srgrimes 14151558Srgrimes/* 14161558Srgrimes * check if a block is available 14171558Srgrimes */ 141837664Scharnierint 14191558Srgrimesisblock(fs, cp, h) 14201558Srgrimes struct fs *fs; 14211558Srgrimes unsigned char *cp; 14221558Srgrimes int h; 14231558Srgrimes{ 14241558Srgrimes unsigned char mask; 14251558Srgrimes 14261558Srgrimes switch (fs->fs_frag) { 14271558Srgrimes case 8: 14281558Srgrimes return (cp[h] == 0xff); 14291558Srgrimes case 4: 14301558Srgrimes mask = 0x0f << ((h & 0x1) << 2); 14311558Srgrimes return ((cp[h >> 1] & mask) == mask); 14321558Srgrimes case 2: 14331558Srgrimes mask = 0x03 << ((h & 0x3) << 1); 14341558Srgrimes return ((cp[h >> 2] & mask) == mask); 14351558Srgrimes case 1: 14361558Srgrimes mask = 0x01 << (h & 0x7); 14371558Srgrimes return ((cp[h >> 3] & mask) == mask); 14381558Srgrimes default: 14391558Srgrimes#ifdef STANDALONE 14401558Srgrimes printf("isblock bad fs_frag %d\n", fs->fs_frag); 14411558Srgrimes#else 14421558Srgrimes fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag); 14431558Srgrimes#endif 14441558Srgrimes return (0); 14451558Srgrimes } 14461558Srgrimes} 14471558Srgrimes 14481558Srgrimes/* 14491558Srgrimes * take a block out of the map 14501558Srgrimes */ 145137664Scharniervoid 14521558Srgrimesclrblock(fs, cp, h) 14531558Srgrimes struct fs *fs; 14541558Srgrimes unsigned char *cp; 14551558Srgrimes int h; 14561558Srgrimes{ 14571558Srgrimes switch ((fs)->fs_frag) { 14581558Srgrimes case 8: 14591558Srgrimes cp[h] = 0; 14601558Srgrimes return; 14611558Srgrimes case 4: 14621558Srgrimes cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 14631558Srgrimes return; 14641558Srgrimes case 2: 14651558Srgrimes cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 14661558Srgrimes return; 14671558Srgrimes case 1: 14681558Srgrimes cp[h >> 3] &= ~(0x01 << (h & 0x7)); 14691558Srgrimes return; 14701558Srgrimes default: 14711558Srgrimes#ifdef STANDALONE 14721558Srgrimes printf("clrblock bad fs_frag %d\n", fs->fs_frag); 14731558Srgrimes#else 14741558Srgrimes fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag); 14751558Srgrimes#endif 14761558Srgrimes return; 14771558Srgrimes } 14781558Srgrimes} 14791558Srgrimes 14801558Srgrimes/* 14811558Srgrimes * put a block into the map 14821558Srgrimes */ 148337664Scharniervoid 14841558Srgrimessetblock(fs, cp, h) 14851558Srgrimes struct fs *fs; 14861558Srgrimes unsigned char *cp; 14871558Srgrimes int h; 14881558Srgrimes{ 14891558Srgrimes switch (fs->fs_frag) { 14901558Srgrimes case 8: 14911558Srgrimes cp[h] = 0xff; 14921558Srgrimes return; 14931558Srgrimes case 4: 14941558Srgrimes cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 14951558Srgrimes return; 14961558Srgrimes case 2: 14971558Srgrimes cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 14981558Srgrimes return; 14991558Srgrimes case 1: 15001558Srgrimes cp[h >> 3] |= (0x01 << (h & 0x7)); 15011558Srgrimes return; 15021558Srgrimes default: 15031558Srgrimes#ifdef STANDALONE 15041558Srgrimes printf("setblock bad fs_frag %d\n", fs->fs_frag); 15051558Srgrimes#else 15061558Srgrimes fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag); 15071558Srgrimes#endif 15081558Srgrimes return; 15091558Srgrimes } 15101558Srgrimes} 151113637Sjoerg 151213637Sjoerg/* 151313769Sjoerg * Determine the number of characters in a 151413637Sjoerg * single line. 151513637Sjoerg */ 151613637Sjoerg 151713637Sjoergstatic int 151813769Sjoergcharsperline() 151913637Sjoerg{ 152013769Sjoerg int columns; 152113637Sjoerg char *cp; 152213637Sjoerg struct winsize ws; 152313637Sjoerg 152413637Sjoerg columns = 0; 152513637Sjoerg if (ioctl(0, TIOCGWINSZ, &ws) != -1) 152613637Sjoerg columns = ws.ws_col; 152713637Sjoerg if (columns == 0 && (cp = getenv("COLUMNS"))) 152813637Sjoerg columns = atoi(cp); 152913637Sjoerg if (columns == 0) 153013637Sjoerg columns = 80; /* last resort */ 153113769Sjoerg return columns; 153213637Sjoerg} 1533