1*17658Smckusick /* lfs_alloc.c 6.8 85/01/07 */ 24359Smckusick 317097Sbloom #include "param.h" 417097Sbloom #include "systm.h" 517097Sbloom #include "mount.h" 617097Sbloom #include "fs.h" 717097Sbloom #include "conf.h" 817097Sbloom #include "buf.h" 917097Sbloom #include "inode.h" 1017097Sbloom #include "dir.h" 1117097Sbloom #include "user.h" 1217097Sbloom #include "quota.h" 1317097Sbloom #include "kernel.h" 144359Smckusick 155212Smckusic extern u_long hashalloc(); 169163Ssam extern ino_t ialloccg(); 179163Ssam extern daddr_t alloccg(); 184651Smckusic extern daddr_t alloccgblk(); 194651Smckusic extern daddr_t fragextend(); 204651Smckusic extern daddr_t blkpref(); 214651Smckusic extern daddr_t mapsearch(); 224607Smckusic extern int inside[], around[]; 235322Smckusic extern unsigned char *fragtbl[]; 244359Smckusick 255375Smckusic /* 265375Smckusic * Allocate a block in the file system. 275375Smckusic * 285375Smckusic * The size of the requested block is given, which must be some 295375Smckusic * multiple of fs_fsize and <= fs_bsize. 305375Smckusic * A preference may be optionally specified. If a preference is given 315375Smckusic * the following hierarchy is used to allocate a block: 325375Smckusic * 1) allocate the requested block. 335375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 345375Smckusic * 3) allocate a block in the same cylinder group. 355375Smckusic * 4) quadradically rehash into other cylinder groups, until an 365375Smckusic * available block is located. 375375Smckusic * If no block preference is given the following heirarchy is used 385375Smckusic * to allocate a block: 395375Smckusic * 1) allocate a block in the cylinder group that contains the 405375Smckusic * inode for the file. 415375Smckusic * 2) quadradically rehash into other cylinder groups, until an 425375Smckusic * available block is located. 435375Smckusic */ 444359Smckusick struct buf * 455965Smckusic alloc(ip, bpref, size) 464463Smckusic register struct inode *ip; 474359Smckusick daddr_t bpref; 484359Smckusick int size; 494359Smckusick { 504359Smckusick daddr_t bno; 514359Smckusick register struct fs *fs; 524463Smckusic register struct buf *bp; 534359Smckusick int cg; 544359Smckusick 555965Smckusic fs = ip->i_fs; 566716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 576716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 586716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 594463Smckusic panic("alloc: bad size"); 606716Smckusick } 615322Smckusic if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 624359Smckusick goto nospace; 6311638Ssam if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 644792Smckusic goto nospace; 657650Ssam #ifdef QUOTA 6612643Ssam u.u_error = chkdq(ip, (long)btodb(size), 0); 6712643Ssam if (u.u_error) 6812643Ssam return (NULL); 697483Skre #endif 704948Smckusic if (bpref >= fs->fs_size) 714948Smckusic bpref = 0; 724359Smckusick if (bpref == 0) 735377Smckusic cg = itog(fs, ip->i_number); 744359Smckusick else 755377Smckusic cg = dtog(fs, bpref); 769163Ssam bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, 779163Ssam (u_long (*)())alloccg); 786567Smckusic if (bno <= 0) 794359Smckusick goto nospace; 8012643Ssam ip->i_blocks += btodb(size); 8112643Ssam ip->i_flag |= IUPD|ICHG; 825965Smckusic bp = getblk(ip->i_dev, fsbtodb(fs, bno), size); 834359Smckusick clrbuf(bp); 844359Smckusick return (bp); 854359Smckusick nospace: 864359Smckusick fserr(fs, "file system full"); 874359Smckusick uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 884359Smckusick u.u_error = ENOSPC; 894359Smckusick return (NULL); 904359Smckusick } 914359Smckusick 925375Smckusic /* 935375Smckusic * Reallocate a fragment to a bigger size 945375Smckusic * 955375Smckusic * The number and size of the old block is given, and a preference 965375Smckusic * and new size is also specified. The allocator attempts to extend 975375Smckusic * the original block. Failing that, the regular block allocator is 985375Smckusic * invoked to get an appropriate block. 995375Smckusic */ 1004426Smckusic struct buf * 1015965Smckusic realloccg(ip, bprev, bpref, osize, nsize) 1025965Smckusic register struct inode *ip; 1034651Smckusic daddr_t bprev, bpref; 1044426Smckusic int osize, nsize; 1054426Smckusic { 1064426Smckusic daddr_t bno; 1074426Smckusic register struct fs *fs; 1084463Smckusic register struct buf *bp, *obp; 1094426Smckusic int cg; 1104426Smckusic 1115965Smckusic fs = ip->i_fs; 1125960Smckusic if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 1136716Smckusick (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 1146716Smckusick printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 1156716Smckusick ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 1164463Smckusic panic("realloccg: bad size"); 1176716Smckusick } 11811638Ssam if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 1194792Smckusic goto nospace; 1206716Smckusick if (bprev == 0) { 1216716Smckusick printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 1226716Smckusick ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 1234463Smckusic panic("realloccg: bad bprev"); 1246716Smckusick } 1257650Ssam #ifdef QUOTA 12612643Ssam u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0); 12712643Ssam if (u.u_error) 12812643Ssam return (NULL); 1297483Skre #endif 1306294Smckusick cg = dtog(fs, bprev); 1315965Smckusic bno = fragextend(ip, cg, (long)bprev, osize, nsize); 1324463Smckusic if (bno != 0) { 1337187Sroot do { 1347187Sroot bp = bread(ip->i_dev, fsbtodb(fs, bno), osize); 1357187Sroot if (bp->b_flags & B_ERROR) { 1367187Sroot brelse(bp); 1377187Sroot return (NULL); 1387187Sroot } 1397187Sroot } while (brealloc(bp, nsize) == 0); 1407187Sroot bp->b_flags |= B_DONE; 1419163Ssam bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 14212643Ssam ip->i_blocks += btodb(nsize - osize); 14312643Ssam ip->i_flag |= IUPD|ICHG; 1444463Smckusic return (bp); 1454463Smckusic } 1464948Smckusic if (bpref >= fs->fs_size) 1474948Smckusic bpref = 0; 1489163Ssam bno = (daddr_t)hashalloc(ip, cg, (long)bpref, nsize, 1499163Ssam (u_long (*)())alloccg); 1506567Smckusic if (bno > 0) { 1515965Smckusic obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize); 1525960Smckusic if (obp->b_flags & B_ERROR) { 1535960Smckusic brelse(obp); 1546294Smckusick return (NULL); 1555960Smckusic } 156*17658Smckusick bp = getblk(ip->i_dev, fsbtodb(fs, bno), nsize); 157*17658Smckusick bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize); 158*17658Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 159*17658Smckusick if (obp->b_flags & B_DELWRI) { 160*17658Smckusick obp->b_flags &= ~B_DELWRI; 161*17658Smckusick u.u_ru.ru_oublock--; /* delete charge */ 162*17658Smckusick } 1634463Smckusic brelse(obp); 1649163Ssam free(ip, bprev, (off_t)osize); 16512643Ssam ip->i_blocks += btodb(nsize - osize); 16612643Ssam ip->i_flag |= IUPD|ICHG; 1676294Smckusick return (bp); 1684463Smckusic } 1694792Smckusic nospace: 1704463Smckusic /* 1714463Smckusic * no space available 1724463Smckusic */ 1734426Smckusic fserr(fs, "file system full"); 1744426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 1754426Smckusic u.u_error = ENOSPC; 1764426Smckusic return (NULL); 1774426Smckusic } 1784426Smckusic 1795375Smckusic /* 1805375Smckusic * Allocate an inode in the file system. 1815375Smckusic * 1825375Smckusic * A preference may be optionally specified. If a preference is given 1835375Smckusic * the following hierarchy is used to allocate an inode: 1845375Smckusic * 1) allocate the requested inode. 1855375Smckusic * 2) allocate an inode in the same cylinder group. 1865375Smckusic * 3) quadradically rehash into other cylinder groups, until an 1875375Smckusic * available inode is located. 1885375Smckusic * If no inode preference is given the following heirarchy is used 1895375Smckusic * to allocate an inode: 1905375Smckusic * 1) allocate an inode in cylinder group 0. 1915375Smckusic * 2) quadradically rehash into other cylinder groups, until an 1925375Smckusic * available inode is located. 1935375Smckusic */ 1944359Smckusick struct inode * 1955965Smckusic ialloc(pip, ipref, mode) 1965965Smckusic register struct inode *pip; 1974359Smckusick ino_t ipref; 1984359Smckusick int mode; 1994359Smckusick { 2005212Smckusic ino_t ino; 2014359Smckusick register struct fs *fs; 2024359Smckusick register struct inode *ip; 2034359Smckusick int cg; 2044359Smckusick 2055965Smckusic fs = pip->i_fs; 2064792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 2074359Smckusick goto noinodes; 2087650Ssam #ifdef QUOTA 20912643Ssam u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0); 21012643Ssam if (u.u_error) 21112643Ssam return (NULL); 2127483Skre #endif 2134948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 2144948Smckusic ipref = 0; 2155377Smckusic cg = itog(fs, ipref); 2165965Smckusic ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); 2174359Smckusick if (ino == 0) 2184359Smckusick goto noinodes; 2195965Smckusic ip = iget(pip->i_dev, pip->i_fs, ino); 2204359Smckusick if (ip == NULL) { 22115120Skarels ifree(pip, ino, 0); 2224359Smckusick return (NULL); 2234359Smckusick } 2246716Smckusick if (ip->i_mode) { 2256716Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 2266716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 2274359Smckusick panic("ialloc: dup alloc"); 2286716Smckusick } 22912643Ssam if (ip->i_blocks) { /* XXX */ 23012643Ssam printf("free inode %s/%d had %d blocks\n", 23112643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 23212643Ssam ip->i_blocks = 0; 23312643Ssam } 2344359Smckusick return (ip); 2354359Smckusick noinodes: 2364359Smckusick fserr(fs, "out of inodes"); 2376294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 2384359Smckusick u.u_error = ENOSPC; 2394359Smckusick return (NULL); 2404359Smckusick } 2414359Smckusick 2424651Smckusic /* 2435375Smckusic * Find a cylinder to place a directory. 2445375Smckusic * 2455375Smckusic * The policy implemented by this algorithm is to select from 2465375Smckusic * among those cylinder groups with above the average number of 2475375Smckusic * free inodes, the one with the smallest number of directories. 2484651Smckusic */ 2499163Ssam ino_t 2505965Smckusic dirpref(fs) 2515965Smckusic register struct fs *fs; 2524359Smckusick { 2534651Smckusic int cg, minndir, mincg, avgifree; 2544359Smckusick 2554792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 2564651Smckusic minndir = fs->fs_ipg; 2574359Smckusick mincg = 0; 2584651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 2595322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 2605322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 2614359Smckusick mincg = cg; 2625322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 2634359Smckusick } 2649163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 2654359Smckusick } 2664359Smckusick 2674651Smckusic /* 2689163Ssam * Select the desired position for the next block in a file. The file is 2699163Ssam * logically divided into sections. The first section is composed of the 2709163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 2719163Ssam * 2729163Ssam * If no blocks have been allocated in the first section, the policy is to 2739163Ssam * request a block in the same cylinder group as the inode that describes 2749163Ssam * the file. If no blocks have been allocated in any other section, the 2759163Ssam * policy is to place the section in a cylinder group with a greater than 2769163Ssam * average number of free blocks. An appropriate cylinder group is found 2779163Ssam * by maintaining a rotor that sweeps the cylinder groups. When a new 2789163Ssam * group of blocks is needed, the rotor is advanced until a cylinder group 2799163Ssam * with greater than the average number of free blocks is found. 2809163Ssam * 2819163Ssam * If a section is already partially allocated, the policy is to 2829163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 2839163Ssam * contiguous blocks and the beginning of the next is physically separated 2849163Ssam * so that the disk head will be in transit between them for at least 2859163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 2869163Ssam * schedule another I/O transfer. 2874651Smckusic */ 2885212Smckusic daddr_t 2899163Ssam blkpref(ip, lbn, indx, bap) 2909163Ssam struct inode *ip; 2919163Ssam daddr_t lbn; 2929163Ssam int indx; 2939163Ssam daddr_t *bap; 2949163Ssam { 2955965Smckusic register struct fs *fs; 2964651Smckusic int cg, avgbfree; 2979163Ssam daddr_t nextblk; 2984651Smckusic 2999163Ssam fs = ip->i_fs; 3009163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 3019163Ssam if (lbn < NDADDR) { 3029163Ssam cg = itog(fs, ip->i_number); 3035322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 3044651Smckusic } 3059163Ssam /* 3069163Ssam * Find a cylinder with greater than average number of 3079163Ssam * unused data blocks. 3089163Ssam */ 3099163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 3109163Ssam for (cg = fs->fs_cgrotor + 1; cg < fs->fs_ncg; cg++) 3119163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3129163Ssam fs->fs_cgrotor = cg; 3139163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3149163Ssam } 3159163Ssam for (cg = 0; cg <= fs->fs_cgrotor; cg++) 3169163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3179163Ssam fs->fs_cgrotor = cg; 3189163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3199163Ssam } 3209163Ssam return (NULL); 3219163Ssam } 3229163Ssam /* 3239163Ssam * One or more previous blocks have been laid out. If less 3249163Ssam * than fs_maxcontig previous blocks are contiguous, the 3259163Ssam * next block is requested contiguously, otherwise it is 3269163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 3279163Ssam */ 3289163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 3299163Ssam if (indx > fs->fs_maxcontig && 33011638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 3319163Ssam != nextblk) 3329163Ssam return (nextblk); 3339163Ssam if (fs->fs_rotdelay != 0) 3349163Ssam /* 3359163Ssam * Here we convert ms of delay to frags as: 3369163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 3379163Ssam * ((sect/frag) * (ms/sec)) 3389163Ssam * then round up to the next block. 3399163Ssam */ 3409163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 3419163Ssam (NSPF(fs) * 1000), fs->fs_frag); 3429163Ssam return (nextblk); 3434651Smckusic } 3444651Smckusic 3455375Smckusic /* 3465375Smckusic * Implement the cylinder overflow algorithm. 3475375Smckusic * 3485375Smckusic * The policy implemented by this algorithm is: 3495375Smckusic * 1) allocate the block in its requested cylinder group. 3505375Smckusic * 2) quadradically rehash on the cylinder group number. 3515375Smckusic * 3) brute force search for a free block. 3525375Smckusic */ 3535212Smckusic /*VARARGS5*/ 3545212Smckusic u_long 3555965Smckusic hashalloc(ip, cg, pref, size, allocator) 3565965Smckusic struct inode *ip; 3574359Smckusick int cg; 3584359Smckusick long pref; 3594359Smckusick int size; /* size for data blocks, mode for inodes */ 3605212Smckusic u_long (*allocator)(); 3614359Smckusick { 3625965Smckusic register struct fs *fs; 3634359Smckusick long result; 3644359Smckusick int i, icg = cg; 3654359Smckusick 3665965Smckusic fs = ip->i_fs; 3674359Smckusick /* 3684359Smckusick * 1: preferred cylinder group 3694359Smckusick */ 3705965Smckusic result = (*allocator)(ip, cg, pref, size); 3714359Smckusick if (result) 3724359Smckusick return (result); 3734359Smckusick /* 3744359Smckusick * 2: quadratic rehash 3754359Smckusick */ 3764359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 3774359Smckusick cg += i; 3784359Smckusick if (cg >= fs->fs_ncg) 3794359Smckusick cg -= fs->fs_ncg; 3805965Smckusic result = (*allocator)(ip, cg, 0, size); 3814359Smckusick if (result) 3824359Smckusick return (result); 3834359Smckusick } 3844359Smckusick /* 3854359Smckusick * 3: brute force search 38610847Ssam * Note that we start at i == 2, since 0 was checked initially, 38710847Ssam * and 1 is always checked in the quadratic rehash. 3884359Smckusick */ 38910848Smckusick cg = (icg + 2) % fs->fs_ncg; 39010847Ssam for (i = 2; i < fs->fs_ncg; i++) { 3915965Smckusic result = (*allocator)(ip, cg, 0, size); 3924359Smckusick if (result) 3934359Smckusick return (result); 3944359Smckusick cg++; 3954359Smckusick if (cg == fs->fs_ncg) 3964359Smckusick cg = 0; 3974359Smckusick } 3986294Smckusick return (NULL); 3994359Smckusick } 4004359Smckusick 4015375Smckusic /* 4025375Smckusic * Determine whether a fragment can be extended. 4035375Smckusic * 4045375Smckusic * Check to see if the necessary fragments are available, and 4055375Smckusic * if they are, allocate them. 4065375Smckusic */ 4074359Smckusick daddr_t 4085965Smckusic fragextend(ip, cg, bprev, osize, nsize) 4095965Smckusic struct inode *ip; 4104426Smckusic int cg; 4114463Smckusic long bprev; 4124426Smckusic int osize, nsize; 4134426Smckusic { 4145965Smckusic register struct fs *fs; 4154463Smckusic register struct buf *bp; 4164463Smckusic register struct cg *cgp; 4174463Smckusic long bno; 4184463Smckusic int frags, bbase; 4194426Smckusic int i; 4204426Smckusic 4215965Smckusic fs = ip->i_fs; 42217224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 4236531Smckusick return (NULL); 4245960Smckusic frags = numfrags(fs, nsize); 42517224Smckusick bbase = fragnum(fs, bprev); 42617224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 4274463Smckusic /* cannot extend across a block boundry */ 4286294Smckusick return (NULL); 4294463Smckusic } 43010278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 4316531Smckusick cgp = bp->b_un.b_cg; 4326531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 4335960Smckusic brelse(bp); 4346294Smckusick return (NULL); 4355960Smckusic } 4368105Sroot cgp->cg_time = time.tv_sec; 4375377Smckusic bno = dtogd(fs, bprev); 4385960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 4395361Smckusic if (isclr(cgp->cg_free, bno + i)) { 4405361Smckusic brelse(bp); 4416294Smckusick return (NULL); 4425361Smckusic } 4435361Smckusic /* 4445361Smckusic * the current fragment can be extended 4455361Smckusic * deduct the count on fragment being extended into 4465361Smckusic * increase the count on the remaining fragment (if any) 4475361Smckusic * allocate the extended piece 4485361Smckusic */ 4495361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 4504463Smckusic if (isclr(cgp->cg_free, bno + i)) 4514463Smckusic break; 4525960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 4535361Smckusic if (i != frags) 4545361Smckusic cgp->cg_frsum[i - frags]++; 4555960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 4565361Smckusic clrbit(cgp->cg_free, bno + i); 4575361Smckusic cgp->cg_cs.cs_nffree--; 4585361Smckusic fs->fs_cstotal.cs_nffree--; 4595361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 4604463Smckusic } 4615361Smckusic fs->fs_fmod++; 4625361Smckusic bdwrite(bp); 4635361Smckusic return (bprev); 4644426Smckusic } 4654426Smckusic 4665375Smckusic /* 4675375Smckusic * Determine whether a block can be allocated. 4685375Smckusic * 4695375Smckusic * Check to see if a block of the apprpriate size is available, 4705375Smckusic * and if it is, allocate it. 4715375Smckusic */ 4729163Ssam daddr_t 4735965Smckusic alloccg(ip, cg, bpref, size) 4745965Smckusic struct inode *ip; 4754359Smckusick int cg; 4764359Smckusick daddr_t bpref; 4774359Smckusick int size; 4784359Smckusick { 4795965Smckusic register struct fs *fs; 4804463Smckusic register struct buf *bp; 4814463Smckusic register struct cg *cgp; 4824463Smckusic int bno, frags; 4834463Smckusic int allocsiz; 4844463Smckusic register int i; 4854359Smckusick 4865965Smckusic fs = ip->i_fs; 4875322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 4886294Smckusick return (NULL); 48910278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 4906531Smckusick cgp = bp->b_un.b_cg; 49115950Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC || 49215950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 4935960Smckusic brelse(bp); 4946294Smckusick return (NULL); 4955960Smckusic } 4968105Sroot cgp->cg_time = time.tv_sec; 4975322Smckusic if (size == fs->fs_bsize) { 4985212Smckusic bno = alloccgblk(fs, cgp, bpref); 4994463Smckusic bdwrite(bp); 5004463Smckusic return (bno); 5014463Smckusic } 5024463Smckusic /* 5034463Smckusic * check to see if any fragments are already available 5044463Smckusic * allocsiz is the size which will be allocated, hacking 5054463Smckusic * it down to a smaller size if necessary 5064463Smckusic */ 5075960Smckusic frags = numfrags(fs, size); 5085322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 5094463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 5104463Smckusic break; 5115322Smckusic if (allocsiz == fs->fs_frag) { 5124463Smckusic /* 5134463Smckusic * no fragments were available, so a block will be 5144463Smckusic * allocated, and hacked up 5154463Smckusic */ 5164792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 5174463Smckusic brelse(bp); 5186294Smckusick return (NULL); 5194463Smckusic } 5205212Smckusic bno = alloccgblk(fs, cgp, bpref); 5215377Smckusic bpref = dtogd(fs, bno); 5225322Smckusic for (i = frags; i < fs->fs_frag; i++) 5234463Smckusic setbit(cgp->cg_free, bpref + i); 5245322Smckusic i = fs->fs_frag - frags; 5254792Smckusic cgp->cg_cs.cs_nffree += i; 5264792Smckusic fs->fs_cstotal.cs_nffree += i; 5275322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 5289762Ssam fs->fs_fmod++; 5294463Smckusic cgp->cg_frsum[i]++; 5304463Smckusic bdwrite(bp); 5314463Smckusic return (bno); 5324463Smckusic } 5334651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 53415950Smckusick if (bno < 0) { 53515950Smckusick brelse(bp); 5366294Smckusick return (NULL); 53715950Smckusick } 5384463Smckusic for (i = 0; i < frags; i++) 5394463Smckusic clrbit(cgp->cg_free, bno + i); 5404792Smckusic cgp->cg_cs.cs_nffree -= frags; 5414792Smckusic fs->fs_cstotal.cs_nffree -= frags; 5425322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 5439762Ssam fs->fs_fmod++; 5444463Smckusic cgp->cg_frsum[allocsiz]--; 5454463Smckusic if (frags != allocsiz) 5464463Smckusic cgp->cg_frsum[allocsiz - frags]++; 5474463Smckusic bdwrite(bp); 5484463Smckusic return (cg * fs->fs_fpg + bno); 5494463Smckusic } 5504463Smckusic 5515375Smckusic /* 5525375Smckusic * Allocate a block in a cylinder group. 5535375Smckusic * 5545375Smckusic * This algorithm implements the following policy: 5555375Smckusic * 1) allocate the requested block. 5565375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 5575375Smckusic * 3) allocate the next available block on the block rotor for the 5585375Smckusic * specified cylinder group. 5595375Smckusic * Note that this routine only allocates fs_bsize blocks; these 5605375Smckusic * blocks may be fragmented by the routine that allocates them. 5615375Smckusic */ 5624463Smckusic daddr_t 5635212Smckusic alloccgblk(fs, cgp, bpref) 5645965Smckusic register struct fs *fs; 5654463Smckusic register struct cg *cgp; 5664463Smckusic daddr_t bpref; 5674463Smckusic { 5684651Smckusic daddr_t bno; 5696294Smckusick int cylno, pos, delta; 5704651Smckusic short *cylbp; 5715361Smckusic register int i; 5724463Smckusic 5734651Smckusic if (bpref == 0) { 5744651Smckusic bpref = cgp->cg_rotor; 5755361Smckusic goto norot; 5765361Smckusic } 57717224Smckusick bpref = blknum(fs, bpref); 5785377Smckusic bpref = dtogd(fs, bpref); 5795361Smckusic /* 5805361Smckusic * if the requested block is available, use it 5815361Smckusic */ 58211638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) { 5835361Smckusic bno = bpref; 5845361Smckusic goto gotit; 5855361Smckusic } 5865361Smckusic /* 5875361Smckusic * check for a block available on the same cylinder 5885361Smckusic */ 5895361Smckusic cylno = cbtocylno(fs, bpref); 5905375Smckusic if (cgp->cg_btot[cylno] == 0) 5915375Smckusic goto norot; 5925375Smckusic if (fs->fs_cpc == 0) { 5935375Smckusic /* 5945375Smckusic * block layout info is not available, so just have 5955375Smckusic * to take any block in this cylinder. 5965375Smckusic */ 5975375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 5985375Smckusic goto norot; 5995375Smckusic } 6005375Smckusic /* 6015361Smckusic * check the summary information to see if a block is 6025361Smckusic * available in the requested cylinder starting at the 6039163Ssam * requested rotational position and proceeding around. 6045361Smckusic */ 6059163Ssam cylbp = cgp->cg_b[cylno]; 6069163Ssam pos = cbtorpos(fs, bpref); 6075361Smckusic for (i = pos; i < NRPOS; i++) 6085361Smckusic if (cylbp[i] > 0) 6095361Smckusic break; 6105361Smckusic if (i == NRPOS) 6115361Smckusic for (i = 0; i < pos; i++) 6125361Smckusic if (cylbp[i] > 0) 6135361Smckusic break; 6145361Smckusic if (cylbp[i] > 0) { 6154651Smckusic /* 6165361Smckusic * found a rotational position, now find the actual 6175361Smckusic * block. A panic if none is actually there. 6184651Smckusic */ 6195361Smckusic pos = cylno % fs->fs_cpc; 6205361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 6216716Smckusick if (fs->fs_postbl[pos][i] == -1) { 6226716Smckusick printf("pos = %d, i = %d, fs = %s\n", 6236716Smckusick pos, i, fs->fs_fsmnt); 6245361Smckusic panic("alloccgblk: cyl groups corrupted"); 6256716Smckusick } 6266294Smckusick for (i = fs->fs_postbl[pos][i];; ) { 6275361Smckusic if (isblock(fs, cgp->cg_free, bno + i)) { 62811638Ssam bno = blkstofrags(fs, (bno + i)); 6295361Smckusic goto gotit; 6305361Smckusic } 6316294Smckusick delta = fs->fs_rotbl[i]; 6326294Smckusick if (delta <= 0 || delta > MAXBPC - i) 6334651Smckusic break; 6346294Smckusick i += delta; 6354651Smckusic } 6366716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 6375361Smckusic panic("alloccgblk: can't find blk in cyl"); 6384359Smckusick } 6395361Smckusic norot: 6405361Smckusic /* 6415361Smckusic * no blocks in the requested cylinder, so take next 6425361Smckusic * available one in this cylinder group. 6435361Smckusic */ 6448628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 6456567Smckusic if (bno < 0) 6466294Smckusick return (NULL); 6474651Smckusic cgp->cg_rotor = bno; 6484359Smckusick gotit: 64911638Ssam clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno)); 6504792Smckusic cgp->cg_cs.cs_nbfree--; 6514792Smckusic fs->fs_cstotal.cs_nbfree--; 6525322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 6535375Smckusic cylno = cbtocylno(fs, bno); 6545375Smckusic cgp->cg_b[cylno][cbtorpos(fs, bno)]--; 6555375Smckusic cgp->cg_btot[cylno]--; 6564359Smckusick fs->fs_fmod++; 6574651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 6584359Smckusick } 6594359Smckusick 6605375Smckusic /* 6615375Smckusic * Determine whether an inode can be allocated. 6625375Smckusic * 6635375Smckusic * Check to see if an inode is available, and if it is, 6645375Smckusic * allocate it using the following policy: 6655375Smckusic * 1) allocate the requested inode. 6665375Smckusic * 2) allocate the next available inode after the requested 6675375Smckusic * inode in the specified cylinder group. 6685375Smckusic */ 6699163Ssam ino_t 6705965Smckusic ialloccg(ip, cg, ipref, mode) 6715965Smckusic struct inode *ip; 6724359Smckusick int cg; 6734359Smckusick daddr_t ipref; 6744359Smckusick int mode; 6754359Smckusick { 6765965Smckusic register struct fs *fs; 6774463Smckusic register struct cg *cgp; 67816784Smckusick struct buf *bp; 67916784Smckusick int start, len, loc, map, i; 6804359Smckusick 6815965Smckusic fs = ip->i_fs; 6825322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 6836294Smckusick return (NULL); 68410278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 6856531Smckusick cgp = bp->b_un.b_cg; 68615950Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC || 68715950Smckusick cgp->cg_cs.cs_nifree == 0) { 6885960Smckusic brelse(bp); 6896294Smckusick return (NULL); 6905960Smckusic } 6918105Sroot cgp->cg_time = time.tv_sec; 6924359Smckusick if (ipref) { 6934359Smckusick ipref %= fs->fs_ipg; 6944359Smckusick if (isclr(cgp->cg_iused, ipref)) 6954359Smckusick goto gotit; 69616784Smckusick } 69716784Smckusick start = cgp->cg_irotor / NBBY; 69816784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 69916784Smckusick loc = skpc(0xff, len, &cgp->cg_iused[start]); 70016784Smckusick if (loc == 0) { 70116784Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 70216784Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 70316784Smckusick panic("ialloccg: map corrupted"); 70416784Smckusick /* NOTREACHED */ 70516784Smckusick } 70616784Smckusick i = start + len - loc; 70716784Smckusick map = cgp->cg_iused[i]; 70816784Smckusick ipref = i * NBBY; 70916784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 71016784Smckusick if ((map & i) == 0) { 7114359Smckusick cgp->cg_irotor = ipref; 7124359Smckusick goto gotit; 7134359Smckusick } 7144359Smckusick } 71516784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 71616784Smckusick panic("ialloccg: block not in map"); 71716784Smckusick /* NOTREACHED */ 7184359Smckusick gotit: 7194359Smckusick setbit(cgp->cg_iused, ipref); 7204792Smckusic cgp->cg_cs.cs_nifree--; 7214792Smckusic fs->fs_cstotal.cs_nifree--; 7225322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 7234359Smckusick fs->fs_fmod++; 7244359Smckusick if ((mode & IFMT) == IFDIR) { 7254792Smckusic cgp->cg_cs.cs_ndir++; 7264792Smckusic fs->fs_cstotal.cs_ndir++; 7275322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 7284359Smckusick } 7294359Smckusick bdwrite(bp); 7304359Smckusick return (cg * fs->fs_ipg + ipref); 7314359Smckusick } 7324359Smckusick 7335375Smckusic /* 7345375Smckusic * Free a block or fragment. 7355375Smckusic * 7365375Smckusic * The specified block or fragment is placed back in the 7375375Smckusic * free map. If a fragment is deallocated, a possible 7385375Smckusic * block reassembly is checked. 7395375Smckusic */ 7409163Ssam free(ip, bno, size) 7415965Smckusic register struct inode *ip; 7424359Smckusick daddr_t bno; 7435212Smckusic off_t size; 7444359Smckusick { 7454359Smckusick register struct fs *fs; 7464359Smckusick register struct cg *cgp; 7474359Smckusick register struct buf *bp; 7484463Smckusic int cg, blk, frags, bbase; 7494463Smckusic register int i; 7504359Smckusick 7515965Smckusic fs = ip->i_fs; 7526716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 7536716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 7546716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 7554426Smckusic panic("free: bad size"); 7566716Smckusick } 7575377Smckusic cg = dtog(fs, bno); 7586567Smckusic if (badblock(fs, bno)) { 7596567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 7604359Smckusick return; 7616567Smckusic } 76210278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 7636531Smckusick cgp = bp->b_un.b_cg; 7646531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 7655960Smckusic brelse(bp); 7664359Smckusick return; 7675960Smckusic } 7688105Sroot cgp->cg_time = time.tv_sec; 7695377Smckusic bno = dtogd(fs, bno); 7705322Smckusic if (size == fs->fs_bsize) { 77111638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) { 7726716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 7736716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 7744426Smckusic panic("free: freeing free block"); 7756567Smckusic } 77611638Ssam setblock(fs, cgp->cg_free, fragstoblks(fs, bno)); 7774792Smckusic cgp->cg_cs.cs_nbfree++; 7784792Smckusic fs->fs_cstotal.cs_nbfree++; 7795322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 7805375Smckusic i = cbtocylno(fs, bno); 7815375Smckusic cgp->cg_b[i][cbtorpos(fs, bno)]++; 7825375Smckusic cgp->cg_btot[i]++; 7834426Smckusic } else { 78417224Smckusick bbase = bno - fragnum(fs, bno); 7854463Smckusic /* 7864463Smckusic * decrement the counts associated with the old frags 7874463Smckusic */ 7886294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 7895322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 7904463Smckusic /* 7914463Smckusic * deallocate the fragment 7924463Smckusic */ 7935960Smckusic frags = numfrags(fs, size); 7944463Smckusic for (i = 0; i < frags; i++) { 7956716Smckusick if (isset(cgp->cg_free, bno + i)) { 7966716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 7976716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 7984426Smckusic panic("free: freeing free frag"); 7996716Smckusick } 8004426Smckusic setbit(cgp->cg_free, bno + i); 8014426Smckusic } 8026294Smckusick cgp->cg_cs.cs_nffree += i; 8036294Smckusick fs->fs_cstotal.cs_nffree += i; 8046294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 8054463Smckusic /* 8064463Smckusic * add back in counts associated with the new frags 8074463Smckusic */ 8086294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 8095322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 8104463Smckusic /* 8114463Smckusic * if a complete block has been reassembled, account for it 8124463Smckusic */ 81311638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) { 8145322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 8155322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 8165322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 8174792Smckusic cgp->cg_cs.cs_nbfree++; 8184792Smckusic fs->fs_cstotal.cs_nbfree++; 8195322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 8205375Smckusic i = cbtocylno(fs, bbase); 8215375Smckusic cgp->cg_b[i][cbtorpos(fs, bbase)]++; 8225375Smckusic cgp->cg_btot[i]++; 8234426Smckusic } 8244426Smckusic } 8254359Smckusick fs->fs_fmod++; 8264359Smckusick bdwrite(bp); 8274359Smckusick } 8284359Smckusick 8295375Smckusic /* 8305375Smckusic * Free an inode. 8315375Smckusic * 8325375Smckusic * The specified inode is placed back in the free map. 8335375Smckusic */ 8345965Smckusic ifree(ip, ino, mode) 8355965Smckusic struct inode *ip; 8364359Smckusick ino_t ino; 8374359Smckusick int mode; 8384359Smckusick { 8394359Smckusick register struct fs *fs; 8404359Smckusick register struct cg *cgp; 8414359Smckusick register struct buf *bp; 8424359Smckusick int cg; 8434359Smckusick 8445965Smckusic fs = ip->i_fs; 8456716Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 8466716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 8476716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 8484359Smckusick panic("ifree: range"); 8496716Smckusick } 8505377Smckusic cg = itog(fs, ino); 85110278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 8526531Smckusick cgp = bp->b_un.b_cg; 8536531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 8545960Smckusic brelse(bp); 8554359Smckusick return; 8565960Smckusic } 8578105Sroot cgp->cg_time = time.tv_sec; 8584359Smckusick ino %= fs->fs_ipg; 8596716Smckusick if (isclr(cgp->cg_iused, ino)) { 8606716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 8616716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 8624359Smckusick panic("ifree: freeing free inode"); 8636716Smckusick } 8644359Smckusick clrbit(cgp->cg_iused, ino); 86516784Smckusick if (ino < cgp->cg_irotor) 86616784Smckusick cgp->cg_irotor = ino; 8674792Smckusic cgp->cg_cs.cs_nifree++; 8684792Smckusic fs->fs_cstotal.cs_nifree++; 8695322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 8704359Smckusick if ((mode & IFMT) == IFDIR) { 8714792Smckusic cgp->cg_cs.cs_ndir--; 8724792Smckusic fs->fs_cstotal.cs_ndir--; 8735322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 8744359Smckusick } 8754359Smckusick fs->fs_fmod++; 8764359Smckusick bdwrite(bp); 8774359Smckusick } 8784359Smckusick 8794463Smckusic /* 8805375Smckusic * Find a block of the specified size in the specified cylinder group. 8815375Smckusic * 8824651Smckusic * It is a panic if a request is made to find a block if none are 8834651Smckusic * available. 8844651Smckusic */ 8854651Smckusic daddr_t 8864651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 8874651Smckusic register struct fs *fs; 8884651Smckusic register struct cg *cgp; 8894651Smckusic daddr_t bpref; 8904651Smckusic int allocsiz; 8914651Smckusic { 8924651Smckusic daddr_t bno; 8934651Smckusic int start, len, loc, i; 8944651Smckusic int blk, field, subfield, pos; 8954651Smckusic 8964651Smckusic /* 8974651Smckusic * find the fragment by searching through the free block 8984651Smckusic * map for an appropriate bit pattern 8994651Smckusic */ 9004651Smckusic if (bpref) 9015377Smckusic start = dtogd(fs, bpref) / NBBY; 9024651Smckusic else 9034651Smckusic start = cgp->cg_frotor / NBBY; 9045398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 90512755Ssam loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start], 90612755Ssam (caddr_t)fragtbl[fs->fs_frag], 90712755Ssam (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 9084651Smckusic if (loc == 0) { 9096531Smckusick len = start + 1; 9106531Smckusick start = 0; 91112755Ssam loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start], 91212755Ssam (caddr_t)fragtbl[fs->fs_frag], 91312755Ssam (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 91416784Smckusick if (loc == 0) { 91516784Smckusick printf("start = %d, len = %d, fs = %s\n", 91616784Smckusick start, len, fs->fs_fsmnt); 91716784Smckusick panic("alloccg: map corrupted"); 9186531Smckusick return (-1); 91916784Smckusick } 9204651Smckusic } 9214651Smckusic bno = (start + len - loc) * NBBY; 9224651Smckusic cgp->cg_frotor = bno; 9234651Smckusic /* 9244651Smckusic * found the byte in the map 9254651Smckusic * sift through the bits to find the selected frag 9264651Smckusic */ 9276294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 9286294Smckusick blk = blkmap(fs, cgp->cg_free, bno); 9294651Smckusic blk <<= 1; 9304651Smckusic field = around[allocsiz]; 9314651Smckusic subfield = inside[allocsiz]; 9325322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 9336294Smckusick if ((blk & field) == subfield) 9346294Smckusick return (bno + pos); 9354651Smckusic field <<= 1; 9364651Smckusic subfield <<= 1; 9374651Smckusic } 9384651Smckusic } 9396716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 9404651Smckusic panic("alloccg: block not in map"); 9416531Smckusick return (-1); 9424651Smckusic } 9434651Smckusic 9444651Smckusic /* 9455375Smckusic * Fserr prints the name of a file system with an error diagnostic. 9465375Smckusic * 9475375Smckusic * The form of the error message is: 9484359Smckusick * fs: error message 9494359Smckusick */ 9504359Smckusick fserr(fs, cp) 9514359Smckusick struct fs *fs; 9524359Smckusick char *cp; 9534359Smckusick { 9544359Smckusick 9554359Smckusick printf("%s: %s\n", fs->fs_fsmnt, cp); 9564359Smckusick } 957