1*17224Smckusick /* lfs_alloc.c 6.6 84/09/28 */ 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 } 1565965Smckusic bp = getblk(ip->i_dev, fsbtodb(fs, bno), nsize); 1578617Sroot bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize); 1589163Ssam bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 1594463Smckusic brelse(obp); 1609163Ssam free(ip, bprev, (off_t)osize); 16112643Ssam ip->i_blocks += btodb(nsize - osize); 16212643Ssam ip->i_flag |= IUPD|ICHG; 1636294Smckusick return (bp); 1644463Smckusic } 1654792Smckusic nospace: 1664463Smckusic /* 1674463Smckusic * no space available 1684463Smckusic */ 1694426Smckusic fserr(fs, "file system full"); 1704426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 1714426Smckusic u.u_error = ENOSPC; 1724426Smckusic return (NULL); 1734426Smckusic } 1744426Smckusic 1755375Smckusic /* 1765375Smckusic * Allocate an inode in the file system. 1775375Smckusic * 1785375Smckusic * A preference may be optionally specified. If a preference is given 1795375Smckusic * the following hierarchy is used to allocate an inode: 1805375Smckusic * 1) allocate the requested inode. 1815375Smckusic * 2) allocate an inode in the same cylinder group. 1825375Smckusic * 3) quadradically rehash into other cylinder groups, until an 1835375Smckusic * available inode is located. 1845375Smckusic * If no inode preference is given the following heirarchy is used 1855375Smckusic * to allocate an inode: 1865375Smckusic * 1) allocate an inode in cylinder group 0. 1875375Smckusic * 2) quadradically rehash into other cylinder groups, until an 1885375Smckusic * available inode is located. 1895375Smckusic */ 1904359Smckusick struct inode * 1915965Smckusic ialloc(pip, ipref, mode) 1925965Smckusic register struct inode *pip; 1934359Smckusick ino_t ipref; 1944359Smckusick int mode; 1954359Smckusick { 1965212Smckusic ino_t ino; 1974359Smckusick register struct fs *fs; 1984359Smckusick register struct inode *ip; 1994359Smckusick int cg; 2004359Smckusick 2015965Smckusic fs = pip->i_fs; 2024792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 2034359Smckusick goto noinodes; 2047650Ssam #ifdef QUOTA 20512643Ssam u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0); 20612643Ssam if (u.u_error) 20712643Ssam return (NULL); 2087483Skre #endif 2094948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 2104948Smckusic ipref = 0; 2115377Smckusic cg = itog(fs, ipref); 2125965Smckusic ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); 2134359Smckusick if (ino == 0) 2144359Smckusick goto noinodes; 2155965Smckusic ip = iget(pip->i_dev, pip->i_fs, ino); 2164359Smckusick if (ip == NULL) { 21715120Skarels ifree(pip, ino, 0); 2184359Smckusick return (NULL); 2194359Smckusick } 2206716Smckusick if (ip->i_mode) { 2216716Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 2226716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 2234359Smckusick panic("ialloc: dup alloc"); 2246716Smckusick } 22512643Ssam if (ip->i_blocks) { /* XXX */ 22612643Ssam printf("free inode %s/%d had %d blocks\n", 22712643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 22812643Ssam ip->i_blocks = 0; 22912643Ssam } 2304359Smckusick return (ip); 2314359Smckusick noinodes: 2324359Smckusick fserr(fs, "out of inodes"); 2336294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 2344359Smckusick u.u_error = ENOSPC; 2354359Smckusick return (NULL); 2364359Smckusick } 2374359Smckusick 2384651Smckusic /* 2395375Smckusic * Find a cylinder to place a directory. 2405375Smckusic * 2415375Smckusic * The policy implemented by this algorithm is to select from 2425375Smckusic * among those cylinder groups with above the average number of 2435375Smckusic * free inodes, the one with the smallest number of directories. 2444651Smckusic */ 2459163Ssam ino_t 2465965Smckusic dirpref(fs) 2475965Smckusic register struct fs *fs; 2484359Smckusick { 2494651Smckusic int cg, minndir, mincg, avgifree; 2504359Smckusick 2514792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 2524651Smckusic minndir = fs->fs_ipg; 2534359Smckusick mincg = 0; 2544651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 2555322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 2565322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 2574359Smckusick mincg = cg; 2585322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 2594359Smckusick } 2609163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 2614359Smckusick } 2624359Smckusick 2634651Smckusic /* 2649163Ssam * Select the desired position for the next block in a file. The file is 2659163Ssam * logically divided into sections. The first section is composed of the 2669163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 2679163Ssam * 2689163Ssam * If no blocks have been allocated in the first section, the policy is to 2699163Ssam * request a block in the same cylinder group as the inode that describes 2709163Ssam * the file. If no blocks have been allocated in any other section, the 2719163Ssam * policy is to place the section in a cylinder group with a greater than 2729163Ssam * average number of free blocks. An appropriate cylinder group is found 2739163Ssam * by maintaining a rotor that sweeps the cylinder groups. When a new 2749163Ssam * group of blocks is needed, the rotor is advanced until a cylinder group 2759163Ssam * with greater than the average number of free blocks is found. 2769163Ssam * 2779163Ssam * If a section is already partially allocated, the policy is to 2789163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 2799163Ssam * contiguous blocks and the beginning of the next is physically separated 2809163Ssam * so that the disk head will be in transit between them for at least 2819163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 2829163Ssam * schedule another I/O transfer. 2834651Smckusic */ 2845212Smckusic daddr_t 2859163Ssam blkpref(ip, lbn, indx, bap) 2869163Ssam struct inode *ip; 2879163Ssam daddr_t lbn; 2889163Ssam int indx; 2899163Ssam daddr_t *bap; 2909163Ssam { 2915965Smckusic register struct fs *fs; 2924651Smckusic int cg, avgbfree; 2939163Ssam daddr_t nextblk; 2944651Smckusic 2959163Ssam fs = ip->i_fs; 2969163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 2979163Ssam if (lbn < NDADDR) { 2989163Ssam cg = itog(fs, ip->i_number); 2995322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 3004651Smckusic } 3019163Ssam /* 3029163Ssam * Find a cylinder with greater than average number of 3039163Ssam * unused data blocks. 3049163Ssam */ 3059163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 3069163Ssam for (cg = fs->fs_cgrotor + 1; cg < fs->fs_ncg; cg++) 3079163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3089163Ssam fs->fs_cgrotor = cg; 3099163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3109163Ssam } 3119163Ssam for (cg = 0; cg <= fs->fs_cgrotor; cg++) 3129163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3139163Ssam fs->fs_cgrotor = cg; 3149163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3159163Ssam } 3169163Ssam return (NULL); 3179163Ssam } 3189163Ssam /* 3199163Ssam * One or more previous blocks have been laid out. If less 3209163Ssam * than fs_maxcontig previous blocks are contiguous, the 3219163Ssam * next block is requested contiguously, otherwise it is 3229163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 3239163Ssam */ 3249163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 3259163Ssam if (indx > fs->fs_maxcontig && 32611638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 3279163Ssam != nextblk) 3289163Ssam return (nextblk); 3299163Ssam if (fs->fs_rotdelay != 0) 3309163Ssam /* 3319163Ssam * Here we convert ms of delay to frags as: 3329163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 3339163Ssam * ((sect/frag) * (ms/sec)) 3349163Ssam * then round up to the next block. 3359163Ssam */ 3369163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 3379163Ssam (NSPF(fs) * 1000), fs->fs_frag); 3389163Ssam return (nextblk); 3394651Smckusic } 3404651Smckusic 3415375Smckusic /* 3425375Smckusic * Implement the cylinder overflow algorithm. 3435375Smckusic * 3445375Smckusic * The policy implemented by this algorithm is: 3455375Smckusic * 1) allocate the block in its requested cylinder group. 3465375Smckusic * 2) quadradically rehash on the cylinder group number. 3475375Smckusic * 3) brute force search for a free block. 3485375Smckusic */ 3495212Smckusic /*VARARGS5*/ 3505212Smckusic u_long 3515965Smckusic hashalloc(ip, cg, pref, size, allocator) 3525965Smckusic struct inode *ip; 3534359Smckusick int cg; 3544359Smckusick long pref; 3554359Smckusick int size; /* size for data blocks, mode for inodes */ 3565212Smckusic u_long (*allocator)(); 3574359Smckusick { 3585965Smckusic register struct fs *fs; 3594359Smckusick long result; 3604359Smckusick int i, icg = cg; 3614359Smckusick 3625965Smckusic fs = ip->i_fs; 3634359Smckusick /* 3644359Smckusick * 1: preferred cylinder group 3654359Smckusick */ 3665965Smckusic result = (*allocator)(ip, cg, pref, size); 3674359Smckusick if (result) 3684359Smckusick return (result); 3694359Smckusick /* 3704359Smckusick * 2: quadratic rehash 3714359Smckusick */ 3724359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 3734359Smckusick cg += i; 3744359Smckusick if (cg >= fs->fs_ncg) 3754359Smckusick cg -= fs->fs_ncg; 3765965Smckusic result = (*allocator)(ip, cg, 0, size); 3774359Smckusick if (result) 3784359Smckusick return (result); 3794359Smckusick } 3804359Smckusick /* 3814359Smckusick * 3: brute force search 38210847Ssam * Note that we start at i == 2, since 0 was checked initially, 38310847Ssam * and 1 is always checked in the quadratic rehash. 3844359Smckusick */ 38510848Smckusick cg = (icg + 2) % fs->fs_ncg; 38610847Ssam for (i = 2; i < fs->fs_ncg; i++) { 3875965Smckusic result = (*allocator)(ip, cg, 0, size); 3884359Smckusick if (result) 3894359Smckusick return (result); 3904359Smckusick cg++; 3914359Smckusick if (cg == fs->fs_ncg) 3924359Smckusick cg = 0; 3934359Smckusick } 3946294Smckusick return (NULL); 3954359Smckusick } 3964359Smckusick 3975375Smckusic /* 3985375Smckusic * Determine whether a fragment can be extended. 3995375Smckusic * 4005375Smckusic * Check to see if the necessary fragments are available, and 4015375Smckusic * if they are, allocate them. 4025375Smckusic */ 4034359Smckusick daddr_t 4045965Smckusic fragextend(ip, cg, bprev, osize, nsize) 4055965Smckusic struct inode *ip; 4064426Smckusic int cg; 4074463Smckusic long bprev; 4084426Smckusic int osize, nsize; 4094426Smckusic { 4105965Smckusic register struct fs *fs; 4114463Smckusic register struct buf *bp; 4124463Smckusic register struct cg *cgp; 4134463Smckusic long bno; 4144463Smckusic int frags, bbase; 4154426Smckusic int i; 4164426Smckusic 4175965Smckusic fs = ip->i_fs; 418*17224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 4196531Smckusick return (NULL); 4205960Smckusic frags = numfrags(fs, nsize); 421*17224Smckusick bbase = fragnum(fs, bprev); 422*17224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 4234463Smckusic /* cannot extend across a block boundry */ 4246294Smckusick return (NULL); 4254463Smckusic } 42610278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 4276531Smckusick cgp = bp->b_un.b_cg; 4286531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 4295960Smckusic brelse(bp); 4306294Smckusick return (NULL); 4315960Smckusic } 4328105Sroot cgp->cg_time = time.tv_sec; 4335377Smckusic bno = dtogd(fs, bprev); 4345960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 4355361Smckusic if (isclr(cgp->cg_free, bno + i)) { 4365361Smckusic brelse(bp); 4376294Smckusick return (NULL); 4385361Smckusic } 4395361Smckusic /* 4405361Smckusic * the current fragment can be extended 4415361Smckusic * deduct the count on fragment being extended into 4425361Smckusic * increase the count on the remaining fragment (if any) 4435361Smckusic * allocate the extended piece 4445361Smckusic */ 4455361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 4464463Smckusic if (isclr(cgp->cg_free, bno + i)) 4474463Smckusic break; 4485960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 4495361Smckusic if (i != frags) 4505361Smckusic cgp->cg_frsum[i - frags]++; 4515960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 4525361Smckusic clrbit(cgp->cg_free, bno + i); 4535361Smckusic cgp->cg_cs.cs_nffree--; 4545361Smckusic fs->fs_cstotal.cs_nffree--; 4555361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 4564463Smckusic } 4575361Smckusic fs->fs_fmod++; 4585361Smckusic bdwrite(bp); 4595361Smckusic return (bprev); 4604426Smckusic } 4614426Smckusic 4625375Smckusic /* 4635375Smckusic * Determine whether a block can be allocated. 4645375Smckusic * 4655375Smckusic * Check to see if a block of the apprpriate size is available, 4665375Smckusic * and if it is, allocate it. 4675375Smckusic */ 4689163Ssam daddr_t 4695965Smckusic alloccg(ip, cg, bpref, size) 4705965Smckusic struct inode *ip; 4714359Smckusick int cg; 4724359Smckusick daddr_t bpref; 4734359Smckusick int size; 4744359Smckusick { 4755965Smckusic register struct fs *fs; 4764463Smckusic register struct buf *bp; 4774463Smckusic register struct cg *cgp; 4784463Smckusic int bno, frags; 4794463Smckusic int allocsiz; 4804463Smckusic register int i; 4814359Smckusick 4825965Smckusic fs = ip->i_fs; 4835322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 4846294Smckusick return (NULL); 48510278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 4866531Smckusick cgp = bp->b_un.b_cg; 48715950Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC || 48815950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 4895960Smckusic brelse(bp); 4906294Smckusick return (NULL); 4915960Smckusic } 4928105Sroot cgp->cg_time = time.tv_sec; 4935322Smckusic if (size == fs->fs_bsize) { 4945212Smckusic bno = alloccgblk(fs, cgp, bpref); 4954463Smckusic bdwrite(bp); 4964463Smckusic return (bno); 4974463Smckusic } 4984463Smckusic /* 4994463Smckusic * check to see if any fragments are already available 5004463Smckusic * allocsiz is the size which will be allocated, hacking 5014463Smckusic * it down to a smaller size if necessary 5024463Smckusic */ 5035960Smckusic frags = numfrags(fs, size); 5045322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 5054463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 5064463Smckusic break; 5075322Smckusic if (allocsiz == fs->fs_frag) { 5084463Smckusic /* 5094463Smckusic * no fragments were available, so a block will be 5104463Smckusic * allocated, and hacked up 5114463Smckusic */ 5124792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 5134463Smckusic brelse(bp); 5146294Smckusick return (NULL); 5154463Smckusic } 5165212Smckusic bno = alloccgblk(fs, cgp, bpref); 5175377Smckusic bpref = dtogd(fs, bno); 5185322Smckusic for (i = frags; i < fs->fs_frag; i++) 5194463Smckusic setbit(cgp->cg_free, bpref + i); 5205322Smckusic i = fs->fs_frag - frags; 5214792Smckusic cgp->cg_cs.cs_nffree += i; 5224792Smckusic fs->fs_cstotal.cs_nffree += i; 5235322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 5249762Ssam fs->fs_fmod++; 5254463Smckusic cgp->cg_frsum[i]++; 5264463Smckusic bdwrite(bp); 5274463Smckusic return (bno); 5284463Smckusic } 5294651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 53015950Smckusick if (bno < 0) { 53115950Smckusick brelse(bp); 5326294Smckusick return (NULL); 53315950Smckusick } 5344463Smckusic for (i = 0; i < frags; i++) 5354463Smckusic clrbit(cgp->cg_free, bno + i); 5364792Smckusic cgp->cg_cs.cs_nffree -= frags; 5374792Smckusic fs->fs_cstotal.cs_nffree -= frags; 5385322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 5399762Ssam fs->fs_fmod++; 5404463Smckusic cgp->cg_frsum[allocsiz]--; 5414463Smckusic if (frags != allocsiz) 5424463Smckusic cgp->cg_frsum[allocsiz - frags]++; 5434463Smckusic bdwrite(bp); 5444463Smckusic return (cg * fs->fs_fpg + bno); 5454463Smckusic } 5464463Smckusic 5475375Smckusic /* 5485375Smckusic * Allocate a block in a cylinder group. 5495375Smckusic * 5505375Smckusic * This algorithm implements the following policy: 5515375Smckusic * 1) allocate the requested block. 5525375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 5535375Smckusic * 3) allocate the next available block on the block rotor for the 5545375Smckusic * specified cylinder group. 5555375Smckusic * Note that this routine only allocates fs_bsize blocks; these 5565375Smckusic * blocks may be fragmented by the routine that allocates them. 5575375Smckusic */ 5584463Smckusic daddr_t 5595212Smckusic alloccgblk(fs, cgp, bpref) 5605965Smckusic register struct fs *fs; 5614463Smckusic register struct cg *cgp; 5624463Smckusic daddr_t bpref; 5634463Smckusic { 5644651Smckusic daddr_t bno; 5656294Smckusick int cylno, pos, delta; 5664651Smckusic short *cylbp; 5675361Smckusic register int i; 5684463Smckusic 5694651Smckusic if (bpref == 0) { 5704651Smckusic bpref = cgp->cg_rotor; 5715361Smckusic goto norot; 5725361Smckusic } 573*17224Smckusick bpref = blknum(fs, bpref); 5745377Smckusic bpref = dtogd(fs, bpref); 5755361Smckusic /* 5765361Smckusic * if the requested block is available, use it 5775361Smckusic */ 57811638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) { 5795361Smckusic bno = bpref; 5805361Smckusic goto gotit; 5815361Smckusic } 5825361Smckusic /* 5835361Smckusic * check for a block available on the same cylinder 5845361Smckusic */ 5855361Smckusic cylno = cbtocylno(fs, bpref); 5865375Smckusic if (cgp->cg_btot[cylno] == 0) 5875375Smckusic goto norot; 5885375Smckusic if (fs->fs_cpc == 0) { 5895375Smckusic /* 5905375Smckusic * block layout info is not available, so just have 5915375Smckusic * to take any block in this cylinder. 5925375Smckusic */ 5935375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 5945375Smckusic goto norot; 5955375Smckusic } 5965375Smckusic /* 5975361Smckusic * check the summary information to see if a block is 5985361Smckusic * available in the requested cylinder starting at the 5999163Ssam * requested rotational position and proceeding around. 6005361Smckusic */ 6019163Ssam cylbp = cgp->cg_b[cylno]; 6029163Ssam pos = cbtorpos(fs, bpref); 6035361Smckusic for (i = pos; i < NRPOS; i++) 6045361Smckusic if (cylbp[i] > 0) 6055361Smckusic break; 6065361Smckusic if (i == NRPOS) 6075361Smckusic for (i = 0; i < pos; i++) 6085361Smckusic if (cylbp[i] > 0) 6095361Smckusic break; 6105361Smckusic if (cylbp[i] > 0) { 6114651Smckusic /* 6125361Smckusic * found a rotational position, now find the actual 6135361Smckusic * block. A panic if none is actually there. 6144651Smckusic */ 6155361Smckusic pos = cylno % fs->fs_cpc; 6165361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 6176716Smckusick if (fs->fs_postbl[pos][i] == -1) { 6186716Smckusick printf("pos = %d, i = %d, fs = %s\n", 6196716Smckusick pos, i, fs->fs_fsmnt); 6205361Smckusic panic("alloccgblk: cyl groups corrupted"); 6216716Smckusick } 6226294Smckusick for (i = fs->fs_postbl[pos][i];; ) { 6235361Smckusic if (isblock(fs, cgp->cg_free, bno + i)) { 62411638Ssam bno = blkstofrags(fs, (bno + i)); 6255361Smckusic goto gotit; 6265361Smckusic } 6276294Smckusick delta = fs->fs_rotbl[i]; 6286294Smckusick if (delta <= 0 || delta > MAXBPC - i) 6294651Smckusic break; 6306294Smckusick i += delta; 6314651Smckusic } 6326716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 6335361Smckusic panic("alloccgblk: can't find blk in cyl"); 6344359Smckusick } 6355361Smckusic norot: 6365361Smckusic /* 6375361Smckusic * no blocks in the requested cylinder, so take next 6385361Smckusic * available one in this cylinder group. 6395361Smckusic */ 6408628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 6416567Smckusic if (bno < 0) 6426294Smckusick return (NULL); 6434651Smckusic cgp->cg_rotor = bno; 6444359Smckusick gotit: 64511638Ssam clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno)); 6464792Smckusic cgp->cg_cs.cs_nbfree--; 6474792Smckusic fs->fs_cstotal.cs_nbfree--; 6485322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 6495375Smckusic cylno = cbtocylno(fs, bno); 6505375Smckusic cgp->cg_b[cylno][cbtorpos(fs, bno)]--; 6515375Smckusic cgp->cg_btot[cylno]--; 6524359Smckusick fs->fs_fmod++; 6534651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 6544359Smckusick } 6554359Smckusick 6565375Smckusic /* 6575375Smckusic * Determine whether an inode can be allocated. 6585375Smckusic * 6595375Smckusic * Check to see if an inode is available, and if it is, 6605375Smckusic * allocate it using the following policy: 6615375Smckusic * 1) allocate the requested inode. 6625375Smckusic * 2) allocate the next available inode after the requested 6635375Smckusic * inode in the specified cylinder group. 6645375Smckusic */ 6659163Ssam ino_t 6665965Smckusic ialloccg(ip, cg, ipref, mode) 6675965Smckusic struct inode *ip; 6684359Smckusick int cg; 6694359Smckusick daddr_t ipref; 6704359Smckusick int mode; 6714359Smckusick { 6725965Smckusic register struct fs *fs; 6734463Smckusic register struct cg *cgp; 67416784Smckusick struct buf *bp; 67516784Smckusick int start, len, loc, map, i; 6764359Smckusick 6775965Smckusic fs = ip->i_fs; 6785322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 6796294Smckusick return (NULL); 68010278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 6816531Smckusick cgp = bp->b_un.b_cg; 68215950Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC || 68315950Smckusick cgp->cg_cs.cs_nifree == 0) { 6845960Smckusic brelse(bp); 6856294Smckusick return (NULL); 6865960Smckusic } 6878105Sroot cgp->cg_time = time.tv_sec; 6884359Smckusick if (ipref) { 6894359Smckusick ipref %= fs->fs_ipg; 6904359Smckusick if (isclr(cgp->cg_iused, ipref)) 6914359Smckusick goto gotit; 69216784Smckusick } 69316784Smckusick start = cgp->cg_irotor / NBBY; 69416784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 69516784Smckusick loc = skpc(0xff, len, &cgp->cg_iused[start]); 69616784Smckusick if (loc == 0) { 69716784Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 69816784Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 69916784Smckusick panic("ialloccg: map corrupted"); 70016784Smckusick /* NOTREACHED */ 70116784Smckusick } 70216784Smckusick i = start + len - loc; 70316784Smckusick map = cgp->cg_iused[i]; 70416784Smckusick ipref = i * NBBY; 70516784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 70616784Smckusick if ((map & i) == 0) { 7074359Smckusick cgp->cg_irotor = ipref; 7084359Smckusick goto gotit; 7094359Smckusick } 7104359Smckusick } 71116784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 71216784Smckusick panic("ialloccg: block not in map"); 71316784Smckusick /* NOTREACHED */ 7144359Smckusick gotit: 7154359Smckusick setbit(cgp->cg_iused, ipref); 7164792Smckusic cgp->cg_cs.cs_nifree--; 7174792Smckusic fs->fs_cstotal.cs_nifree--; 7185322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 7194359Smckusick fs->fs_fmod++; 7204359Smckusick if ((mode & IFMT) == IFDIR) { 7214792Smckusic cgp->cg_cs.cs_ndir++; 7224792Smckusic fs->fs_cstotal.cs_ndir++; 7235322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 7244359Smckusick } 7254359Smckusick bdwrite(bp); 7264359Smckusick return (cg * fs->fs_ipg + ipref); 7274359Smckusick } 7284359Smckusick 7295375Smckusic /* 7305375Smckusic * Free a block or fragment. 7315375Smckusic * 7325375Smckusic * The specified block or fragment is placed back in the 7335375Smckusic * free map. If a fragment is deallocated, a possible 7345375Smckusic * block reassembly is checked. 7355375Smckusic */ 7369163Ssam free(ip, bno, size) 7375965Smckusic register struct inode *ip; 7384359Smckusick daddr_t bno; 7395212Smckusic off_t size; 7404359Smckusick { 7414359Smckusick register struct fs *fs; 7424359Smckusick register struct cg *cgp; 7434359Smckusick register struct buf *bp; 7444463Smckusic int cg, blk, frags, bbase; 7454463Smckusic register int i; 7464359Smckusick 7475965Smckusic fs = ip->i_fs; 7486716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 7496716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 7506716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 7514426Smckusic panic("free: bad size"); 7526716Smckusick } 7535377Smckusic cg = dtog(fs, bno); 7546567Smckusic if (badblock(fs, bno)) { 7556567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 7564359Smckusick return; 7576567Smckusic } 75810278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 7596531Smckusick cgp = bp->b_un.b_cg; 7606531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 7615960Smckusic brelse(bp); 7624359Smckusick return; 7635960Smckusic } 7648105Sroot cgp->cg_time = time.tv_sec; 7655377Smckusic bno = dtogd(fs, bno); 7665322Smckusic if (size == fs->fs_bsize) { 76711638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) { 7686716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 7696716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 7704426Smckusic panic("free: freeing free block"); 7716567Smckusic } 77211638Ssam setblock(fs, cgp->cg_free, fragstoblks(fs, bno)); 7734792Smckusic cgp->cg_cs.cs_nbfree++; 7744792Smckusic fs->fs_cstotal.cs_nbfree++; 7755322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 7765375Smckusic i = cbtocylno(fs, bno); 7775375Smckusic cgp->cg_b[i][cbtorpos(fs, bno)]++; 7785375Smckusic cgp->cg_btot[i]++; 7794426Smckusic } else { 780*17224Smckusick bbase = bno - fragnum(fs, bno); 7814463Smckusic /* 7824463Smckusic * decrement the counts associated with the old frags 7834463Smckusic */ 7846294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 7855322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 7864463Smckusic /* 7874463Smckusic * deallocate the fragment 7884463Smckusic */ 7895960Smckusic frags = numfrags(fs, size); 7904463Smckusic for (i = 0; i < frags; i++) { 7916716Smckusick if (isset(cgp->cg_free, bno + i)) { 7926716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 7936716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 7944426Smckusic panic("free: freeing free frag"); 7956716Smckusick } 7964426Smckusic setbit(cgp->cg_free, bno + i); 7974426Smckusic } 7986294Smckusick cgp->cg_cs.cs_nffree += i; 7996294Smckusick fs->fs_cstotal.cs_nffree += i; 8006294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 8014463Smckusic /* 8024463Smckusic * add back in counts associated with the new frags 8034463Smckusic */ 8046294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 8055322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 8064463Smckusic /* 8074463Smckusic * if a complete block has been reassembled, account for it 8084463Smckusic */ 80911638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) { 8105322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 8115322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 8125322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 8134792Smckusic cgp->cg_cs.cs_nbfree++; 8144792Smckusic fs->fs_cstotal.cs_nbfree++; 8155322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 8165375Smckusic i = cbtocylno(fs, bbase); 8175375Smckusic cgp->cg_b[i][cbtorpos(fs, bbase)]++; 8185375Smckusic cgp->cg_btot[i]++; 8194426Smckusic } 8204426Smckusic } 8214359Smckusick fs->fs_fmod++; 8224359Smckusick bdwrite(bp); 8234359Smckusick } 8244359Smckusick 8255375Smckusic /* 8265375Smckusic * Free an inode. 8275375Smckusic * 8285375Smckusic * The specified inode is placed back in the free map. 8295375Smckusic */ 8305965Smckusic ifree(ip, ino, mode) 8315965Smckusic struct inode *ip; 8324359Smckusick ino_t ino; 8334359Smckusick int mode; 8344359Smckusick { 8354359Smckusick register struct fs *fs; 8364359Smckusick register struct cg *cgp; 8374359Smckusick register struct buf *bp; 8384359Smckusick int cg; 8394359Smckusick 8405965Smckusic fs = ip->i_fs; 8416716Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 8426716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 8436716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 8444359Smckusick panic("ifree: range"); 8456716Smckusick } 8465377Smckusic cg = itog(fs, ino); 84710278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 8486531Smckusick cgp = bp->b_un.b_cg; 8496531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 8505960Smckusic brelse(bp); 8514359Smckusick return; 8525960Smckusic } 8538105Sroot cgp->cg_time = time.tv_sec; 8544359Smckusick ino %= fs->fs_ipg; 8556716Smckusick if (isclr(cgp->cg_iused, ino)) { 8566716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 8576716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 8584359Smckusick panic("ifree: freeing free inode"); 8596716Smckusick } 8604359Smckusick clrbit(cgp->cg_iused, ino); 86116784Smckusick if (ino < cgp->cg_irotor) 86216784Smckusick cgp->cg_irotor = ino; 8634792Smckusic cgp->cg_cs.cs_nifree++; 8644792Smckusic fs->fs_cstotal.cs_nifree++; 8655322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 8664359Smckusick if ((mode & IFMT) == IFDIR) { 8674792Smckusic cgp->cg_cs.cs_ndir--; 8684792Smckusic fs->fs_cstotal.cs_ndir--; 8695322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 8704359Smckusick } 8714359Smckusick fs->fs_fmod++; 8724359Smckusick bdwrite(bp); 8734359Smckusick } 8744359Smckusick 8754463Smckusic /* 8765375Smckusic * Find a block of the specified size in the specified cylinder group. 8775375Smckusic * 8784651Smckusic * It is a panic if a request is made to find a block if none are 8794651Smckusic * available. 8804651Smckusic */ 8814651Smckusic daddr_t 8824651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 8834651Smckusic register struct fs *fs; 8844651Smckusic register struct cg *cgp; 8854651Smckusic daddr_t bpref; 8864651Smckusic int allocsiz; 8874651Smckusic { 8884651Smckusic daddr_t bno; 8894651Smckusic int start, len, loc, i; 8904651Smckusic int blk, field, subfield, pos; 8914651Smckusic 8924651Smckusic /* 8934651Smckusic * find the fragment by searching through the free block 8944651Smckusic * map for an appropriate bit pattern 8954651Smckusic */ 8964651Smckusic if (bpref) 8975377Smckusic start = dtogd(fs, bpref) / NBBY; 8984651Smckusic else 8994651Smckusic start = cgp->cg_frotor / NBBY; 9005398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 90112755Ssam loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start], 90212755Ssam (caddr_t)fragtbl[fs->fs_frag], 90312755Ssam (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 9044651Smckusic if (loc == 0) { 9056531Smckusick len = start + 1; 9066531Smckusick start = 0; 90712755Ssam loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start], 90812755Ssam (caddr_t)fragtbl[fs->fs_frag], 90912755Ssam (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 91016784Smckusick if (loc == 0) { 91116784Smckusick printf("start = %d, len = %d, fs = %s\n", 91216784Smckusick start, len, fs->fs_fsmnt); 91316784Smckusick panic("alloccg: map corrupted"); 9146531Smckusick return (-1); 91516784Smckusick } 9164651Smckusic } 9174651Smckusic bno = (start + len - loc) * NBBY; 9184651Smckusic cgp->cg_frotor = bno; 9194651Smckusic /* 9204651Smckusic * found the byte in the map 9214651Smckusic * sift through the bits to find the selected frag 9224651Smckusic */ 9236294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 9246294Smckusick blk = blkmap(fs, cgp->cg_free, bno); 9254651Smckusic blk <<= 1; 9264651Smckusic field = around[allocsiz]; 9274651Smckusic subfield = inside[allocsiz]; 9285322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 9296294Smckusick if ((blk & field) == subfield) 9306294Smckusick return (bno + pos); 9314651Smckusic field <<= 1; 9324651Smckusic subfield <<= 1; 9334651Smckusic } 9344651Smckusic } 9356716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 9364651Smckusic panic("alloccg: block not in map"); 9376531Smckusick return (-1); 9384651Smckusic } 9394651Smckusic 9404651Smckusic /* 9415375Smckusic * Fserr prints the name of a file system with an error diagnostic. 9425375Smckusic * 9435375Smckusic * The form of the error message is: 9444359Smckusick * fs: error message 9454359Smckusick */ 9464359Smckusick fserr(fs, cp) 9474359Smckusick struct fs *fs; 9484359Smckusick char *cp; 9494359Smckusick { 9504359Smckusick 9514359Smckusick printf("%s: %s\n", fs->fs_fsmnt, cp); 9524359Smckusick } 953