14359Smckusick /* Copyright (c) 1981 Regents of the University of California */ 24359Smckusick 3*5960Smckusic static char vers[] = "@(#)lfs_alloc.c 1.18 02/25/82"; 44359Smckusick 54359Smckusick /* alloc.c 4.8 81/03/08 */ 64359Smckusick 74359Smckusick #include "../h/param.h" 84359Smckusick #include "../h/systm.h" 94359Smckusick #include "../h/mount.h" 104359Smckusick #include "../h/fs.h" 114359Smckusick #include "../h/conf.h" 124359Smckusick #include "../h/buf.h" 134359Smckusick #include "../h/inode.h" 144359Smckusick #include "../h/user.h" 154359Smckusick 165212Smckusic extern u_long hashalloc(); 175212Smckusic extern ino_t ialloccg(); 184651Smckusic extern daddr_t alloccg(); 194651Smckusic extern daddr_t alloccgblk(); 204651Smckusic extern daddr_t fragextend(); 214651Smckusic extern daddr_t blkpref(); 224651Smckusic extern daddr_t mapsearch(); 234607Smckusic extern int inside[], around[]; 245322Smckusic extern unsigned char *fragtbl[]; 254359Smckusick 265375Smckusic /* 275375Smckusic * Allocate a block in the file system. 285375Smckusic * 295375Smckusic * The size of the requested block is given, which must be some 305375Smckusic * multiple of fs_fsize and <= fs_bsize. 315375Smckusic * A preference may be optionally specified. If a preference is given 325375Smckusic * the following hierarchy is used to allocate a block: 335375Smckusic * 1) allocate the requested block. 345375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 355375Smckusic * 3) allocate a block in the same cylinder group. 365375Smckusic * 4) quadradically rehash into other cylinder groups, until an 375375Smckusic * available block is located. 385375Smckusic * If no block preference is given the following heirarchy is used 395375Smckusic * to allocate a block: 405375Smckusic * 1) allocate a block in the cylinder group that contains the 415375Smckusic * inode for the file. 425375Smckusic * 2) quadradically rehash into other cylinder groups, until an 435375Smckusic * available block is located. 445375Smckusic */ 454359Smckusick struct buf * 464359Smckusick alloc(dev, ip, bpref, size) 474359Smckusick dev_t dev; 484463Smckusic register struct inode *ip; 494359Smckusick daddr_t bpref; 504359Smckusick int size; 514359Smckusick { 524359Smckusick daddr_t bno; 534359Smckusick register struct fs *fs; 544463Smckusic register struct buf *bp; 554359Smckusick int cg; 564359Smckusick 575322Smckusic fs = getfs(dev); 58*5960Smckusic if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) 594463Smckusic panic("alloc: bad size"); 605322Smckusic if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 614359Smckusick goto nospace; 624792Smckusic if (u.u_uid != 0 && 635322Smckusic fs->fs_cstotal.cs_nbfree * fs->fs_frag + fs->fs_cstotal.cs_nffree < 645322Smckusic fs->fs_dsize * fs->fs_minfree / 100) 654792Smckusic goto nospace; 664948Smckusic if (bpref >= fs->fs_size) 674948Smckusic bpref = 0; 684359Smckusick if (bpref == 0) 695377Smckusic cg = itog(fs, ip->i_number); 704359Smckusick else 715377Smckusic cg = dtog(fs, bpref); 725212Smckusic bno = (daddr_t)hashalloc(dev, fs, cg, (long)bpref, size, alloccg); 734359Smckusick if (bno == 0) 744359Smckusick goto nospace; 755322Smckusic bp = getblk(dev, fsbtodb(fs, bno), size); 764359Smckusick clrbuf(bp); 774359Smckusick return (bp); 784359Smckusick nospace: 794359Smckusick fserr(fs, "file system full"); 804359Smckusick uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 814359Smckusick u.u_error = ENOSPC; 824359Smckusick return (NULL); 834359Smckusick } 844359Smckusick 855375Smckusic /* 865375Smckusic * Reallocate a fragment to a bigger size 875375Smckusic * 885375Smckusic * The number and size of the old block is given, and a preference 895375Smckusic * and new size is also specified. The allocator attempts to extend 905375Smckusic * the original block. Failing that, the regular block allocator is 915375Smckusic * invoked to get an appropriate block. 925375Smckusic */ 934426Smckusic struct buf * 945212Smckusic realloccg(dev, bprev, bpref, osize, nsize) 954426Smckusic dev_t dev; 964651Smckusic daddr_t bprev, bpref; 974426Smckusic int osize, nsize; 984426Smckusic { 994426Smckusic daddr_t bno; 1004426Smckusic register struct fs *fs; 1014463Smckusic register struct buf *bp, *obp; 1024463Smckusic caddr_t cp; 1034426Smckusic int cg; 1044426Smckusic 1055322Smckusic fs = getfs(dev); 106*5960Smckusic if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 107*5960Smckusic (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) 1084463Smckusic panic("realloccg: bad size"); 1094792Smckusic if (u.u_uid != 0 && 1105322Smckusic fs->fs_cstotal.cs_nbfree * fs->fs_frag + fs->fs_cstotal.cs_nffree < 1115322Smckusic fs->fs_dsize * fs->fs_minfree / 100) 1124792Smckusic goto nospace; 1135375Smckusic if (bprev != 0) 1145377Smckusic cg = dtog(fs, bprev); 1155375Smckusic else 1164463Smckusic panic("realloccg: bad bprev"); 1174463Smckusic bno = fragextend(dev, fs, cg, (long)bprev, osize, nsize); 1184463Smckusic if (bno != 0) { 1195322Smckusic bp = bread(dev, fsbtodb(fs, bno), osize); 120*5960Smckusic if (bp->b_flags & B_ERROR) { 121*5960Smckusic brelse(bp); 122*5960Smckusic return 0; 123*5960Smckusic } 1244463Smckusic bp->b_bcount = nsize; 1254463Smckusic blkclr(bp->b_un.b_addr + osize, nsize - osize); 1264463Smckusic return (bp); 1274463Smckusic } 1284948Smckusic if (bpref >= fs->fs_size) 1294948Smckusic bpref = 0; 1305212Smckusic bno = (daddr_t)hashalloc(dev, fs, cg, (long)bpref, nsize, alloccg); 1314463Smckusic if (bno != 0) { 1324463Smckusic /* 1334463Smckusic * make a new copy 1344463Smckusic */ 1355322Smckusic obp = bread(dev, fsbtodb(fs, bprev), osize); 136*5960Smckusic if (obp->b_flags & B_ERROR) { 137*5960Smckusic brelse(obp); 138*5960Smckusic return 0; 139*5960Smckusic } 1405322Smckusic bp = getblk(dev, fsbtodb(fs, bno), nsize); 1414463Smckusic cp = bp->b_un.b_addr; 1424463Smckusic bp->b_un.b_addr = obp->b_un.b_addr; 1434463Smckusic obp->b_un.b_addr = cp; 1444463Smckusic obp->b_flags |= B_INVAL; 1454463Smckusic brelse(obp); 1465212Smckusic fre(dev, bprev, (off_t)osize); 1474463Smckusic blkclr(bp->b_un.b_addr + osize, nsize - osize); 1484463Smckusic return(bp); 1494463Smckusic } 1504792Smckusic nospace: 1514463Smckusic /* 1524463Smckusic * no space available 1534463Smckusic */ 1544426Smckusic fserr(fs, "file system full"); 1554426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 1564426Smckusic u.u_error = ENOSPC; 1574426Smckusic return (NULL); 1584426Smckusic } 1594426Smckusic 1605375Smckusic /* 1615375Smckusic * Allocate an inode in the file system. 1625375Smckusic * 1635375Smckusic * A preference may be optionally specified. If a preference is given 1645375Smckusic * the following hierarchy is used to allocate an inode: 1655375Smckusic * 1) allocate the requested inode. 1665375Smckusic * 2) allocate an inode in the same cylinder group. 1675375Smckusic * 3) quadradically rehash into other cylinder groups, until an 1685375Smckusic * available inode is located. 1695375Smckusic * If no inode preference is given the following heirarchy is used 1705375Smckusic * to allocate an inode: 1715375Smckusic * 1) allocate an inode in cylinder group 0. 1725375Smckusic * 2) quadradically rehash into other cylinder groups, until an 1735375Smckusic * available inode is located. 1745375Smckusic */ 1754359Smckusick struct inode * 1764359Smckusick ialloc(dev, ipref, mode) 1774359Smckusick dev_t dev; 1784359Smckusick ino_t ipref; 1794359Smckusick int mode; 1804359Smckusick { 1815212Smckusic ino_t ino; 1824359Smckusick register struct fs *fs; 1834359Smckusick register struct inode *ip; 1844359Smckusick int cg; 1854359Smckusick 1864359Smckusick fs = getfs(dev); 1874792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 1884359Smckusick goto noinodes; 1894948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 1904948Smckusic ipref = 0; 1915377Smckusic cg = itog(fs, ipref); 1925212Smckusic ino = (ino_t)hashalloc(dev, fs, cg, (long)ipref, mode, ialloccg); 1934359Smckusick if (ino == 0) 1944359Smckusick goto noinodes; 1954359Smckusick ip = iget(dev, ino); 1964359Smckusick if (ip == NULL) { 1975212Smckusic ifree(dev, ino, 0); 1984359Smckusick return (NULL); 1994359Smckusick } 2004359Smckusick if (ip->i_mode) 2014359Smckusick panic("ialloc: dup alloc"); 2024359Smckusick return (ip); 2034359Smckusick noinodes: 2044359Smckusick fserr(fs, "out of inodes"); 2054359Smckusick uprintf("\n%s: create failed, no inodes free\n", fs->fs_fsmnt); 2064359Smckusick u.u_error = ENOSPC; 2074359Smckusick return (NULL); 2084359Smckusick } 2094359Smckusick 2104651Smckusic /* 2115375Smckusic * Find a cylinder to place a directory. 2125375Smckusic * 2135375Smckusic * The policy implemented by this algorithm is to select from 2145375Smckusic * among those cylinder groups with above the average number of 2155375Smckusic * free inodes, the one with the smallest number of directories. 2164651Smckusic */ 2174651Smckusic dirpref(dev) 2184359Smckusick dev_t dev; 2194359Smckusick { 2204359Smckusick register struct fs *fs; 2214651Smckusic int cg, minndir, mincg, avgifree; 2224359Smckusick 2234359Smckusick fs = getfs(dev); 2244792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 2254651Smckusic minndir = fs->fs_ipg; 2264359Smckusick mincg = 0; 2274651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 2285322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 2295322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 2304359Smckusick mincg = cg; 2315322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 2324359Smckusick } 2334359Smckusick return (fs->fs_ipg * mincg); 2344359Smckusick } 2354359Smckusick 2364651Smckusic /* 2375375Smckusic * Select a cylinder to place a large block of data. 2385375Smckusic * 2395375Smckusic * The policy implemented by this algorithm is to maintain a 2405375Smckusic * rotor that sweeps the cylinder groups. When a block is 2415375Smckusic * needed, the rotor is advanced until a cylinder group with 2425375Smckusic * greater than the average number of free blocks is found. 2434651Smckusic */ 2445212Smckusic daddr_t 2454651Smckusic blkpref(dev) 2464651Smckusic dev_t dev; 2474651Smckusic { 2484651Smckusic register struct fs *fs; 2494651Smckusic int cg, avgbfree; 2504651Smckusic 2514651Smckusic fs = getfs(dev); 2524792Smckusic avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 2534651Smckusic for (cg = fs->fs_cgrotor + 1; cg < fs->fs_ncg; cg++) 2545322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 2554651Smckusic fs->fs_cgrotor = cg; 2565322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 2574651Smckusic } 2584651Smckusic for (cg = 0; cg <= fs->fs_cgrotor; cg++) 2595322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 2604651Smckusic fs->fs_cgrotor = cg; 2615322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 2624651Smckusic } 2634651Smckusic return (0); 2644651Smckusic } 2654651Smckusic 2665375Smckusic /* 2675375Smckusic * Implement the cylinder overflow algorithm. 2685375Smckusic * 2695375Smckusic * The policy implemented by this algorithm is: 2705375Smckusic * 1) allocate the block in its requested cylinder group. 2715375Smckusic * 2) quadradically rehash on the cylinder group number. 2725375Smckusic * 3) brute force search for a free block. 2735375Smckusic */ 2745212Smckusic /*VARARGS5*/ 2755212Smckusic u_long 2764359Smckusick hashalloc(dev, fs, cg, pref, size, allocator) 2774359Smckusick dev_t dev; 2784359Smckusick register struct fs *fs; 2794359Smckusick int cg; 2804359Smckusick long pref; 2814359Smckusick int size; /* size for data blocks, mode for inodes */ 2825212Smckusic u_long (*allocator)(); 2834359Smckusick { 2844359Smckusick long result; 2854359Smckusick int i, icg = cg; 2864359Smckusick 2874359Smckusick /* 2884359Smckusick * 1: preferred cylinder group 2894359Smckusick */ 2904359Smckusick result = (*allocator)(dev, fs, cg, pref, size); 2914359Smckusick if (result) 2924359Smckusick return (result); 2934359Smckusick /* 2944359Smckusick * 2: quadratic rehash 2954359Smckusick */ 2964359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 2974359Smckusick cg += i; 2984359Smckusick if (cg >= fs->fs_ncg) 2994359Smckusick cg -= fs->fs_ncg; 3004359Smckusick result = (*allocator)(dev, fs, cg, 0, size); 3014359Smckusick if (result) 3024359Smckusick return (result); 3034359Smckusick } 3044359Smckusick /* 3054359Smckusick * 3: brute force search 3064359Smckusick */ 3074359Smckusick cg = icg; 3084359Smckusick for (i = 0; i < fs->fs_ncg; i++) { 3094359Smckusick result = (*allocator)(dev, fs, cg, 0, size); 3104359Smckusick if (result) 3114359Smckusick return (result); 3124359Smckusick cg++; 3134359Smckusick if (cg == fs->fs_ncg) 3144359Smckusick cg = 0; 3154359Smckusick } 3164359Smckusick return (0); 3174359Smckusick } 3184359Smckusick 3195375Smckusic /* 3205375Smckusic * Determine whether a fragment can be extended. 3215375Smckusic * 3225375Smckusic * Check to see if the necessary fragments are available, and 3235375Smckusic * if they are, allocate them. 3245375Smckusic */ 3254359Smckusick daddr_t 3264463Smckusic fragextend(dev, fs, cg, bprev, osize, nsize) 3274426Smckusic dev_t dev; 3284426Smckusic register struct fs *fs; 3294426Smckusic int cg; 3304463Smckusic long bprev; 3314426Smckusic int osize, nsize; 3324426Smckusic { 3334463Smckusic register struct buf *bp; 3344463Smckusic register struct cg *cgp; 3354463Smckusic long bno; 3364463Smckusic int frags, bbase; 3374426Smckusic int i; 3384426Smckusic 339*5960Smckusic frags = numfrags(fs, nsize); 340*5960Smckusic bbase = fragoff(fs, bprev); 3415322Smckusic if (bbase > (bprev + frags - 1) % fs->fs_frag) { 3424463Smckusic /* cannot extend across a block boundry */ 3434463Smckusic return (0); 3444463Smckusic } 3455377Smckusic bp = bread(dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize); 346*5960Smckusic if (bp->b_flags & B_ERROR) { 347*5960Smckusic brelse(bp); 348*5960Smckusic return 0; 349*5960Smckusic } 3504426Smckusic cgp = bp->b_un.b_cg; 3515377Smckusic bno = dtogd(fs, bprev); 352*5960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 3535361Smckusic if (isclr(cgp->cg_free, bno + i)) { 3545361Smckusic brelse(bp); 3555361Smckusic return (0); 3565361Smckusic } 3575361Smckusic /* 3585361Smckusic * the current fragment can be extended 3595361Smckusic * deduct the count on fragment being extended into 3605361Smckusic * increase the count on the remaining fragment (if any) 3615361Smckusic * allocate the extended piece 3625361Smckusic */ 3635361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 3644463Smckusic if (isclr(cgp->cg_free, bno + i)) 3654463Smckusic break; 366*5960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 3675361Smckusic if (i != frags) 3685361Smckusic cgp->cg_frsum[i - frags]++; 369*5960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 3705361Smckusic clrbit(cgp->cg_free, bno + i); 3715361Smckusic cgp->cg_cs.cs_nffree--; 3725361Smckusic fs->fs_cstotal.cs_nffree--; 3735361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 3744463Smckusic } 3755361Smckusic fs->fs_fmod++; 3765361Smckusic bdwrite(bp); 3775361Smckusic return (bprev); 3784426Smckusic } 3794426Smckusic 3805375Smckusic /* 3815375Smckusic * Determine whether a block can be allocated. 3825375Smckusic * 3835375Smckusic * Check to see if a block of the apprpriate size is available, 3845375Smckusic * and if it is, allocate it. 3855375Smckusic */ 3864426Smckusic daddr_t 3874359Smckusick alloccg(dev, fs, cg, bpref, size) 3884359Smckusick dev_t dev; 3894463Smckusic register struct fs *fs; 3904359Smckusick int cg; 3914359Smckusick daddr_t bpref; 3924359Smckusick int size; 3934359Smckusick { 3944463Smckusic register struct buf *bp; 3954463Smckusic register struct cg *cgp; 3964463Smckusic int bno, frags; 3974463Smckusic int allocsiz; 3984463Smckusic register int i; 3994359Smckusick 4005322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 4014792Smckusic return (0); 4025377Smckusic bp = bread(dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize); 403*5960Smckusic if (bp->b_flags & B_ERROR) { 404*5960Smckusic brelse(bp); 405*5960Smckusic return 0; 406*5960Smckusic } 4074359Smckusick cgp = bp->b_un.b_cg; 4085322Smckusic if (size == fs->fs_bsize) { 4095212Smckusic bno = alloccgblk(fs, cgp, bpref); 4104463Smckusic bdwrite(bp); 4114463Smckusic return (bno); 4124463Smckusic } 4134463Smckusic /* 4144463Smckusic * check to see if any fragments are already available 4154463Smckusic * allocsiz is the size which will be allocated, hacking 4164463Smckusic * it down to a smaller size if necessary 4174463Smckusic */ 418*5960Smckusic frags = numfrags(fs, size); 4195322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 4204463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 4214463Smckusic break; 4225322Smckusic if (allocsiz == fs->fs_frag) { 4234463Smckusic /* 4244463Smckusic * no fragments were available, so a block will be 4254463Smckusic * allocated, and hacked up 4264463Smckusic */ 4274792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 4284463Smckusic brelse(bp); 4294463Smckusic return (0); 4304463Smckusic } 4315212Smckusic bno = alloccgblk(fs, cgp, bpref); 4325377Smckusic bpref = dtogd(fs, bno); 4335322Smckusic for (i = frags; i < fs->fs_frag; i++) 4344463Smckusic setbit(cgp->cg_free, bpref + i); 4355322Smckusic i = fs->fs_frag - frags; 4364792Smckusic cgp->cg_cs.cs_nffree += i; 4374792Smckusic fs->fs_cstotal.cs_nffree += i; 4385322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 4394463Smckusic cgp->cg_frsum[i]++; 4404463Smckusic bdwrite(bp); 4414463Smckusic return (bno); 4424463Smckusic } 4434651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 4444651Smckusic if (bno == 0) 4454651Smckusic return (0); 4464463Smckusic for (i = 0; i < frags; i++) 4474463Smckusic clrbit(cgp->cg_free, bno + i); 4484792Smckusic cgp->cg_cs.cs_nffree -= frags; 4494792Smckusic fs->fs_cstotal.cs_nffree -= frags; 4505322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 4514463Smckusic cgp->cg_frsum[allocsiz]--; 4524463Smckusic if (frags != allocsiz) 4534463Smckusic cgp->cg_frsum[allocsiz - frags]++; 4544463Smckusic bdwrite(bp); 4554463Smckusic return (cg * fs->fs_fpg + bno); 4564463Smckusic } 4574463Smckusic 4585375Smckusic /* 4595375Smckusic * Allocate a block in a cylinder group. 4605375Smckusic * 4615375Smckusic * This algorithm implements the following policy: 4625375Smckusic * 1) allocate the requested block. 4635375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 4645375Smckusic * 3) allocate the next available block on the block rotor for the 4655375Smckusic * specified cylinder group. 4665375Smckusic * Note that this routine only allocates fs_bsize blocks; these 4675375Smckusic * blocks may be fragmented by the routine that allocates them. 4685375Smckusic */ 4694463Smckusic daddr_t 4705212Smckusic alloccgblk(fs, cgp, bpref) 4714463Smckusic struct fs *fs; 4724463Smckusic register struct cg *cgp; 4734463Smckusic daddr_t bpref; 4744463Smckusic { 4754651Smckusic daddr_t bno; 4764651Smckusic int cylno, pos; 4774651Smckusic short *cylbp; 4785361Smckusic register int i; 4794463Smckusic 4804651Smckusic if (bpref == 0) { 4814651Smckusic bpref = cgp->cg_rotor; 4825361Smckusic goto norot; 4835361Smckusic } 4845361Smckusic bpref &= ~(fs->fs_frag - 1); 4855377Smckusic bpref = dtogd(fs, bpref); 4865361Smckusic /* 4875361Smckusic * if the requested block is available, use it 4885361Smckusic */ 4895361Smckusic if (isblock(fs, cgp->cg_free, bpref/fs->fs_frag)) { 4905361Smckusic bno = bpref; 4915361Smckusic goto gotit; 4925361Smckusic } 4935361Smckusic /* 4945361Smckusic * check for a block available on the same cylinder 4955361Smckusic */ 4965361Smckusic cylno = cbtocylno(fs, bpref); 4975375Smckusic if (cgp->cg_btot[cylno] == 0) 4985375Smckusic goto norot; 4995375Smckusic if (fs->fs_cpc == 0) { 5005375Smckusic /* 5015375Smckusic * block layout info is not available, so just have 5025375Smckusic * to take any block in this cylinder. 5035375Smckusic */ 5045375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 5055375Smckusic goto norot; 5065375Smckusic } 5075375Smckusic /* 5085375Smckusic * find a block that is rotationally optimal 5095375Smckusic */ 5105361Smckusic cylbp = cgp->cg_b[cylno]; 5115361Smckusic if (fs->fs_rotdelay == 0) { 5125361Smckusic pos = cbtorpos(fs, bpref); 5134651Smckusic } else { 5144651Smckusic /* 5155361Smckusic * here we convert ms of delay to frags as: 5165361Smckusic * (frags) = (ms) * (rev/sec) * (sect/rev) / 5175361Smckusic * ((sect/frag) * (ms/sec)) 5185361Smckusic * then round up to the next rotational position 5194651Smckusic */ 5205361Smckusic bpref += fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 5215361Smckusic (NSPF(fs) * 1000); 5225361Smckusic pos = cbtorpos(fs, bpref); 5235361Smckusic pos = (pos + 1) % NRPOS; 5245361Smckusic } 5255361Smckusic /* 5265361Smckusic * check the summary information to see if a block is 5275361Smckusic * available in the requested cylinder starting at the 5285361Smckusic * optimal rotational position and proceeding around. 5295361Smckusic */ 5305361Smckusic for (i = pos; i < NRPOS; i++) 5315361Smckusic if (cylbp[i] > 0) 5325361Smckusic break; 5335361Smckusic if (i == NRPOS) 5345361Smckusic for (i = 0; i < pos; i++) 5355361Smckusic if (cylbp[i] > 0) 5365361Smckusic break; 5375361Smckusic if (cylbp[i] > 0) { 5384651Smckusic /* 5395361Smckusic * found a rotational position, now find the actual 5405361Smckusic * block. A panic if none is actually there. 5414651Smckusic */ 5425361Smckusic pos = cylno % fs->fs_cpc; 5435361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 5445361Smckusic if (fs->fs_postbl[pos][i] == -1) 5455361Smckusic panic("alloccgblk: cyl groups corrupted"); 5465361Smckusic for (i = fs->fs_postbl[pos][i]; ; i += fs->fs_rotbl[i]) { 5475361Smckusic if (isblock(fs, cgp->cg_free, bno + i)) { 5485361Smckusic bno = (bno + i) * fs->fs_frag; 5495361Smckusic goto gotit; 5505361Smckusic } 5515361Smckusic if (fs->fs_rotbl[i] == 0) 5524651Smckusic break; 5534651Smckusic } 5545361Smckusic panic("alloccgblk: can't find blk in cyl"); 5554359Smckusick } 5565361Smckusic norot: 5575361Smckusic /* 5585361Smckusic * no blocks in the requested cylinder, so take next 5595361Smckusic * available one in this cylinder group. 5605361Smckusic */ 5615322Smckusic bno = mapsearch(fs, cgp, bpref, fs->fs_frag); 5624651Smckusic if (bno == 0) 5634651Smckusic return (0); 5644651Smckusic cgp->cg_rotor = bno; 5654359Smckusick gotit: 5665322Smckusic clrblock(fs, cgp->cg_free, bno/fs->fs_frag); 5674792Smckusic cgp->cg_cs.cs_nbfree--; 5684792Smckusic fs->fs_cstotal.cs_nbfree--; 5695322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 5705375Smckusic cylno = cbtocylno(fs, bno); 5715375Smckusic cgp->cg_b[cylno][cbtorpos(fs, bno)]--; 5725375Smckusic cgp->cg_btot[cylno]--; 5734359Smckusick fs->fs_fmod++; 5744651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 5754359Smckusick } 5764359Smckusick 5775375Smckusic /* 5785375Smckusic * Determine whether an inode can be allocated. 5795375Smckusic * 5805375Smckusic * Check to see if an inode is available, and if it is, 5815375Smckusic * allocate it using the following policy: 5825375Smckusic * 1) allocate the requested inode. 5835375Smckusic * 2) allocate the next available inode after the requested 5845375Smckusic * inode in the specified cylinder group. 5855375Smckusic */ 5865212Smckusic ino_t 5874359Smckusick ialloccg(dev, fs, cg, ipref, mode) 5884359Smckusick dev_t dev; 5894463Smckusic register struct fs *fs; 5904359Smckusick int cg; 5914359Smckusick daddr_t ipref; 5924359Smckusick int mode; 5934359Smckusick { 5944463Smckusic register struct buf *bp; 5954463Smckusic register struct cg *cgp; 5964359Smckusick int i; 5974359Smckusick 5985322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 5994792Smckusic return (0); 6005377Smckusic bp = bread(dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize); 601*5960Smckusic if (bp->b_flags & B_ERROR) { 602*5960Smckusic brelse(bp); 603*5960Smckusic return 0; 604*5960Smckusic } 6054359Smckusick cgp = bp->b_un.b_cg; 6064359Smckusick if (ipref) { 6074359Smckusick ipref %= fs->fs_ipg; 6084359Smckusick if (isclr(cgp->cg_iused, ipref)) 6094359Smckusick goto gotit; 6104359Smckusick } else 6114359Smckusick ipref = cgp->cg_irotor; 6124359Smckusick for (i = 0; i < fs->fs_ipg; i++) { 6134359Smckusick ipref++; 6144359Smckusick if (ipref >= fs->fs_ipg) 6154359Smckusick ipref = 0; 6164359Smckusick if (isclr(cgp->cg_iused, ipref)) { 6174359Smckusick cgp->cg_irotor = ipref; 6184359Smckusick goto gotit; 6194359Smckusick } 6204359Smckusick } 6214359Smckusick brelse(bp); 6224359Smckusick return (0); 6234359Smckusick gotit: 6244359Smckusick setbit(cgp->cg_iused, ipref); 6254792Smckusic cgp->cg_cs.cs_nifree--; 6264792Smckusic fs->fs_cstotal.cs_nifree--; 6275322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 6284359Smckusick fs->fs_fmod++; 6294359Smckusick if ((mode & IFMT) == IFDIR) { 6304792Smckusic cgp->cg_cs.cs_ndir++; 6314792Smckusic fs->fs_cstotal.cs_ndir++; 6325322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 6334359Smckusick } 6344359Smckusick bdwrite(bp); 6354359Smckusick return (cg * fs->fs_ipg + ipref); 6364359Smckusick } 6374359Smckusick 6385375Smckusic /* 6395375Smckusic * Free a block or fragment. 6405375Smckusic * 6415375Smckusic * The specified block or fragment is placed back in the 6425375Smckusic * free map. If a fragment is deallocated, a possible 6435375Smckusic * block reassembly is checked. 6445375Smckusic */ 6454359Smckusick fre(dev, bno, size) 6464359Smckusick dev_t dev; 6474359Smckusick daddr_t bno; 6485212Smckusic off_t size; 6494359Smckusick { 6504359Smckusick register struct fs *fs; 6514359Smckusick register struct cg *cgp; 6524359Smckusick register struct buf *bp; 6534463Smckusic int cg, blk, frags, bbase; 6544463Smckusic register int i; 6554359Smckusick 6565322Smckusic fs = getfs(dev); 657*5960Smckusic if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) 6584426Smckusic panic("free: bad size"); 6595377Smckusic cg = dtog(fs, bno); 6604359Smckusick if (badblock(fs, bno)) 6614359Smckusick return; 6625377Smckusic bp = bread(dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize); 663*5960Smckusic if (bp->b_flags & B_ERROR) { 664*5960Smckusic brelse(bp); 6654359Smckusick return; 666*5960Smckusic } 6674359Smckusick cgp = bp->b_un.b_cg; 6685377Smckusic bno = dtogd(fs, bno); 6695322Smckusic if (size == fs->fs_bsize) { 6705322Smckusic if (isblock(fs, cgp->cg_free, bno/fs->fs_frag)) 6714426Smckusic panic("free: freeing free block"); 6725322Smckusic setblock(fs, cgp->cg_free, bno/fs->fs_frag); 6734792Smckusic cgp->cg_cs.cs_nbfree++; 6744792Smckusic fs->fs_cstotal.cs_nbfree++; 6755322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 6765375Smckusic i = cbtocylno(fs, bno); 6775375Smckusic cgp->cg_b[i][cbtorpos(fs, bno)]++; 6785375Smckusic cgp->cg_btot[i]++; 6794426Smckusic } else { 6805322Smckusic bbase = bno - (bno % fs->fs_frag); 6814463Smckusic /* 6824463Smckusic * decrement the counts associated with the old frags 6834463Smckusic */ 6844463Smckusic blk = ((cgp->cg_free[bbase / NBBY] >> (bbase % NBBY)) & 6855322Smckusic (0xff >> (NBBY - fs->fs_frag))); 6865322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 6874463Smckusic /* 6884463Smckusic * deallocate the fragment 6894463Smckusic */ 690*5960Smckusic frags = numfrags(fs, size); 6914463Smckusic for (i = 0; i < frags; i++) { 6924426Smckusic if (isset(cgp->cg_free, bno + i)) 6934426Smckusic panic("free: freeing free frag"); 6944426Smckusic setbit(cgp->cg_free, bno + i); 6954792Smckusic cgp->cg_cs.cs_nffree++; 6964792Smckusic fs->fs_cstotal.cs_nffree++; 6975322Smckusic fs->fs_cs(fs, cg).cs_nffree++; 6984426Smckusic } 6994463Smckusic /* 7004463Smckusic * add back in counts associated with the new frags 7014463Smckusic */ 7024463Smckusic blk = ((cgp->cg_free[bbase / NBBY] >> (bbase % NBBY)) & 7035322Smckusic (0xff >> (NBBY - fs->fs_frag))); 7045322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 7054463Smckusic /* 7064463Smckusic * if a complete block has been reassembled, account for it 7074463Smckusic */ 7085322Smckusic if (isblock(fs, cgp->cg_free, bbase / fs->fs_frag)) { 7095322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 7105322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 7115322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 7124792Smckusic cgp->cg_cs.cs_nbfree++; 7134792Smckusic fs->fs_cstotal.cs_nbfree++; 7145322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 7155375Smckusic i = cbtocylno(fs, bbase); 7165375Smckusic cgp->cg_b[i][cbtorpos(fs, bbase)]++; 7175375Smckusic cgp->cg_btot[i]++; 7184426Smckusic } 7194426Smckusic } 7204359Smckusick fs->fs_fmod++; 7214359Smckusick bdwrite(bp); 7224359Smckusick } 7234359Smckusick 7245375Smckusic /* 7255375Smckusic * Free an inode. 7265375Smckusic * 7275375Smckusic * The specified inode is placed back in the free map. 7285375Smckusic */ 7294359Smckusick ifree(dev, ino, mode) 7304359Smckusick dev_t dev; 7314359Smckusick ino_t ino; 7324359Smckusick int mode; 7334359Smckusick { 7344359Smckusick register struct fs *fs; 7354359Smckusick register struct cg *cgp; 7364359Smckusick register struct buf *bp; 7374359Smckusick int cg; 7384359Smckusick 7394359Smckusick fs = getfs(dev); 7404359Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) 7414359Smckusick panic("ifree: range"); 7425377Smckusic cg = itog(fs, ino); 7435377Smckusic bp = bread(dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize); 744*5960Smckusic if (bp->b_flags & B_ERROR) { 745*5960Smckusic brelse(bp); 7464359Smckusick return; 747*5960Smckusic } 7484359Smckusick cgp = bp->b_un.b_cg; 7494359Smckusick ino %= fs->fs_ipg; 7504359Smckusick if (isclr(cgp->cg_iused, ino)) 7514359Smckusick panic("ifree: freeing free inode"); 7524359Smckusick clrbit(cgp->cg_iused, ino); 7534792Smckusic cgp->cg_cs.cs_nifree++; 7544792Smckusic fs->fs_cstotal.cs_nifree++; 7555322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 7564359Smckusick if ((mode & IFMT) == IFDIR) { 7574792Smckusic cgp->cg_cs.cs_ndir--; 7584792Smckusic fs->fs_cstotal.cs_ndir--; 7595322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 7604359Smckusick } 7614359Smckusick fs->fs_fmod++; 7624359Smckusick bdwrite(bp); 7634359Smckusick } 7644359Smckusick 7654463Smckusic /* 7665375Smckusic * Find a block of the specified size in the specified cylinder group. 7675375Smckusic * 7684651Smckusic * It is a panic if a request is made to find a block if none are 7694651Smckusic * available. 7704651Smckusic */ 7714651Smckusic daddr_t 7724651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 7734651Smckusic register struct fs *fs; 7744651Smckusic register struct cg *cgp; 7754651Smckusic daddr_t bpref; 7764651Smckusic int allocsiz; 7774651Smckusic { 7784651Smckusic daddr_t bno; 7794651Smckusic int start, len, loc, i; 7804651Smckusic int blk, field, subfield, pos; 7814651Smckusic 7824651Smckusic /* 7834651Smckusic * find the fragment by searching through the free block 7844651Smckusic * map for an appropriate bit pattern 7854651Smckusic */ 7864651Smckusic if (bpref) 7875377Smckusic start = dtogd(fs, bpref) / NBBY; 7884651Smckusic else 7894651Smckusic start = cgp->cg_frotor / NBBY; 7905398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 7915322Smckusic loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag], 7925322Smckusic 1 << (allocsiz - 1)); 7934651Smckusic if (loc == 0) { 7945398Smckusic loc = fs->fs_dblkno / NBBY; 7955398Smckusic len = start - loc + 1; 7965398Smckusic start = loc; 7975322Smckusic loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag], 7984651Smckusic 1 << (allocsiz - 1)); 7994651Smckusic if (loc == 0) { 8004651Smckusic panic("alloccg: map corrupted"); 8014651Smckusic return (0); 8024651Smckusic } 8034651Smckusic } 8044651Smckusic bno = (start + len - loc) * NBBY; 8054651Smckusic cgp->cg_frotor = bno; 8064651Smckusic /* 8074651Smckusic * found the byte in the map 8084651Smckusic * sift through the bits to find the selected frag 8094651Smckusic */ 8105322Smckusic for (i = 0; i < NBBY; i += fs->fs_frag) { 8115322Smckusic blk = (cgp->cg_free[bno / NBBY] >> i) & 8125322Smckusic (0xff >> NBBY - fs->fs_frag); 8134651Smckusic blk <<= 1; 8144651Smckusic field = around[allocsiz]; 8154651Smckusic subfield = inside[allocsiz]; 8165322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 8174651Smckusic if ((blk & field) == subfield) { 8184651Smckusic return (bno + i + pos); 8194651Smckusic } 8204651Smckusic field <<= 1; 8214651Smckusic subfield <<= 1; 8224651Smckusic } 8234651Smckusic } 8244651Smckusic panic("alloccg: block not in map"); 8254651Smckusic return (0); 8264651Smckusic } 8274651Smckusic 8284651Smckusic /* 8295375Smckusic * Update the frsum fields to reflect addition or deletion 8305375Smckusic * of some frags. 8314463Smckusic */ 8325322Smckusic fragacct(fs, fragmap, fraglist, cnt) 8335322Smckusic struct fs *fs; 8344472Smckusic int fragmap; 8354792Smckusic long fraglist[]; 8364463Smckusic int cnt; 8374463Smckusic { 8384463Smckusic int inblk; 8394463Smckusic register int field, subfield; 8404463Smckusic register int siz, pos; 8414463Smckusic 8425322Smckusic inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 8434463Smckusic fragmap <<= 1; 8445322Smckusic for (siz = 1; siz < fs->fs_frag; siz++) { 8454463Smckusic if (((1 << siz) & inblk) == 0) 8464463Smckusic continue; 8474463Smckusic field = around[siz]; 8484463Smckusic subfield = inside[siz]; 8495322Smckusic for (pos = siz; pos <= fs->fs_frag; pos++) { 8504463Smckusic if ((fragmap & field) == subfield) { 8514463Smckusic fraglist[siz] += cnt; 8524463Smckusic pos += siz; 8534463Smckusic field <<= siz; 8544463Smckusic subfield <<= siz; 8554463Smckusic } 8564463Smckusic field <<= 1; 8574463Smckusic subfield <<= 1; 8584463Smckusic } 8594463Smckusic } 8604463Smckusic } 8614463Smckusic 8625375Smckusic /* 8635375Smckusic * Check that a specified block number is in range. 8645375Smckusic */ 8654359Smckusick badblock(fs, bn) 8664359Smckusick register struct fs *fs; 8674359Smckusick daddr_t bn; 8684359Smckusick { 8694359Smckusick 8705377Smckusic if ((unsigned)bn >= fs->fs_size || bn < cgdmin(fs, dtog(fs, bn))) { 8714359Smckusick fserr(fs, "bad block"); 8724359Smckusick return (1); 8734359Smckusick } 8744359Smckusick return (0); 8754359Smckusick } 8764359Smckusick 8774359Smckusick /* 8785375Smckusic * Getfs maps a device number into a pointer to the incore super block. 8794359Smckusick * 8805375Smckusic * The algorithm is a linear search through the mount table. A 8815375Smckusic * consistency check of the super block magic number is performed. 8825375Smckusic * 8834359Smckusick * panic: no fs -- the device is not mounted. 8844359Smckusick * this "cannot happen" 8854359Smckusick */ 8864359Smckusick struct fs * 8874359Smckusick getfs(dev) 8884359Smckusick dev_t dev; 8894359Smckusick { 8904359Smckusick register struct mount *mp; 8914359Smckusick register struct fs *fs; 8924359Smckusick 8934359Smckusick for (mp = &mount[0]; mp < &mount[NMOUNT]; mp++) 8944359Smckusick if (mp->m_bufp != NULL && mp->m_dev == dev) { 8954359Smckusick fs = mp->m_bufp->b_un.b_fs; 8964359Smckusick if (fs->fs_magic != FS_MAGIC) 8974359Smckusick panic("getfs: bad magic"); 8984359Smckusick return (fs); 8994359Smckusick } 9004359Smckusick panic("getfs: no fs"); 9014359Smckusick return (NULL); 9024359Smckusick } 9034359Smckusick 9044359Smckusick /* 9055375Smckusic * Fserr prints the name of a file system with an error diagnostic. 9065375Smckusic * 9075375Smckusic * The form of the error message is: 9084359Smckusick * fs: error message 9094359Smckusick */ 9104359Smckusick fserr(fs, cp) 9114359Smckusick struct fs *fs; 9124359Smckusick char *cp; 9134359Smckusick { 9144359Smckusick 9154359Smckusick printf("%s: %s\n", fs->fs_fsmnt, cp); 9164359Smckusick } 9174359Smckusick 9184359Smckusick /* 9194359Smckusick * Getfsx returns the index in the file system 9204359Smckusick * table of the specified device. The swap device 9214359Smckusick * is also assigned a pseudo-index. The index may 9224359Smckusick * be used as a compressed indication of the location 9234359Smckusick * of a block, recording 9244359Smckusick * <getfsx(dev),blkno> 9254359Smckusick * rather than 9264359Smckusick * <dev, blkno> 9274359Smckusick * provided the information need remain valid only 9284359Smckusick * as long as the file system is mounted. 9294359Smckusick */ 9304359Smckusick getfsx(dev) 9314359Smckusick dev_t dev; 9324359Smckusick { 9334359Smckusick register struct mount *mp; 9344359Smckusick 9354359Smckusick if (dev == swapdev) 9364359Smckusick return (MSWAPX); 9374359Smckusick for(mp = &mount[0]; mp < &mount[NMOUNT]; mp++) 9384359Smckusick if (mp->m_dev == dev) 9394359Smckusick return (mp - &mount[0]); 9404359Smckusick return (-1); 9414359Smckusick } 9424359Smckusick 9434359Smckusick /* 9444359Smckusick * Update is the internal name of 'sync'. It goes through the disk 9454359Smckusick * queues to initiate sandbagged IO; goes through the inodes to write 9465375Smckusic * modified nodes; and it goes through the mount table to initiate 9475375Smckusic * the writing of the modified super blocks. 9484359Smckusick */ 9494359Smckusick update() 9504359Smckusick { 9514359Smckusick register struct inode *ip; 9524359Smckusick register struct mount *mp; 9534359Smckusick register struct buf *bp; 9544359Smckusick struct fs *fs; 9554359Smckusick time_t tim; 9564651Smckusic int i, blks; 9574359Smckusick 9584359Smckusick if (updlock) 9594359Smckusick return; 9604359Smckusick updlock++; 9614359Smckusick /* 9624359Smckusick * Write back modified superblocks. 9634359Smckusick * Consistency check that the superblock 9644359Smckusick * of each file system is still in the buffer cache. 9654359Smckusick */ 9664359Smckusick for (mp = &mount[0]; mp < &mount[NMOUNT]; mp++) 9674359Smckusick if (mp->m_bufp != NULL) { 9684359Smckusick fs = mp->m_bufp->b_un.b_fs; 9694359Smckusick if (fs->fs_fmod == 0) 9704359Smckusick continue; 9714359Smckusick if (fs->fs_ronly != 0) 9724359Smckusick panic("update: rofs mod"); 9735344Smckusic bp = getblk(mp->m_dev, SBLOCK, SBSIZE); 9744359Smckusick fs->fs_fmod = 0; 9754359Smckusick fs->fs_time = TIME; 9764359Smckusick if (bp->b_un.b_fs != fs) 9774359Smckusick panic("update: bad b_fs"); 9784359Smckusick bwrite(bp); 9795322Smckusic blks = howmany(fs->fs_cssize, fs->fs_bsize); 9804651Smckusic for (i = 0; i < blks; i++) { 9815322Smckusic bp = getblk(mp->m_dev, 9825322Smckusic fsbtodb(fs, fs->fs_csaddr + (i * fs->fs_frag)), 9835322Smckusic fs->fs_bsize); 9844359Smckusick bwrite(bp); 9854359Smckusick } 9864359Smckusick } 9874359Smckusick /* 9884359Smckusick * Write back each (modified) inode. 9894359Smckusick */ 9904359Smckusick for (ip = inode; ip < inodeNINODE; ip++) 9914359Smckusick if((ip->i_flag&ILOCK)==0 && ip->i_count) { 9924359Smckusick ip->i_flag |= ILOCK; 9934359Smckusick ip->i_count++; 9944359Smckusick tim = TIME; 9954359Smckusick iupdat(ip, &tim, &tim, 0); 9964359Smckusick iput(ip); 9974359Smckusick } 9984359Smckusick updlock = 0; 9994359Smckusick /* 10004359Smckusick * Force stale buffer cache information to be flushed, 10014359Smckusick * for all devices. 10024359Smckusick */ 10034359Smckusick bflush(NODEV); 10044359Smckusick } 10055322Smckusic 10065322Smckusic /* 10075375Smckusic * block operations 10085375Smckusic * 10095375Smckusic * check if a block is available 10105322Smckusic */ 10115322Smckusic isblock(fs, cp, h) 10125322Smckusic struct fs *fs; 10135322Smckusic unsigned char *cp; 10145322Smckusic int h; 10155322Smckusic { 10165322Smckusic unsigned char mask; 10175322Smckusic 10185322Smckusic switch (fs->fs_frag) { 10195322Smckusic case 8: 10205322Smckusic return (cp[h] == 0xff); 10215322Smckusic case 4: 10225322Smckusic mask = 0x0f << ((h & 0x1) << 2); 10235322Smckusic return ((cp[h >> 1] & mask) == mask); 10245322Smckusic case 2: 10255322Smckusic mask = 0x03 << ((h & 0x3) << 1); 10265322Smckusic return ((cp[h >> 2] & mask) == mask); 10275322Smckusic case 1: 10285322Smckusic mask = 0x01 << (h & 0x7); 10295322Smckusic return ((cp[h >> 3] & mask) == mask); 10305322Smckusic default: 10315322Smckusic panic("isblock bad fs_frag"); 10325322Smckusic return; 10335322Smckusic } 10345322Smckusic } 10355375Smckusic 10365375Smckusic /* 10375375Smckusic * take a block out of the map 10385375Smckusic */ 10395322Smckusic clrblock(fs, cp, h) 10405322Smckusic struct fs *fs; 10415322Smckusic unsigned char *cp; 10425322Smckusic int h; 10435322Smckusic { 10445322Smckusic switch ((fs)->fs_frag) { 10455322Smckusic case 8: 10465322Smckusic cp[h] = 0; 10475322Smckusic return; 10485322Smckusic case 4: 10495322Smckusic cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 10505322Smckusic return; 10515322Smckusic case 2: 10525322Smckusic cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 10535322Smckusic return; 10545322Smckusic case 1: 10555322Smckusic cp[h >> 3] &= ~(0x01 << (h & 0x7)); 10565322Smckusic return; 10575322Smckusic default: 10585322Smckusic panic("clrblock bad fs_frag"); 10595322Smckusic return; 10605322Smckusic } 10615322Smckusic } 10625375Smckusic 10635375Smckusic /* 10645375Smckusic * put a block into the map 10655375Smckusic */ 10665322Smckusic setblock(fs, cp, h) 10675322Smckusic struct fs *fs; 10685322Smckusic unsigned char *cp; 10695322Smckusic int h; 10705322Smckusic { 10715322Smckusic switch (fs->fs_frag) { 10725322Smckusic case 8: 10735322Smckusic cp[h] = 0xff; 10745322Smckusic return; 10755322Smckusic case 4: 10765322Smckusic cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 10775322Smckusic return; 10785322Smckusic case 2: 10795322Smckusic cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 10805322Smckusic return; 10815322Smckusic case 1: 10825322Smckusic cp[h >> 3] |= (0x01 << (h & 0x7)); 10835322Smckusic return; 10845322Smckusic default: 10855322Smckusic panic("setblock bad fs_frag"); 10865322Smckusic return; 10875322Smckusic } 10885322Smckusic } 1089