16497Smckusick /* alloc.c 4.1 82/03/25 */ 24359Smckusick 3*6531Smckusick /* merged into kernel: @(#)lfs_alloc.c 2.3 04/11/82 */ 44359Smckusick 56497Smckusick /* last monet version: 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" 146310Smckusick #include "../h/ndir.h" 154359Smckusick #include "../h/user.h" 164359Smckusick 175212Smckusic extern u_long hashalloc(); 185212Smckusic extern ino_t ialloccg(); 194651Smckusic extern daddr_t alloccg(); 204651Smckusic extern daddr_t alloccgblk(); 214651Smckusic extern daddr_t fragextend(); 224651Smckusic extern daddr_t blkpref(); 234651Smckusic extern daddr_t mapsearch(); 244607Smckusic extern int inside[], around[]; 255322Smckusic extern unsigned char *fragtbl[]; 264359Smckusick 275375Smckusic /* 285375Smckusic * Allocate a block in the file system. 295375Smckusic * 305375Smckusic * The size of the requested block is given, which must be some 315375Smckusic * multiple of fs_fsize and <= fs_bsize. 325375Smckusic * A preference may be optionally specified. If a preference is given 335375Smckusic * the following hierarchy is used to allocate a block: 345375Smckusic * 1) allocate the requested block. 355375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 365375Smckusic * 3) allocate a block in the same cylinder group. 375375Smckusic * 4) quadradically rehash into other cylinder groups, until an 385375Smckusic * available block is located. 395375Smckusic * If no block preference is given the following heirarchy is used 405375Smckusic * to allocate a block: 415375Smckusic * 1) allocate a block in the cylinder group that contains the 425375Smckusic * inode for the file. 435375Smckusic * 2) quadradically rehash into other cylinder groups, until an 445375Smckusic * available block is located. 455375Smckusic */ 464359Smckusick struct buf * 475965Smckusic alloc(ip, bpref, size) 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 575965Smckusic fs = ip->i_fs; 585960Smckusic 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); 725965Smckusic bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, alloccg); 734359Smckusick if (bno == 0) 744359Smckusick goto nospace; 755965Smckusic bp = getblk(ip->i_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 * 945965Smckusic realloccg(ip, bprev, bpref, osize, nsize) 955965Smckusic register struct inode *ip; 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 1055965Smckusic fs = ip->i_fs; 1065960Smckusic if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 1075960Smckusic (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; 1136294Smckusick if (bprev == 0) 1144463Smckusic panic("realloccg: bad bprev"); 1156294Smckusick cg = dtog(fs, bprev); 1165965Smckusic bno = fragextend(ip, cg, (long)bprev, osize, nsize); 1174463Smckusic if (bno != 0) { 1185965Smckusic bp = bread(ip->i_dev, fsbtodb(fs, bno), osize); 1195960Smckusic if (bp->b_flags & B_ERROR) { 1205960Smckusic brelse(bp); 1216294Smckusick return (NULL); 1225960Smckusic } 1234463Smckusic bp->b_bcount = nsize; 1244463Smckusic blkclr(bp->b_un.b_addr + osize, nsize - osize); 1254463Smckusic return (bp); 1264463Smckusic } 1274948Smckusic if (bpref >= fs->fs_size) 1284948Smckusic bpref = 0; 1295965Smckusic bno = (daddr_t)hashalloc(ip, cg, (long)bpref, nsize, alloccg); 1304463Smckusic if (bno != 0) { 1314463Smckusic /* 1324463Smckusic * make a new copy 1334463Smckusic */ 1345965Smckusic obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize); 1355960Smckusic if (obp->b_flags & B_ERROR) { 1365960Smckusic brelse(obp); 1376294Smckusick return (NULL); 1385960Smckusic } 1395965Smckusic bp = getblk(ip->i_dev, fsbtodb(fs, bno), nsize); 1404463Smckusic cp = bp->b_un.b_addr; 1414463Smckusic bp->b_un.b_addr = obp->b_un.b_addr; 1424463Smckusic obp->b_un.b_addr = cp; 1434463Smckusic obp->b_flags |= B_INVAL; 1444463Smckusic brelse(obp); 1455965Smckusic fre(ip, bprev, (off_t)osize); 1464463Smckusic blkclr(bp->b_un.b_addr + osize, nsize - osize); 1476294Smckusick return (bp); 1484463Smckusic } 1494792Smckusic nospace: 1504463Smckusic /* 1514463Smckusic * no space available 1524463Smckusic */ 1534426Smckusic fserr(fs, "file system full"); 1544426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 1554426Smckusic u.u_error = ENOSPC; 1564426Smckusic return (NULL); 1574426Smckusic } 1584426Smckusic 1595375Smckusic /* 1605375Smckusic * Allocate an inode in the file system. 1615375Smckusic * 1625375Smckusic * A preference may be optionally specified. If a preference is given 1635375Smckusic * the following hierarchy is used to allocate an inode: 1645375Smckusic * 1) allocate the requested inode. 1655375Smckusic * 2) allocate an inode in the same cylinder group. 1665375Smckusic * 3) quadradically rehash into other cylinder groups, until an 1675375Smckusic * available inode is located. 1685375Smckusic * If no inode preference is given the following heirarchy is used 1695375Smckusic * to allocate an inode: 1705375Smckusic * 1) allocate an inode in cylinder group 0. 1715375Smckusic * 2) quadradically rehash into other cylinder groups, until an 1725375Smckusic * available inode is located. 1735375Smckusic */ 1744359Smckusick struct inode * 1755965Smckusic ialloc(pip, ipref, mode) 1765965Smckusic register struct inode *pip; 1774359Smckusick ino_t ipref; 1784359Smckusick int mode; 1794359Smckusick { 1805212Smckusic ino_t ino; 1814359Smckusick register struct fs *fs; 1824359Smckusick register struct inode *ip; 1834359Smckusick int cg; 1844359Smckusick 1855965Smckusic fs = pip->i_fs; 1864792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 1874359Smckusick goto noinodes; 1884948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 1894948Smckusic ipref = 0; 1905377Smckusic cg = itog(fs, ipref); 1915965Smckusic ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); 1924359Smckusick if (ino == 0) 1934359Smckusick goto noinodes; 1945965Smckusic ip = iget(pip->i_dev, pip->i_fs, ino); 1954359Smckusick if (ip == NULL) { 1965965Smckusic ifree(ip, ino, 0); 1974359Smckusick return (NULL); 1984359Smckusick } 1994359Smckusick if (ip->i_mode) 2004359Smckusick panic("ialloc: dup alloc"); 2014359Smckusick return (ip); 2024359Smckusick noinodes: 2034359Smckusick fserr(fs, "out of inodes"); 2046294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 2054359Smckusick u.u_error = ENOSPC; 2064359Smckusick return (NULL); 2074359Smckusick } 2084359Smckusick 2094651Smckusic /* 2105375Smckusic * Find a cylinder to place a directory. 2115375Smckusic * 2125375Smckusic * The policy implemented by this algorithm is to select from 2135375Smckusic * among those cylinder groups with above the average number of 2145375Smckusic * free inodes, the one with the smallest number of directories. 2154651Smckusic */ 2165965Smckusic dirpref(fs) 2175965Smckusic register struct fs *fs; 2184359Smckusick { 2194651Smckusic int cg, minndir, mincg, avgifree; 2204359Smckusick 2214792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 2224651Smckusic minndir = fs->fs_ipg; 2234359Smckusick mincg = 0; 2244651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 2255322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 2265322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 2274359Smckusick mincg = cg; 2285322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 2294359Smckusick } 2304359Smckusick return (fs->fs_ipg * mincg); 2314359Smckusick } 2324359Smckusick 2334651Smckusic /* 2345375Smckusic * Select a cylinder to place a large block of data. 2355375Smckusic * 2365375Smckusic * The policy implemented by this algorithm is to maintain a 2375375Smckusic * rotor that sweeps the cylinder groups. When a block is 2385375Smckusic * needed, the rotor is advanced until a cylinder group with 2395375Smckusic * greater than the average number of free blocks is found. 2404651Smckusic */ 2415212Smckusic daddr_t 2425965Smckusic blkpref(fs) 2435965Smckusic register struct fs *fs; 2444651Smckusic { 2454651Smckusic int cg, avgbfree; 2464651Smckusic 2474792Smckusic avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 2484651Smckusic for (cg = fs->fs_cgrotor + 1; cg < fs->fs_ncg; cg++) 2495322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 2504651Smckusic fs->fs_cgrotor = cg; 2515322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 2524651Smckusic } 2534651Smckusic for (cg = 0; cg <= fs->fs_cgrotor; 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 } 2586294Smckusick return (NULL); 2594651Smckusic } 2604651Smckusic 2615375Smckusic /* 2625375Smckusic * Implement the cylinder overflow algorithm. 2635375Smckusic * 2645375Smckusic * The policy implemented by this algorithm is: 2655375Smckusic * 1) allocate the block in its requested cylinder group. 2665375Smckusic * 2) quadradically rehash on the cylinder group number. 2675375Smckusic * 3) brute force search for a free block. 2685375Smckusic */ 2695212Smckusic /*VARARGS5*/ 2705212Smckusic u_long 2715965Smckusic hashalloc(ip, cg, pref, size, allocator) 2725965Smckusic struct inode *ip; 2734359Smckusick int cg; 2744359Smckusick long pref; 2754359Smckusick int size; /* size for data blocks, mode for inodes */ 2765212Smckusic u_long (*allocator)(); 2774359Smckusick { 2785965Smckusic register struct fs *fs; 2794359Smckusick long result; 2804359Smckusick int i, icg = cg; 2814359Smckusick 2825965Smckusic fs = ip->i_fs; 2834359Smckusick /* 2844359Smckusick * 1: preferred cylinder group 2854359Smckusick */ 2865965Smckusic result = (*allocator)(ip, cg, pref, size); 2874359Smckusick if (result) 2884359Smckusick return (result); 2894359Smckusick /* 2904359Smckusick * 2: quadratic rehash 2914359Smckusick */ 2924359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 2934359Smckusick cg += i; 2944359Smckusick if (cg >= fs->fs_ncg) 2954359Smckusick cg -= fs->fs_ncg; 2965965Smckusic result = (*allocator)(ip, cg, 0, size); 2974359Smckusick if (result) 2984359Smckusick return (result); 2994359Smckusick } 3004359Smckusick /* 3014359Smckusick * 3: brute force search 3024359Smckusick */ 3034359Smckusick cg = icg; 3044359Smckusick for (i = 0; i < fs->fs_ncg; i++) { 3055965Smckusic result = (*allocator)(ip, cg, 0, size); 3064359Smckusick if (result) 3074359Smckusick return (result); 3084359Smckusick cg++; 3094359Smckusick if (cg == fs->fs_ncg) 3104359Smckusick cg = 0; 3114359Smckusick } 3126294Smckusick return (NULL); 3134359Smckusick } 3144359Smckusick 3155375Smckusic /* 3165375Smckusic * Determine whether a fragment can be extended. 3175375Smckusic * 3185375Smckusic * Check to see if the necessary fragments are available, and 3195375Smckusic * if they are, allocate them. 3205375Smckusic */ 3214359Smckusick daddr_t 3225965Smckusic fragextend(ip, cg, bprev, osize, nsize) 3235965Smckusic struct inode *ip; 3244426Smckusic int cg; 3254463Smckusic long bprev; 3264426Smckusic int osize, nsize; 3274426Smckusic { 3285965Smckusic register struct fs *fs; 3294463Smckusic register struct buf *bp; 3304463Smckusic register struct cg *cgp; 3314463Smckusic long bno; 3324463Smckusic int frags, bbase; 3334426Smckusic int i; 3344426Smckusic 3355965Smckusic fs = ip->i_fs; 336*6531Smckusick if (fs->fs_cs(fs, cg).cs_nffree < nsize - osize) 337*6531Smckusick return (NULL); 3385960Smckusic frags = numfrags(fs, nsize); 3395960Smckusic bbase = fragoff(fs, bprev); 3405322Smckusic if (bbase > (bprev + frags - 1) % fs->fs_frag) { 3414463Smckusic /* cannot extend across a block boundry */ 3426294Smckusick return (NULL); 3434463Smckusic } 3445965Smckusic bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize); 345*6531Smckusick cgp = bp->b_un.b_cg; 346*6531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 3475960Smckusic brelse(bp); 3486294Smckusick return (NULL); 3495960Smckusic } 3505377Smckusic bno = dtogd(fs, bprev); 3515960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 3525361Smckusic if (isclr(cgp->cg_free, bno + i)) { 3535361Smckusic brelse(bp); 3546294Smckusick return (NULL); 3555361Smckusic } 3565361Smckusic /* 3575361Smckusic * the current fragment can be extended 3585361Smckusic * deduct the count on fragment being extended into 3595361Smckusic * increase the count on the remaining fragment (if any) 3605361Smckusic * allocate the extended piece 3615361Smckusic */ 3625361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 3634463Smckusic if (isclr(cgp->cg_free, bno + i)) 3644463Smckusic break; 3655960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 3665361Smckusic if (i != frags) 3675361Smckusic cgp->cg_frsum[i - frags]++; 3685960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 3695361Smckusic clrbit(cgp->cg_free, bno + i); 3705361Smckusic cgp->cg_cs.cs_nffree--; 3715361Smckusic fs->fs_cstotal.cs_nffree--; 3725361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 3734463Smckusic } 3745361Smckusic fs->fs_fmod++; 3755361Smckusic bdwrite(bp); 3765361Smckusic return (bprev); 3774426Smckusic } 3784426Smckusic 3795375Smckusic /* 3805375Smckusic * Determine whether a block can be allocated. 3815375Smckusic * 3825375Smckusic * Check to see if a block of the apprpriate size is available, 3835375Smckusic * and if it is, allocate it. 3845375Smckusic */ 3854426Smckusic daddr_t 3865965Smckusic alloccg(ip, cg, bpref, size) 3875965Smckusic struct inode *ip; 3884359Smckusick int cg; 3894359Smckusick daddr_t bpref; 3904359Smckusick int size; 3914359Smckusick { 3925965Smckusic register struct fs *fs; 3934463Smckusic register struct buf *bp; 3944463Smckusic register struct cg *cgp; 3954463Smckusic int bno, frags; 3964463Smckusic int allocsiz; 3974463Smckusic register int i; 3984359Smckusick 3995965Smckusic fs = ip->i_fs; 4005322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 4016294Smckusick return (NULL); 4025965Smckusic bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize); 403*6531Smckusick cgp = bp->b_un.b_cg; 404*6531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 4055960Smckusic brelse(bp); 4066294Smckusick return (NULL); 4075960Smckusic } 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 */ 4185960Smckusic 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); 4296294Smckusick return (NULL); 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); 444*6531Smckusick if (bno == -1) 4456294Smckusick return (NULL); 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) 4715965Smckusic register struct fs *fs; 4724463Smckusic register struct cg *cgp; 4734463Smckusic daddr_t bpref; 4744463Smckusic { 4754651Smckusic daddr_t bno; 4766294Smckusick int cylno, pos, delta; 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"); 5466294Smckusick for (i = fs->fs_postbl[pos][i];; ) { 5475361Smckusic if (isblock(fs, cgp->cg_free, bno + i)) { 5485361Smckusic bno = (bno + i) * fs->fs_frag; 5495361Smckusic goto gotit; 5505361Smckusic } 5516294Smckusick delta = fs->fs_rotbl[i]; 5526294Smckusick if (delta <= 0 || delta > MAXBPC - i) 5534651Smckusic break; 5546294Smckusick i += delta; 5554651Smckusic } 5565361Smckusic panic("alloccgblk: can't find blk in cyl"); 5574359Smckusick } 5585361Smckusic norot: 5595361Smckusic /* 5605361Smckusic * no blocks in the requested cylinder, so take next 5615361Smckusic * available one in this cylinder group. 5625361Smckusic */ 5635322Smckusic bno = mapsearch(fs, cgp, bpref, fs->fs_frag); 564*6531Smckusick if (bno == -1) 5656294Smckusick return (NULL); 5664651Smckusic cgp->cg_rotor = bno; 5674359Smckusick gotit: 5685322Smckusic clrblock(fs, cgp->cg_free, bno/fs->fs_frag); 5694792Smckusic cgp->cg_cs.cs_nbfree--; 5704792Smckusic fs->fs_cstotal.cs_nbfree--; 5715322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 5725375Smckusic cylno = cbtocylno(fs, bno); 5735375Smckusic cgp->cg_b[cylno][cbtorpos(fs, bno)]--; 5745375Smckusic cgp->cg_btot[cylno]--; 5754359Smckusick fs->fs_fmod++; 5764651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 5774359Smckusick } 5784359Smckusick 5795375Smckusic /* 5805375Smckusic * Determine whether an inode can be allocated. 5815375Smckusic * 5825375Smckusic * Check to see if an inode is available, and if it is, 5835375Smckusic * allocate it using the following policy: 5845375Smckusic * 1) allocate the requested inode. 5855375Smckusic * 2) allocate the next available inode after the requested 5865375Smckusic * inode in the specified cylinder group. 5875375Smckusic */ 5885212Smckusic ino_t 5895965Smckusic ialloccg(ip, cg, ipref, mode) 5905965Smckusic struct inode *ip; 5914359Smckusick int cg; 5924359Smckusick daddr_t ipref; 5934359Smckusick int mode; 5944359Smckusick { 5955965Smckusic register struct fs *fs; 5964463Smckusic register struct buf *bp; 5974463Smckusic register struct cg *cgp; 5984359Smckusick int i; 5994359Smckusick 6005965Smckusic fs = ip->i_fs; 6015322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 6026294Smckusick return (NULL); 6035965Smckusic bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize); 604*6531Smckusick cgp = bp->b_un.b_cg; 605*6531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 6065960Smckusic brelse(bp); 6076294Smckusick return (NULL); 6085960Smckusic } 6094359Smckusick if (ipref) { 6104359Smckusick ipref %= fs->fs_ipg; 6114359Smckusick if (isclr(cgp->cg_iused, ipref)) 6124359Smckusick goto gotit; 6134359Smckusick } else 6144359Smckusick ipref = cgp->cg_irotor; 6154359Smckusick for (i = 0; i < fs->fs_ipg; i++) { 6164359Smckusick ipref++; 6174359Smckusick if (ipref >= fs->fs_ipg) 6184359Smckusick ipref = 0; 6194359Smckusick if (isclr(cgp->cg_iused, ipref)) { 6204359Smckusick cgp->cg_irotor = ipref; 6214359Smckusick goto gotit; 6224359Smckusick } 6234359Smckusick } 6244359Smckusick brelse(bp); 6256294Smckusick return (NULL); 6264359Smckusick gotit: 6274359Smckusick setbit(cgp->cg_iused, ipref); 6284792Smckusic cgp->cg_cs.cs_nifree--; 6294792Smckusic fs->fs_cstotal.cs_nifree--; 6305322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 6314359Smckusick fs->fs_fmod++; 6324359Smckusick if ((mode & IFMT) == IFDIR) { 6334792Smckusic cgp->cg_cs.cs_ndir++; 6344792Smckusic fs->fs_cstotal.cs_ndir++; 6355322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 6364359Smckusick } 6374359Smckusick bdwrite(bp); 6384359Smckusick return (cg * fs->fs_ipg + ipref); 6394359Smckusick } 6404359Smckusick 6415375Smckusic /* 6425375Smckusic * Free a block or fragment. 6435375Smckusic * 6445375Smckusic * The specified block or fragment is placed back in the 6455375Smckusic * free map. If a fragment is deallocated, a possible 6465375Smckusic * block reassembly is checked. 6475375Smckusic */ 6485965Smckusic fre(ip, bno, size) 6495965Smckusic register struct inode *ip; 6504359Smckusick daddr_t bno; 6515212Smckusic off_t size; 6524359Smckusick { 6534359Smckusick register struct fs *fs; 6544359Smckusick register struct cg *cgp; 6554359Smckusick register struct buf *bp; 6564463Smckusic int cg, blk, frags, bbase; 6574463Smckusic register int i; 6584359Smckusick 6595965Smckusic fs = ip->i_fs; 6605960Smckusic if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) 6614426Smckusic panic("free: bad size"); 6625377Smckusic cg = dtog(fs, bno); 6634359Smckusick if (badblock(fs, bno)) 6644359Smckusick return; 6655965Smckusic bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize); 666*6531Smckusick cgp = bp->b_un.b_cg; 667*6531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 6685960Smckusic brelse(bp); 6694359Smckusick return; 6705960Smckusic } 6715377Smckusic bno = dtogd(fs, bno); 6725322Smckusic if (size == fs->fs_bsize) { 6735322Smckusic if (isblock(fs, cgp->cg_free, bno/fs->fs_frag)) 6744426Smckusic panic("free: freeing free block"); 6755322Smckusic setblock(fs, cgp->cg_free, bno/fs->fs_frag); 6764792Smckusic cgp->cg_cs.cs_nbfree++; 6774792Smckusic fs->fs_cstotal.cs_nbfree++; 6785322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 6795375Smckusic i = cbtocylno(fs, bno); 6805375Smckusic cgp->cg_b[i][cbtorpos(fs, bno)]++; 6815375Smckusic cgp->cg_btot[i]++; 6824426Smckusic } else { 6835322Smckusic bbase = bno - (bno % fs->fs_frag); 6844463Smckusic /* 6854463Smckusic * decrement the counts associated with the old frags 6864463Smckusic */ 6876294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 6885322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 6894463Smckusic /* 6904463Smckusic * deallocate the fragment 6914463Smckusic */ 6925960Smckusic frags = numfrags(fs, size); 6934463Smckusic for (i = 0; i < frags; i++) { 6944426Smckusic if (isset(cgp->cg_free, bno + i)) 6954426Smckusic panic("free: freeing free frag"); 6964426Smckusic setbit(cgp->cg_free, bno + i); 6974426Smckusic } 6986294Smckusick cgp->cg_cs.cs_nffree += i; 6996294Smckusick fs->fs_cstotal.cs_nffree += i; 7006294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 7014463Smckusic /* 7024463Smckusic * add back in counts associated with the new frags 7034463Smckusic */ 7046294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 7055322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 7064463Smckusic /* 7074463Smckusic * if a complete block has been reassembled, account for it 7084463Smckusic */ 7095322Smckusic if (isblock(fs, cgp->cg_free, bbase / fs->fs_frag)) { 7105322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 7115322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 7125322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 7134792Smckusic cgp->cg_cs.cs_nbfree++; 7144792Smckusic fs->fs_cstotal.cs_nbfree++; 7155322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 7165375Smckusic i = cbtocylno(fs, bbase); 7175375Smckusic cgp->cg_b[i][cbtorpos(fs, bbase)]++; 7185375Smckusic cgp->cg_btot[i]++; 7194426Smckusic } 7204426Smckusic } 7214359Smckusick fs->fs_fmod++; 7224359Smckusick bdwrite(bp); 7234359Smckusick } 7244359Smckusick 7255375Smckusic /* 7265375Smckusic * Free an inode. 7275375Smckusic * 7285375Smckusic * The specified inode is placed back in the free map. 7295375Smckusic */ 7305965Smckusic ifree(ip, ino, mode) 7315965Smckusic struct inode *ip; 7324359Smckusick ino_t ino; 7334359Smckusick int mode; 7344359Smckusick { 7354359Smckusick register struct fs *fs; 7364359Smckusick register struct cg *cgp; 7374359Smckusick register struct buf *bp; 7384359Smckusick int cg; 7394359Smckusick 7405965Smckusic fs = ip->i_fs; 7414359Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) 7424359Smckusick panic("ifree: range"); 7435377Smckusic cg = itog(fs, ino); 7445965Smckusic bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize); 745*6531Smckusick cgp = bp->b_un.b_cg; 746*6531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 7475960Smckusic brelse(bp); 7484359Smckusick return; 7495960Smckusic } 7504359Smckusick ino %= fs->fs_ipg; 7514359Smckusick if (isclr(cgp->cg_iused, ino)) 7524359Smckusick panic("ifree: freeing free inode"); 7534359Smckusick clrbit(cgp->cg_iused, ino); 7544792Smckusic cgp->cg_cs.cs_nifree++; 7554792Smckusic fs->fs_cstotal.cs_nifree++; 7565322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 7574359Smckusick if ((mode & IFMT) == IFDIR) { 7584792Smckusic cgp->cg_cs.cs_ndir--; 7594792Smckusic fs->fs_cstotal.cs_ndir--; 7605322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 7614359Smckusick } 7624359Smckusick fs->fs_fmod++; 7634359Smckusick bdwrite(bp); 7644359Smckusick } 7654359Smckusick 7664463Smckusic /* 7675375Smckusic * Find a block of the specified size in the specified cylinder group. 7685375Smckusic * 7694651Smckusic * It is a panic if a request is made to find a block if none are 7704651Smckusic * available. 7714651Smckusic */ 7724651Smckusic daddr_t 7734651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 7744651Smckusic register struct fs *fs; 7754651Smckusic register struct cg *cgp; 7764651Smckusic daddr_t bpref; 7774651Smckusic int allocsiz; 7784651Smckusic { 7794651Smckusic daddr_t bno; 7804651Smckusic int start, len, loc, i; 7814651Smckusic int blk, field, subfield, pos; 7824651Smckusic 7834651Smckusic /* 7844651Smckusic * find the fragment by searching through the free block 7854651Smckusic * map for an appropriate bit pattern 7864651Smckusic */ 7874651Smckusic if (bpref) 7885377Smckusic start = dtogd(fs, bpref) / NBBY; 7894651Smckusic else 7904651Smckusic start = cgp->cg_frotor / NBBY; 7915398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 7925322Smckusic loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag], 7936292Smckusick 1 << (allocsiz - 1 + (fs->fs_frag % NBBY))); 7944651Smckusic if (loc == 0) { 795*6531Smckusick len = start + 1; 796*6531Smckusick start = 0; 7975322Smckusic loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag], 7986292Smckusick 1 << (allocsiz - 1 + (fs->fs_frag % NBBY))); 7994651Smckusic if (loc == 0) { 8004651Smckusic panic("alloccg: map corrupted"); 801*6531Smckusick return (-1); 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 */ 8106294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 8116294Smckusick blk = blkmap(fs, cgp->cg_free, bno); 8124651Smckusic blk <<= 1; 8134651Smckusic field = around[allocsiz]; 8144651Smckusic subfield = inside[allocsiz]; 8155322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 8166294Smckusick if ((blk & field) == subfield) 8176294Smckusick return (bno + pos); 8184651Smckusic field <<= 1; 8194651Smckusic subfield <<= 1; 8204651Smckusic } 8214651Smckusic } 8224651Smckusic panic("alloccg: block not in map"); 823*6531Smckusick return (-1); 8244651Smckusic } 8254651Smckusic 8264651Smckusic /* 8275375Smckusic * Update the frsum fields to reflect addition or deletion 8285375Smckusic * of some frags. 8294463Smckusic */ 8305322Smckusic fragacct(fs, fragmap, fraglist, cnt) 8315322Smckusic struct fs *fs; 8324472Smckusic int fragmap; 8334792Smckusic long fraglist[]; 8344463Smckusic int cnt; 8354463Smckusic { 8364463Smckusic int inblk; 8374463Smckusic register int field, subfield; 8384463Smckusic register int siz, pos; 8394463Smckusic 8405322Smckusic inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 8414463Smckusic fragmap <<= 1; 8425322Smckusic for (siz = 1; siz < fs->fs_frag; siz++) { 8436292Smckusick if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 8444463Smckusic continue; 8454463Smckusic field = around[siz]; 8464463Smckusic subfield = inside[siz]; 8475322Smckusic for (pos = siz; pos <= fs->fs_frag; pos++) { 8484463Smckusic if ((fragmap & field) == subfield) { 8494463Smckusic fraglist[siz] += cnt; 8504463Smckusic pos += siz; 8514463Smckusic field <<= siz; 8524463Smckusic subfield <<= siz; 8534463Smckusic } 8544463Smckusic field <<= 1; 8554463Smckusic subfield <<= 1; 8564463Smckusic } 8574463Smckusic } 8584463Smckusic } 8594463Smckusic 8605375Smckusic /* 8615375Smckusic * Check that a specified block number is in range. 8625375Smckusic */ 8634359Smckusick badblock(fs, bn) 8644359Smckusick register struct fs *fs; 8654359Smckusick daddr_t bn; 8664359Smckusick { 8674359Smckusick 868*6531Smckusick if ((unsigned)bn >= fs->fs_size) { 8694359Smckusick fserr(fs, "bad block"); 8704359Smckusick return (1); 8714359Smckusick } 8724359Smckusick return (0); 8734359Smckusick } 8744359Smckusick 8754359Smckusick /* 8765375Smckusic * Getfs maps a device number into a pointer to the incore super block. 8774359Smckusick * 8785375Smckusic * The algorithm is a linear search through the mount table. A 8795375Smckusic * consistency check of the super block magic number is performed. 8805375Smckusic * 8814359Smckusick * panic: no fs -- the device is not mounted. 8824359Smckusick * this "cannot happen" 8834359Smckusick */ 8844359Smckusick struct fs * 8854359Smckusick getfs(dev) 8864359Smckusick dev_t dev; 8874359Smckusick { 8884359Smckusick register struct mount *mp; 8894359Smckusick register struct fs *fs; 8904359Smckusick 8916294Smckusick for (mp = &mount[0]; mp < &mount[NMOUNT]; mp++) { 8926294Smckusick if (mp->m_bufp == NULL || mp->m_dev != dev) 8936294Smckusick continue; 8946294Smckusick fs = mp->m_bufp->b_un.b_fs; 8956294Smckusick if (fs->fs_magic != FS_MAGIC) 8966294Smckusick panic("getfs: bad magic"); 8976294Smckusick return (fs); 8986294Smckusick } 8994359Smckusick panic("getfs: no fs"); 9004359Smckusick return (NULL); 9014359Smckusick } 9024359Smckusick 9034359Smckusick /* 9045375Smckusic * Fserr prints the name of a file system with an error diagnostic. 9055375Smckusic * 9065375Smckusic * The form of the error message is: 9074359Smckusick * fs: error message 9084359Smckusick */ 9094359Smckusick fserr(fs, cp) 9104359Smckusick struct fs *fs; 9114359Smckusick char *cp; 9124359Smckusick { 9134359Smckusick 9144359Smckusick printf("%s: %s\n", fs->fs_fsmnt, cp); 9154359Smckusick } 9164359Smckusick 9174359Smckusick /* 9184359Smckusick * Getfsx returns the index in the file system 9194359Smckusick * table of the specified device. The swap device 9204359Smckusick * is also assigned a pseudo-index. The index may 9214359Smckusick * be used as a compressed indication of the location 9224359Smckusick * of a block, recording 9234359Smckusick * <getfsx(dev),blkno> 9244359Smckusick * rather than 9254359Smckusick * <dev, blkno> 9264359Smckusick * provided the information need remain valid only 9274359Smckusick * as long as the file system is mounted. 9284359Smckusick */ 9294359Smckusick getfsx(dev) 9304359Smckusick dev_t dev; 9314359Smckusick { 9324359Smckusick register struct mount *mp; 9334359Smckusick 9344359Smckusick if (dev == swapdev) 9354359Smckusick return (MSWAPX); 9364359Smckusick for(mp = &mount[0]; mp < &mount[NMOUNT]; mp++) 9374359Smckusick if (mp->m_dev == dev) 9384359Smckusick return (mp - &mount[0]); 9394359Smckusick return (-1); 9404359Smckusick } 9414359Smckusick 9424359Smckusick /* 9434359Smckusick * Update is the internal name of 'sync'. It goes through the disk 9444359Smckusick * queues to initiate sandbagged IO; goes through the inodes to write 9455375Smckusic * modified nodes; and it goes through the mount table to initiate 9465375Smckusic * the writing of the modified super blocks. 9474359Smckusick */ 9486294Smckusick update(flag) 9496294Smckusick int flag; 9504359Smckusick { 9514359Smckusick register struct inode *ip; 9524359Smckusick register struct mount *mp; 9534359Smckusick register struct buf *bp; 9544359Smckusick struct fs *fs; 9554651Smckusic int i, blks; 9564359Smckusick 9574359Smckusick if (updlock) 9584359Smckusick return; 9594359Smckusick updlock++; 9604359Smckusick /* 9614359Smckusick * Write back modified superblocks. 9624359Smckusick * Consistency check that the superblock 9634359Smckusick * of each file system is still in the buffer cache. 9644359Smckusick */ 9656294Smckusick for (mp = &mount[0]; mp < &mount[NMOUNT]; mp++) { 9666294Smckusick if (mp->m_bufp == NULL) 9676294Smckusick continue; 9686294Smckusick fs = mp->m_bufp->b_un.b_fs; 9696294Smckusick if (fs->fs_fmod == 0) 9706294Smckusick continue; 9716294Smckusick if (fs->fs_ronly != 0) 9726294Smckusick panic("update: rofs mod"); 9736294Smckusick bp = getblk(mp->m_dev, SBLOCK, SBSIZE); 974*6531Smckusick if (bp->b_un.b_fs != fs || fs->fs_magic != FS_MAGIC) 975*6531Smckusick panic("update: bad b_fs"); 9766294Smckusick fs->fs_fmod = 0; 9776310Smckusick fs->fs_time = time; 9786294Smckusick bwrite(bp); 979*6531Smckusick blks = howmany(fs->fs_cssize, fs->fs_fsize); 980*6531Smckusick for (i = 0; i < blks; i += fs->fs_frag) { 9816294Smckusick bp = getblk(mp->m_dev, 982*6531Smckusick fsbtodb(fs, fs->fs_csaddr + i), 983*6531Smckusick blks - i < fs->fs_frag ? 984*6531Smckusick (blks - i) * fs->fs_fsize : 985*6531Smckusick fs->fs_bsize); 9864359Smckusick bwrite(bp); 9874359Smckusick } 9886294Smckusick } 9894359Smckusick /* 9904359Smckusick * Write back each (modified) inode. 9914359Smckusick */ 9926294Smckusick for (ip = inode; ip < inodeNINODE; ip++) { 9936294Smckusick if ((ip->i_flag & ILOCK) != 0 || ip->i_count == 0) 9946294Smckusick continue; 9956294Smckusick ip->i_flag |= ILOCK; 9966294Smckusick ip->i_count++; 9976310Smckusick iupdat(ip, &time, &time, 0); 9986294Smckusick iput(ip); 9996294Smckusick } 10004359Smckusick updlock = 0; 10014359Smckusick /* 10024359Smckusick * Force stale buffer cache information to be flushed, 10034359Smckusick * for all devices. 10044359Smckusick */ 10054359Smckusick bflush(NODEV); 10064359Smckusick } 10075322Smckusic 10085322Smckusic /* 10095375Smckusic * block operations 10105375Smckusic * 10115375Smckusic * check if a block is available 10125322Smckusic */ 10135322Smckusic isblock(fs, cp, h) 10145322Smckusic struct fs *fs; 10155322Smckusic unsigned char *cp; 10165322Smckusic int h; 10175322Smckusic { 10185322Smckusic unsigned char mask; 10195322Smckusic 10205322Smckusic switch (fs->fs_frag) { 10215322Smckusic case 8: 10225322Smckusic return (cp[h] == 0xff); 10235322Smckusic case 4: 10245322Smckusic mask = 0x0f << ((h & 0x1) << 2); 10255322Smckusic return ((cp[h >> 1] & mask) == mask); 10265322Smckusic case 2: 10275322Smckusic mask = 0x03 << ((h & 0x3) << 1); 10285322Smckusic return ((cp[h >> 2] & mask) == mask); 10295322Smckusic case 1: 10305322Smckusic mask = 0x01 << (h & 0x7); 10315322Smckusic return ((cp[h >> 3] & mask) == mask); 10325322Smckusic default: 10336294Smckusick panic("isblock"); 10346294Smckusick return (NULL); 10355322Smckusic } 10365322Smckusic } 10375375Smckusic 10385375Smckusic /* 10395375Smckusic * take a block out of the map 10405375Smckusic */ 10415322Smckusic clrblock(fs, cp, h) 10425322Smckusic struct fs *fs; 10435322Smckusic unsigned char *cp; 10445322Smckusic int h; 10455322Smckusic { 10465322Smckusic switch ((fs)->fs_frag) { 10475322Smckusic case 8: 10485322Smckusic cp[h] = 0; 10495322Smckusic return; 10505322Smckusic case 4: 10515322Smckusic cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 10525322Smckusic return; 10535322Smckusic case 2: 10545322Smckusic cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 10555322Smckusic return; 10565322Smckusic case 1: 10575322Smckusic cp[h >> 3] &= ~(0x01 << (h & 0x7)); 10585322Smckusic return; 10595322Smckusic default: 10606294Smckusick panic("clrblock"); 10615322Smckusic return; 10625322Smckusic } 10635322Smckusic } 10645375Smckusic 10655375Smckusic /* 10665375Smckusic * put a block into the map 10675375Smckusic */ 10685322Smckusic setblock(fs, cp, h) 10695322Smckusic struct fs *fs; 10705322Smckusic unsigned char *cp; 10715322Smckusic int h; 10725322Smckusic { 10735322Smckusic switch (fs->fs_frag) { 10745322Smckusic case 8: 10755322Smckusic cp[h] = 0xff; 10765322Smckusic return; 10775322Smckusic case 4: 10785322Smckusic cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 10795322Smckusic return; 10805322Smckusic case 2: 10815322Smckusic cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 10825322Smckusic return; 10835322Smckusic case 1: 10845322Smckusic cp[h >> 3] |= (0x01 << (h & 0x7)); 10855322Smckusic return; 10865322Smckusic default: 10876294Smckusick panic("setblock"); 10885322Smckusic return; 10895322Smckusic } 10905322Smckusic } 1091