1*9762Ssam /* lfs_alloc.c 2.20 82/12/17 */ 24359Smckusick 34359Smckusick #include "../h/param.h" 44359Smckusick #include "../h/systm.h" 54359Smckusick #include "../h/mount.h" 64359Smckusick #include "../h/fs.h" 74359Smckusick #include "../h/conf.h" 84359Smckusick #include "../h/buf.h" 94359Smckusick #include "../h/inode.h" 106567Smckusic #include "../h/dir.h" 114359Smckusick #include "../h/user.h" 127483Skre #include "../h/quota.h" 138105Sroot #include "../h/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; 634792Smckusic if (u.u_uid != 0 && 645322Smckusic fs->fs_cstotal.cs_nbfree * fs->fs_frag + fs->fs_cstotal.cs_nffree < 655322Smckusic fs->fs_dsize * fs->fs_minfree / 100) 664792Smckusic goto nospace; 677650Ssam #ifdef QUOTA 687483Skre if (chkdq(ip, (long)((unsigned)size/DEV_BSIZE), 0)) 697483Skre return(NULL); 707483Skre #endif 714948Smckusic if (bpref >= fs->fs_size) 724948Smckusic bpref = 0; 734359Smckusick if (bpref == 0) 745377Smckusic cg = itog(fs, ip->i_number); 754359Smckusick else 765377Smckusic cg = dtog(fs, bpref); 779163Ssam bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, 789163Ssam (u_long (*)())alloccg); 796567Smckusic if (bno <= 0) 804359Smckusick goto nospace; 815965Smckusic bp = getblk(ip->i_dev, fsbtodb(fs, bno), size); 824359Smckusick clrbuf(bp); 834359Smckusick return (bp); 844359Smckusick nospace: 854359Smckusick fserr(fs, "file system full"); 864359Smckusick uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 874359Smckusick u.u_error = ENOSPC; 884359Smckusick return (NULL); 894359Smckusick } 904359Smckusick 915375Smckusic /* 925375Smckusic * Reallocate a fragment to a bigger size 935375Smckusic * 945375Smckusic * The number and size of the old block is given, and a preference 955375Smckusic * and new size is also specified. The allocator attempts to extend 965375Smckusic * the original block. Failing that, the regular block allocator is 975375Smckusic * invoked to get an appropriate block. 985375Smckusic */ 994426Smckusic struct buf * 1005965Smckusic realloccg(ip, bprev, bpref, osize, nsize) 1015965Smckusic register struct inode *ip; 1024651Smckusic daddr_t bprev, bpref; 1034426Smckusic int osize, nsize; 1044426Smckusic { 1054426Smckusic daddr_t bno; 1064426Smckusic register struct fs *fs; 1074463Smckusic register struct buf *bp, *obp; 1084426Smckusic int cg; 1094426Smckusic 1105965Smckusic fs = ip->i_fs; 1115960Smckusic if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 1126716Smckusick (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 1136716Smckusick printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 1146716Smckusick ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 1154463Smckusic panic("realloccg: bad size"); 1166716Smckusick } 1174792Smckusic if (u.u_uid != 0 && 1185322Smckusic fs->fs_cstotal.cs_nbfree * fs->fs_frag + fs->fs_cstotal.cs_nffree < 1195322Smckusic fs->fs_dsize * fs->fs_minfree / 100) 1204792Smckusic goto nospace; 1216716Smckusick if (bprev == 0) { 1226716Smckusick printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 1236716Smckusick ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 1244463Smckusic panic("realloccg: bad bprev"); 1256716Smckusick } 1267650Ssam #ifdef QUOTA 1277483Skre if (chkdq(ip, (long)((unsigned)(nsize-osize)/DEV_BSIZE), 0)) 1287483Skre 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); 1424463Smckusic return (bp); 1434463Smckusic } 1444948Smckusic if (bpref >= fs->fs_size) 1454948Smckusic bpref = 0; 1469163Ssam bno = (daddr_t)hashalloc(ip, cg, (long)bpref, nsize, 1479163Ssam (u_long (*)())alloccg); 1486567Smckusic if (bno > 0) { 1495965Smckusic obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize); 1505960Smckusic if (obp->b_flags & B_ERROR) { 1515960Smckusic brelse(obp); 1526294Smckusick return (NULL); 1535960Smckusic } 1545965Smckusic bp = getblk(ip->i_dev, fsbtodb(fs, bno), nsize); 1558617Sroot bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize); 1569163Ssam bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 1574463Smckusic brelse(obp); 1589163Ssam free(ip, bprev, (off_t)osize); 1596294Smckusick return (bp); 1604463Smckusic } 1614792Smckusic nospace: 1624463Smckusic /* 1634463Smckusic * no space available 1644463Smckusic */ 1654426Smckusic fserr(fs, "file system full"); 1664426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 1674426Smckusic u.u_error = ENOSPC; 1684426Smckusic return (NULL); 1694426Smckusic } 1704426Smckusic 1715375Smckusic /* 1725375Smckusic * Allocate an inode in the file system. 1735375Smckusic * 1745375Smckusic * A preference may be optionally specified. If a preference is given 1755375Smckusic * the following hierarchy is used to allocate an inode: 1765375Smckusic * 1) allocate the requested inode. 1775375Smckusic * 2) allocate an inode in the same cylinder group. 1785375Smckusic * 3) quadradically rehash into other cylinder groups, until an 1795375Smckusic * available inode is located. 1805375Smckusic * If no inode preference is given the following heirarchy is used 1815375Smckusic * to allocate an inode: 1825375Smckusic * 1) allocate an inode in cylinder group 0. 1835375Smckusic * 2) quadradically rehash into other cylinder groups, until an 1845375Smckusic * available inode is located. 1855375Smckusic */ 1864359Smckusick struct inode * 1875965Smckusic ialloc(pip, ipref, mode) 1885965Smckusic register struct inode *pip; 1894359Smckusick ino_t ipref; 1904359Smckusick int mode; 1914359Smckusick { 1925212Smckusic ino_t ino; 1934359Smckusick register struct fs *fs; 1944359Smckusick register struct inode *ip; 1954359Smckusick int cg; 1964359Smckusick 1975965Smckusic fs = pip->i_fs; 1984792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 1994359Smckusick goto noinodes; 2007650Ssam #ifdef QUOTA 2019163Ssam if (chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0)) 2027483Skre return(NULL); 2037483Skre #endif 2044948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 2054948Smckusic ipref = 0; 2065377Smckusic cg = itog(fs, ipref); 2075965Smckusic ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); 2084359Smckusick if (ino == 0) 2094359Smckusick goto noinodes; 2105965Smckusic ip = iget(pip->i_dev, pip->i_fs, ino); 2114359Smckusick if (ip == NULL) { 2125965Smckusic ifree(ip, ino, 0); 2134359Smckusick return (NULL); 2144359Smckusick } 2156716Smckusick if (ip->i_mode) { 2166716Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 2176716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 2184359Smckusick panic("ialloc: dup alloc"); 2196716Smckusick } 2204359Smckusick return (ip); 2214359Smckusick noinodes: 2224359Smckusick fserr(fs, "out of inodes"); 2236294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 2244359Smckusick u.u_error = ENOSPC; 2254359Smckusick return (NULL); 2264359Smckusick } 2274359Smckusick 2284651Smckusic /* 2295375Smckusic * Find a cylinder to place a directory. 2305375Smckusic * 2315375Smckusic * The policy implemented by this algorithm is to select from 2325375Smckusic * among those cylinder groups with above the average number of 2335375Smckusic * free inodes, the one with the smallest number of directories. 2344651Smckusic */ 2359163Ssam ino_t 2365965Smckusic dirpref(fs) 2375965Smckusic register struct fs *fs; 2384359Smckusick { 2394651Smckusic int cg, minndir, mincg, avgifree; 2404359Smckusick 2414792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 2424651Smckusic minndir = fs->fs_ipg; 2434359Smckusick mincg = 0; 2444651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 2455322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 2465322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 2474359Smckusick mincg = cg; 2485322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 2494359Smckusick } 2509163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 2514359Smckusick } 2524359Smckusick 2534651Smckusic /* 2549163Ssam * Select the desired position for the next block in a file. The file is 2559163Ssam * logically divided into sections. The first section is composed of the 2569163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 2579163Ssam * 2589163Ssam * If no blocks have been allocated in the first section, the policy is to 2599163Ssam * request a block in the same cylinder group as the inode that describes 2609163Ssam * the file. If no blocks have been allocated in any other section, the 2619163Ssam * policy is to place the section in a cylinder group with a greater than 2629163Ssam * average number of free blocks. An appropriate cylinder group is found 2639163Ssam * by maintaining a rotor that sweeps the cylinder groups. When a new 2649163Ssam * group of blocks is needed, the rotor is advanced until a cylinder group 2659163Ssam * with greater than the average number of free blocks is found. 2669163Ssam * 2679163Ssam * If a section is already partially allocated, the policy is to 2689163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 2699163Ssam * contiguous blocks and the beginning of the next is physically separated 2709163Ssam * so that the disk head will be in transit between them for at least 2719163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 2729163Ssam * schedule another I/O transfer. 2734651Smckusic */ 2745212Smckusic daddr_t 2759163Ssam blkpref(ip, lbn, indx, bap) 2769163Ssam struct inode *ip; 2779163Ssam daddr_t lbn; 2789163Ssam int indx; 2799163Ssam daddr_t *bap; 2809163Ssam { 2815965Smckusic register struct fs *fs; 2824651Smckusic int cg, avgbfree; 2839163Ssam daddr_t nextblk; 2844651Smckusic 2859163Ssam fs = ip->i_fs; 2869163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 2879163Ssam if (lbn < NDADDR) { 2889163Ssam cg = itog(fs, ip->i_number); 2895322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 2904651Smckusic } 2919163Ssam /* 2929163Ssam * Find a cylinder with greater than average number of 2939163Ssam * unused data blocks. 2949163Ssam */ 2959163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 2969163Ssam for (cg = fs->fs_cgrotor + 1; cg < fs->fs_ncg; cg++) 2979163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 2989163Ssam fs->fs_cgrotor = cg; 2999163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3009163Ssam } 3019163Ssam for (cg = 0; cg <= fs->fs_cgrotor; cg++) 3029163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3039163Ssam fs->fs_cgrotor = cg; 3049163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3059163Ssam } 3069163Ssam return (NULL); 3079163Ssam } 3089163Ssam /* 3099163Ssam * One or more previous blocks have been laid out. If less 3109163Ssam * than fs_maxcontig previous blocks are contiguous, the 3119163Ssam * next block is requested contiguously, otherwise it is 3129163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 3139163Ssam */ 3149163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 3159163Ssam if (indx > fs->fs_maxcontig && 3169163Ssam bap[indx - fs->fs_maxcontig] + fs->fs_frag * fs->fs_maxcontig 3179163Ssam != nextblk) 3189163Ssam return (nextblk); 3199163Ssam if (fs->fs_rotdelay != 0) 3209163Ssam /* 3219163Ssam * Here we convert ms of delay to frags as: 3229163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 3239163Ssam * ((sect/frag) * (ms/sec)) 3249163Ssam * then round up to the next block. 3259163Ssam */ 3269163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 3279163Ssam (NSPF(fs) * 1000), fs->fs_frag); 3289163Ssam return (nextblk); 3294651Smckusic } 3304651Smckusic 3315375Smckusic /* 3325375Smckusic * Implement the cylinder overflow algorithm. 3335375Smckusic * 3345375Smckusic * The policy implemented by this algorithm is: 3355375Smckusic * 1) allocate the block in its requested cylinder group. 3365375Smckusic * 2) quadradically rehash on the cylinder group number. 3375375Smckusic * 3) brute force search for a free block. 3385375Smckusic */ 3395212Smckusic /*VARARGS5*/ 3405212Smckusic u_long 3415965Smckusic hashalloc(ip, cg, pref, size, allocator) 3425965Smckusic struct inode *ip; 3434359Smckusick int cg; 3444359Smckusick long pref; 3454359Smckusick int size; /* size for data blocks, mode for inodes */ 3465212Smckusic u_long (*allocator)(); 3474359Smckusick { 3485965Smckusic register struct fs *fs; 3494359Smckusick long result; 3504359Smckusick int i, icg = cg; 3514359Smckusick 3525965Smckusic fs = ip->i_fs; 3534359Smckusick /* 3544359Smckusick * 1: preferred cylinder group 3554359Smckusick */ 3565965Smckusic result = (*allocator)(ip, cg, pref, size); 3574359Smckusick if (result) 3584359Smckusick return (result); 3594359Smckusick /* 3604359Smckusick * 2: quadratic rehash 3614359Smckusick */ 3624359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 3634359Smckusick cg += i; 3644359Smckusick if (cg >= fs->fs_ncg) 3654359Smckusick cg -= fs->fs_ncg; 3665965Smckusic result = (*allocator)(ip, cg, 0, size); 3674359Smckusick if (result) 3684359Smckusick return (result); 3694359Smckusick } 3704359Smckusick /* 3714359Smckusick * 3: brute force search 3724359Smckusick */ 3734359Smckusick cg = icg; 3744359Smckusick for (i = 0; i < fs->fs_ncg; i++) { 3755965Smckusic result = (*allocator)(ip, cg, 0, size); 3764359Smckusick if (result) 3774359Smckusick return (result); 3784359Smckusick cg++; 3794359Smckusick if (cg == fs->fs_ncg) 3804359Smckusick cg = 0; 3814359Smckusick } 3826294Smckusick return (NULL); 3834359Smckusick } 3844359Smckusick 3855375Smckusic /* 3865375Smckusic * Determine whether a fragment can be extended. 3875375Smckusic * 3885375Smckusic * Check to see if the necessary fragments are available, and 3895375Smckusic * if they are, allocate them. 3905375Smckusic */ 3914359Smckusick daddr_t 3925965Smckusic fragextend(ip, cg, bprev, osize, nsize) 3935965Smckusic struct inode *ip; 3944426Smckusic int cg; 3954463Smckusic long bprev; 3964426Smckusic int osize, nsize; 3974426Smckusic { 3985965Smckusic register struct fs *fs; 3994463Smckusic register struct buf *bp; 4004463Smckusic register struct cg *cgp; 4014463Smckusic long bno; 4024463Smckusic int frags, bbase; 4034426Smckusic int i; 4044426Smckusic 4055965Smckusic fs = ip->i_fs; 4066531Smckusick if (fs->fs_cs(fs, cg).cs_nffree < nsize - osize) 4076531Smckusick return (NULL); 4085960Smckusic frags = numfrags(fs, nsize); 4095960Smckusic bbase = fragoff(fs, bprev); 4105322Smckusic if (bbase > (bprev + frags - 1) % fs->fs_frag) { 4114463Smckusic /* cannot extend across a block boundry */ 4126294Smckusick return (NULL); 4134463Smckusic } 4148617Sroot bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_bsize); 4156531Smckusick cgp = bp->b_un.b_cg; 4166531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 4175960Smckusic brelse(bp); 4186294Smckusick return (NULL); 4195960Smckusic } 4208105Sroot cgp->cg_time = time.tv_sec; 4215377Smckusic bno = dtogd(fs, bprev); 4225960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 4235361Smckusic if (isclr(cgp->cg_free, bno + i)) { 4245361Smckusic brelse(bp); 4256294Smckusick return (NULL); 4265361Smckusic } 4275361Smckusic /* 4285361Smckusic * the current fragment can be extended 4295361Smckusic * deduct the count on fragment being extended into 4305361Smckusic * increase the count on the remaining fragment (if any) 4315361Smckusic * allocate the extended piece 4325361Smckusic */ 4335361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 4344463Smckusic if (isclr(cgp->cg_free, bno + i)) 4354463Smckusic break; 4365960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 4375361Smckusic if (i != frags) 4385361Smckusic cgp->cg_frsum[i - frags]++; 4395960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 4405361Smckusic clrbit(cgp->cg_free, bno + i); 4415361Smckusic cgp->cg_cs.cs_nffree--; 4425361Smckusic fs->fs_cstotal.cs_nffree--; 4435361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 4444463Smckusic } 4455361Smckusic fs->fs_fmod++; 4465361Smckusic bdwrite(bp); 4475361Smckusic return (bprev); 4484426Smckusic } 4494426Smckusic 4505375Smckusic /* 4515375Smckusic * Determine whether a block can be allocated. 4525375Smckusic * 4535375Smckusic * Check to see if a block of the apprpriate size is available, 4545375Smckusic * and if it is, allocate it. 4555375Smckusic */ 4569163Ssam daddr_t 4575965Smckusic alloccg(ip, cg, bpref, size) 4585965Smckusic struct inode *ip; 4594359Smckusick int cg; 4604359Smckusick daddr_t bpref; 4614359Smckusick int size; 4624359Smckusick { 4635965Smckusic register struct fs *fs; 4644463Smckusic register struct buf *bp; 4654463Smckusic register struct cg *cgp; 4664463Smckusic int bno, frags; 4674463Smckusic int allocsiz; 4684463Smckusic register int i; 4694359Smckusick 4705965Smckusic fs = ip->i_fs; 4715322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 4726294Smckusick return (NULL); 4738617Sroot bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_bsize); 4746531Smckusick cgp = bp->b_un.b_cg; 4756531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 4765960Smckusic brelse(bp); 4776294Smckusick return (NULL); 4785960Smckusic } 4798105Sroot cgp->cg_time = time.tv_sec; 4805322Smckusic if (size == fs->fs_bsize) { 4815212Smckusic bno = alloccgblk(fs, cgp, bpref); 4824463Smckusic bdwrite(bp); 4834463Smckusic return (bno); 4844463Smckusic } 4854463Smckusic /* 4864463Smckusic * check to see if any fragments are already available 4874463Smckusic * allocsiz is the size which will be allocated, hacking 4884463Smckusic * it down to a smaller size if necessary 4894463Smckusic */ 4905960Smckusic frags = numfrags(fs, size); 4915322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 4924463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 4934463Smckusic break; 4945322Smckusic if (allocsiz == fs->fs_frag) { 4954463Smckusic /* 4964463Smckusic * no fragments were available, so a block will be 4974463Smckusic * allocated, and hacked up 4984463Smckusic */ 4994792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 5004463Smckusic brelse(bp); 5016294Smckusick return (NULL); 5024463Smckusic } 5035212Smckusic bno = alloccgblk(fs, cgp, bpref); 5045377Smckusic bpref = dtogd(fs, bno); 5055322Smckusic for (i = frags; i < fs->fs_frag; i++) 5064463Smckusic setbit(cgp->cg_free, bpref + i); 5075322Smckusic i = fs->fs_frag - frags; 5084792Smckusic cgp->cg_cs.cs_nffree += i; 5094792Smckusic fs->fs_cstotal.cs_nffree += i; 5105322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 511*9762Ssam fs->fs_fmod++; 5124463Smckusic cgp->cg_frsum[i]++; 5134463Smckusic bdwrite(bp); 5144463Smckusic return (bno); 5154463Smckusic } 5164651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 5176567Smckusic if (bno < 0) 5186294Smckusick return (NULL); 5194463Smckusic for (i = 0; i < frags; i++) 5204463Smckusic clrbit(cgp->cg_free, bno + i); 5214792Smckusic cgp->cg_cs.cs_nffree -= frags; 5224792Smckusic fs->fs_cstotal.cs_nffree -= frags; 5235322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 524*9762Ssam fs->fs_fmod++; 5254463Smckusic cgp->cg_frsum[allocsiz]--; 5264463Smckusic if (frags != allocsiz) 5274463Smckusic cgp->cg_frsum[allocsiz - frags]++; 5284463Smckusic bdwrite(bp); 5294463Smckusic return (cg * fs->fs_fpg + bno); 5304463Smckusic } 5314463Smckusic 5325375Smckusic /* 5335375Smckusic * Allocate a block in a cylinder group. 5345375Smckusic * 5355375Smckusic * This algorithm implements the following policy: 5365375Smckusic * 1) allocate the requested block. 5375375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 5385375Smckusic * 3) allocate the next available block on the block rotor for the 5395375Smckusic * specified cylinder group. 5405375Smckusic * Note that this routine only allocates fs_bsize blocks; these 5415375Smckusic * blocks may be fragmented by the routine that allocates them. 5425375Smckusic */ 5434463Smckusic daddr_t 5445212Smckusic alloccgblk(fs, cgp, bpref) 5455965Smckusic register struct fs *fs; 5464463Smckusic register struct cg *cgp; 5474463Smckusic daddr_t bpref; 5484463Smckusic { 5494651Smckusic daddr_t bno; 5506294Smckusick int cylno, pos, delta; 5514651Smckusic short *cylbp; 5525361Smckusic register int i; 5534463Smckusic 5544651Smckusic if (bpref == 0) { 5554651Smckusic bpref = cgp->cg_rotor; 5565361Smckusic goto norot; 5575361Smckusic } 5585361Smckusic bpref &= ~(fs->fs_frag - 1); 5595377Smckusic bpref = dtogd(fs, bpref); 5605361Smckusic /* 5615361Smckusic * if the requested block is available, use it 5625361Smckusic */ 5635361Smckusic if (isblock(fs, cgp->cg_free, bpref/fs->fs_frag)) { 5645361Smckusic bno = bpref; 5655361Smckusic goto gotit; 5665361Smckusic } 5675361Smckusic /* 5685361Smckusic * check for a block available on the same cylinder 5695361Smckusic */ 5705361Smckusic cylno = cbtocylno(fs, bpref); 5715375Smckusic if (cgp->cg_btot[cylno] == 0) 5725375Smckusic goto norot; 5735375Smckusic if (fs->fs_cpc == 0) { 5745375Smckusic /* 5755375Smckusic * block layout info is not available, so just have 5765375Smckusic * to take any block in this cylinder. 5775375Smckusic */ 5785375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 5795375Smckusic goto norot; 5805375Smckusic } 5815375Smckusic /* 5825361Smckusic * check the summary information to see if a block is 5835361Smckusic * available in the requested cylinder starting at the 5849163Ssam * requested rotational position and proceeding around. 5855361Smckusic */ 5869163Ssam cylbp = cgp->cg_b[cylno]; 5879163Ssam pos = cbtorpos(fs, bpref); 5885361Smckusic for (i = pos; i < NRPOS; i++) 5895361Smckusic if (cylbp[i] > 0) 5905361Smckusic break; 5915361Smckusic if (i == NRPOS) 5925361Smckusic for (i = 0; i < pos; i++) 5935361Smckusic if (cylbp[i] > 0) 5945361Smckusic break; 5955361Smckusic if (cylbp[i] > 0) { 5964651Smckusic /* 5975361Smckusic * found a rotational position, now find the actual 5985361Smckusic * block. A panic if none is actually there. 5994651Smckusic */ 6005361Smckusic pos = cylno % fs->fs_cpc; 6015361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 6026716Smckusick if (fs->fs_postbl[pos][i] == -1) { 6036716Smckusick printf("pos = %d, i = %d, fs = %s\n", 6046716Smckusick pos, i, fs->fs_fsmnt); 6055361Smckusic panic("alloccgblk: cyl groups corrupted"); 6066716Smckusick } 6076294Smckusick for (i = fs->fs_postbl[pos][i];; ) { 6085361Smckusic if (isblock(fs, cgp->cg_free, bno + i)) { 6095361Smckusic bno = (bno + i) * fs->fs_frag; 6105361Smckusic goto gotit; 6115361Smckusic } 6126294Smckusick delta = fs->fs_rotbl[i]; 6136294Smckusick if (delta <= 0 || delta > MAXBPC - i) 6144651Smckusic break; 6156294Smckusick i += delta; 6164651Smckusic } 6176716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 6185361Smckusic panic("alloccgblk: can't find blk in cyl"); 6194359Smckusick } 6205361Smckusic norot: 6215361Smckusic /* 6225361Smckusic * no blocks in the requested cylinder, so take next 6235361Smckusic * available one in this cylinder group. 6245361Smckusic */ 6258628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 6266567Smckusic if (bno < 0) 6276294Smckusick return (NULL); 6284651Smckusic cgp->cg_rotor = bno; 6294359Smckusick gotit: 6308769Sroot clrblock(fs, cgp->cg_free, (long)(bno/fs->fs_frag)); 6314792Smckusic cgp->cg_cs.cs_nbfree--; 6324792Smckusic fs->fs_cstotal.cs_nbfree--; 6335322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 6345375Smckusic cylno = cbtocylno(fs, bno); 6355375Smckusic cgp->cg_b[cylno][cbtorpos(fs, bno)]--; 6365375Smckusic cgp->cg_btot[cylno]--; 6374359Smckusick fs->fs_fmod++; 6384651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 6394359Smckusick } 6404359Smckusick 6415375Smckusic /* 6425375Smckusic * Determine whether an inode can be allocated. 6435375Smckusic * 6445375Smckusic * Check to see if an inode is available, and if it is, 6455375Smckusic * allocate it using the following policy: 6465375Smckusic * 1) allocate the requested inode. 6475375Smckusic * 2) allocate the next available inode after the requested 6485375Smckusic * inode in the specified cylinder group. 6495375Smckusic */ 6509163Ssam ino_t 6515965Smckusic ialloccg(ip, cg, ipref, mode) 6525965Smckusic struct inode *ip; 6534359Smckusick int cg; 6544359Smckusick daddr_t ipref; 6554359Smckusick int mode; 6564359Smckusick { 6575965Smckusic register struct fs *fs; 6584463Smckusic register struct buf *bp; 6594463Smckusic register struct cg *cgp; 6604359Smckusick int i; 6614359Smckusick 6625965Smckusic fs = ip->i_fs; 6635322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 6646294Smckusick return (NULL); 6658617Sroot bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_bsize); 6666531Smckusick cgp = bp->b_un.b_cg; 6676531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 6685960Smckusic brelse(bp); 6696294Smckusick return (NULL); 6705960Smckusic } 6718105Sroot cgp->cg_time = time.tv_sec; 6724359Smckusick if (ipref) { 6734359Smckusick ipref %= fs->fs_ipg; 6744359Smckusick if (isclr(cgp->cg_iused, ipref)) 6754359Smckusick goto gotit; 6764359Smckusick } else 6774359Smckusick ipref = cgp->cg_irotor; 6784359Smckusick for (i = 0; i < fs->fs_ipg; i++) { 6794359Smckusick ipref++; 6804359Smckusick if (ipref >= fs->fs_ipg) 6814359Smckusick ipref = 0; 6824359Smckusick if (isclr(cgp->cg_iused, ipref)) { 6834359Smckusick cgp->cg_irotor = ipref; 6844359Smckusick goto gotit; 6854359Smckusick } 6864359Smckusick } 6874359Smckusick brelse(bp); 6886294Smckusick return (NULL); 6894359Smckusick gotit: 6904359Smckusick setbit(cgp->cg_iused, ipref); 6914792Smckusic cgp->cg_cs.cs_nifree--; 6924792Smckusic fs->fs_cstotal.cs_nifree--; 6935322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 6944359Smckusick fs->fs_fmod++; 6954359Smckusick if ((mode & IFMT) == IFDIR) { 6964792Smckusic cgp->cg_cs.cs_ndir++; 6974792Smckusic fs->fs_cstotal.cs_ndir++; 6985322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 6994359Smckusick } 7004359Smckusick bdwrite(bp); 7014359Smckusick return (cg * fs->fs_ipg + ipref); 7024359Smckusick } 7034359Smckusick 7045375Smckusic /* 7055375Smckusic * Free a block or fragment. 7065375Smckusic * 7075375Smckusic * The specified block or fragment is placed back in the 7085375Smckusic * free map. If a fragment is deallocated, a possible 7095375Smckusic * block reassembly is checked. 7105375Smckusic */ 7119163Ssam free(ip, bno, size) 7125965Smckusic register struct inode *ip; 7134359Smckusick daddr_t bno; 7145212Smckusic off_t size; 7154359Smckusick { 7164359Smckusick register struct fs *fs; 7174359Smckusick register struct cg *cgp; 7184359Smckusick register struct buf *bp; 7194463Smckusic int cg, blk, frags, bbase; 7204463Smckusic register int i; 7214359Smckusick 7225965Smckusic fs = ip->i_fs; 7236716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 7246716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 7256716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 7264426Smckusic panic("free: bad size"); 7276716Smckusick } 7285377Smckusic cg = dtog(fs, bno); 7296567Smckusic if (badblock(fs, bno)) { 7306567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 7314359Smckusick return; 7326567Smckusic } 7338617Sroot bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_bsize); 7346531Smckusick cgp = bp->b_un.b_cg; 7356531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 7365960Smckusic brelse(bp); 7374359Smckusick return; 7385960Smckusic } 7398105Sroot cgp->cg_time = time.tv_sec; 7405377Smckusic bno = dtogd(fs, bno); 7415322Smckusic if (size == fs->fs_bsize) { 7426567Smckusic if (isblock(fs, cgp->cg_free, bno/fs->fs_frag)) { 7436716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 7446716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 7454426Smckusic panic("free: freeing free block"); 7466567Smckusic } 7475322Smckusic setblock(fs, cgp->cg_free, bno/fs->fs_frag); 7484792Smckusic cgp->cg_cs.cs_nbfree++; 7494792Smckusic fs->fs_cstotal.cs_nbfree++; 7505322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 7515375Smckusic i = cbtocylno(fs, bno); 7525375Smckusic cgp->cg_b[i][cbtorpos(fs, bno)]++; 7535375Smckusic cgp->cg_btot[i]++; 7544426Smckusic } else { 7555322Smckusic bbase = bno - (bno % fs->fs_frag); 7564463Smckusic /* 7574463Smckusic * decrement the counts associated with the old frags 7584463Smckusic */ 7596294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 7605322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 7614463Smckusic /* 7624463Smckusic * deallocate the fragment 7634463Smckusic */ 7645960Smckusic frags = numfrags(fs, size); 7654463Smckusic for (i = 0; i < frags; i++) { 7666716Smckusick if (isset(cgp->cg_free, bno + i)) { 7676716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 7686716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 7694426Smckusic panic("free: freeing free frag"); 7706716Smckusick } 7714426Smckusic setbit(cgp->cg_free, bno + i); 7724426Smckusic } 7736294Smckusick cgp->cg_cs.cs_nffree += i; 7746294Smckusick fs->fs_cstotal.cs_nffree += i; 7756294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 7764463Smckusic /* 7774463Smckusic * add back in counts associated with the new frags 7784463Smckusic */ 7796294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 7805322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 7814463Smckusic /* 7824463Smckusic * if a complete block has been reassembled, account for it 7834463Smckusic */ 7845322Smckusic if (isblock(fs, cgp->cg_free, bbase / fs->fs_frag)) { 7855322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 7865322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 7875322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 7884792Smckusic cgp->cg_cs.cs_nbfree++; 7894792Smckusic fs->fs_cstotal.cs_nbfree++; 7905322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 7915375Smckusic i = cbtocylno(fs, bbase); 7925375Smckusic cgp->cg_b[i][cbtorpos(fs, bbase)]++; 7935375Smckusic cgp->cg_btot[i]++; 7944426Smckusic } 7954426Smckusic } 7964359Smckusick fs->fs_fmod++; 7974359Smckusick bdwrite(bp); 7984359Smckusick } 7994359Smckusick 8005375Smckusic /* 8015375Smckusic * Free an inode. 8025375Smckusic * 8035375Smckusic * The specified inode is placed back in the free map. 8045375Smckusic */ 8055965Smckusic ifree(ip, ino, mode) 8065965Smckusic struct inode *ip; 8074359Smckusick ino_t ino; 8084359Smckusick int mode; 8094359Smckusick { 8104359Smckusick register struct fs *fs; 8114359Smckusick register struct cg *cgp; 8124359Smckusick register struct buf *bp; 8134359Smckusick int cg; 8144359Smckusick 8155965Smckusic fs = ip->i_fs; 8166716Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 8176716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 8186716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 8194359Smckusick panic("ifree: range"); 8206716Smckusick } 8215377Smckusic cg = itog(fs, ino); 8228617Sroot bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_bsize); 8236531Smckusick cgp = bp->b_un.b_cg; 8246531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 8255960Smckusic brelse(bp); 8264359Smckusick return; 8275960Smckusic } 8288105Sroot cgp->cg_time = time.tv_sec; 8294359Smckusick ino %= fs->fs_ipg; 8306716Smckusick if (isclr(cgp->cg_iused, ino)) { 8316716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 8326716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 8334359Smckusick panic("ifree: freeing free inode"); 8346716Smckusick } 8354359Smckusick clrbit(cgp->cg_iused, ino); 8364792Smckusic cgp->cg_cs.cs_nifree++; 8374792Smckusic fs->fs_cstotal.cs_nifree++; 8385322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 8394359Smckusick if ((mode & IFMT) == IFDIR) { 8404792Smckusic cgp->cg_cs.cs_ndir--; 8414792Smckusic fs->fs_cstotal.cs_ndir--; 8425322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 8434359Smckusick } 8444359Smckusick fs->fs_fmod++; 8454359Smckusick bdwrite(bp); 8464359Smckusick } 8474359Smckusick 8484463Smckusic /* 8495375Smckusic * Find a block of the specified size in the specified cylinder group. 8505375Smckusic * 8514651Smckusic * It is a panic if a request is made to find a block if none are 8524651Smckusic * available. 8534651Smckusic */ 8544651Smckusic daddr_t 8554651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 8564651Smckusic register struct fs *fs; 8574651Smckusic register struct cg *cgp; 8584651Smckusic daddr_t bpref; 8594651Smckusic int allocsiz; 8604651Smckusic { 8614651Smckusic daddr_t bno; 8624651Smckusic int start, len, loc, i; 8634651Smckusic int blk, field, subfield, pos; 8644651Smckusic 8654651Smckusic /* 8664651Smckusic * find the fragment by searching through the free block 8674651Smckusic * map for an appropriate bit pattern 8684651Smckusic */ 8694651Smckusic if (bpref) 8705377Smckusic start = dtogd(fs, bpref) / NBBY; 8714651Smckusic else 8724651Smckusic start = cgp->cg_frotor / NBBY; 8735398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 8745322Smckusic loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag], 8756292Smckusick 1 << (allocsiz - 1 + (fs->fs_frag % NBBY))); 8764651Smckusic if (loc == 0) { 8776531Smckusick len = start + 1; 8786531Smckusick start = 0; 8795322Smckusic loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag], 8806292Smckusick 1 << (allocsiz - 1 + (fs->fs_frag % NBBY))); 881*9762Ssam if (loc == 0) 8826531Smckusick return (-1); 8834651Smckusic } 8844651Smckusic bno = (start + len - loc) * NBBY; 8854651Smckusic cgp->cg_frotor = bno; 8864651Smckusic /* 8874651Smckusic * found the byte in the map 8884651Smckusic * sift through the bits to find the selected frag 8894651Smckusic */ 8906294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 8916294Smckusick blk = blkmap(fs, cgp->cg_free, bno); 8924651Smckusic blk <<= 1; 8934651Smckusic field = around[allocsiz]; 8944651Smckusic subfield = inside[allocsiz]; 8955322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 8966294Smckusick if ((blk & field) == subfield) 8976294Smckusick return (bno + pos); 8984651Smckusic field <<= 1; 8994651Smckusic subfield <<= 1; 9004651Smckusic } 9014651Smckusic } 9026716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 9034651Smckusic panic("alloccg: block not in map"); 9046531Smckusick return (-1); 9054651Smckusic } 9064651Smckusic 9074651Smckusic /* 9085375Smckusic * Fserr prints the name of a file system with an error diagnostic. 9095375Smckusic * 9105375Smckusic * The form of the error message is: 9114359Smckusick * fs: error message 9124359Smckusick */ 9134359Smckusick fserr(fs, cp) 9144359Smckusick struct fs *fs; 9154359Smckusick char *cp; 9164359Smckusick { 9174359Smckusick 9184359Smckusick printf("%s: %s\n", fs->fs_fsmnt, cp); 9194359Smckusick } 920