1*15120Skarels /* lfs_alloc.c 6.2 83/09/28 */ 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; 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) { 217*15120Skarels 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; 4186531Smckusick if (fs->fs_cs(fs, cg).cs_nffree < nsize - osize) 4196531Smckusick return (NULL); 4205960Smckusic frags = numfrags(fs, nsize); 4215960Smckusic bbase = fragoff(fs, bprev); 4225322Smckusic if (bbase > (bprev + frags - 1) % fs->fs_frag) { 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; 4876531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 4885960Smckusic brelse(bp); 4896294Smckusick return (NULL); 4905960Smckusic } 49110278Smckusick if (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize) 49210278Smckusick return (NULL); 4938105Sroot cgp->cg_time = time.tv_sec; 4945322Smckusic if (size == fs->fs_bsize) { 4955212Smckusic bno = alloccgblk(fs, cgp, bpref); 4964463Smckusic bdwrite(bp); 4974463Smckusic return (bno); 4984463Smckusic } 4994463Smckusic /* 5004463Smckusic * check to see if any fragments are already available 5014463Smckusic * allocsiz is the size which will be allocated, hacking 5024463Smckusic * it down to a smaller size if necessary 5034463Smckusic */ 5045960Smckusic frags = numfrags(fs, size); 5055322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 5064463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 5074463Smckusic break; 5085322Smckusic if (allocsiz == fs->fs_frag) { 5094463Smckusic /* 5104463Smckusic * no fragments were available, so a block will be 5114463Smckusic * allocated, and hacked up 5124463Smckusic */ 5134792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 5144463Smckusic brelse(bp); 5156294Smckusick return (NULL); 5164463Smckusic } 5175212Smckusic bno = alloccgblk(fs, cgp, bpref); 5185377Smckusic bpref = dtogd(fs, bno); 5195322Smckusic for (i = frags; i < fs->fs_frag; i++) 5204463Smckusic setbit(cgp->cg_free, bpref + i); 5215322Smckusic i = fs->fs_frag - frags; 5224792Smckusic cgp->cg_cs.cs_nffree += i; 5234792Smckusic fs->fs_cstotal.cs_nffree += i; 5245322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 5259762Ssam fs->fs_fmod++; 5264463Smckusic cgp->cg_frsum[i]++; 5274463Smckusic bdwrite(bp); 5284463Smckusic return (bno); 5294463Smckusic } 5304651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 5316567Smckusic if (bno < 0) 5326294Smckusick return (NULL); 5334463Smckusic for (i = 0; i < frags; i++) 5344463Smckusic clrbit(cgp->cg_free, bno + i); 5354792Smckusic cgp->cg_cs.cs_nffree -= frags; 5364792Smckusic fs->fs_cstotal.cs_nffree -= frags; 5375322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 5389762Ssam fs->fs_fmod++; 5394463Smckusic cgp->cg_frsum[allocsiz]--; 5404463Smckusic if (frags != allocsiz) 5414463Smckusic cgp->cg_frsum[allocsiz - frags]++; 5424463Smckusic bdwrite(bp); 5434463Smckusic return (cg * fs->fs_fpg + bno); 5444463Smckusic } 5454463Smckusic 5465375Smckusic /* 5475375Smckusic * Allocate a block in a cylinder group. 5485375Smckusic * 5495375Smckusic * This algorithm implements the following policy: 5505375Smckusic * 1) allocate the requested block. 5515375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 5525375Smckusic * 3) allocate the next available block on the block rotor for the 5535375Smckusic * specified cylinder group. 5545375Smckusic * Note that this routine only allocates fs_bsize blocks; these 5555375Smckusic * blocks may be fragmented by the routine that allocates them. 5565375Smckusic */ 5574463Smckusic daddr_t 5585212Smckusic alloccgblk(fs, cgp, bpref) 5595965Smckusic register struct fs *fs; 5604463Smckusic register struct cg *cgp; 5614463Smckusic daddr_t bpref; 5624463Smckusic { 5634651Smckusic daddr_t bno; 5646294Smckusick int cylno, pos, delta; 5654651Smckusic short *cylbp; 5665361Smckusic register int i; 5674463Smckusic 5684651Smckusic if (bpref == 0) { 5694651Smckusic bpref = cgp->cg_rotor; 5705361Smckusic goto norot; 5715361Smckusic } 5725361Smckusic bpref &= ~(fs->fs_frag - 1); 5735377Smckusic bpref = dtogd(fs, bpref); 5745361Smckusic /* 5755361Smckusic * if the requested block is available, use it 5765361Smckusic */ 57711638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) { 5785361Smckusic bno = bpref; 5795361Smckusic goto gotit; 5805361Smckusic } 5815361Smckusic /* 5825361Smckusic * check for a block available on the same cylinder 5835361Smckusic */ 5845361Smckusic cylno = cbtocylno(fs, bpref); 5855375Smckusic if (cgp->cg_btot[cylno] == 0) 5865375Smckusic goto norot; 5875375Smckusic if (fs->fs_cpc == 0) { 5885375Smckusic /* 5895375Smckusic * block layout info is not available, so just have 5905375Smckusic * to take any block in this cylinder. 5915375Smckusic */ 5925375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 5935375Smckusic goto norot; 5945375Smckusic } 5955375Smckusic /* 5965361Smckusic * check the summary information to see if a block is 5975361Smckusic * available in the requested cylinder starting at the 5989163Ssam * requested rotational position and proceeding around. 5995361Smckusic */ 6009163Ssam cylbp = cgp->cg_b[cylno]; 6019163Ssam pos = cbtorpos(fs, bpref); 6025361Smckusic for (i = pos; i < NRPOS; i++) 6035361Smckusic if (cylbp[i] > 0) 6045361Smckusic break; 6055361Smckusic if (i == NRPOS) 6065361Smckusic for (i = 0; i < pos; i++) 6075361Smckusic if (cylbp[i] > 0) 6085361Smckusic break; 6095361Smckusic if (cylbp[i] > 0) { 6104651Smckusic /* 6115361Smckusic * found a rotational position, now find the actual 6125361Smckusic * block. A panic if none is actually there. 6134651Smckusic */ 6145361Smckusic pos = cylno % fs->fs_cpc; 6155361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 6166716Smckusick if (fs->fs_postbl[pos][i] == -1) { 6176716Smckusick printf("pos = %d, i = %d, fs = %s\n", 6186716Smckusick pos, i, fs->fs_fsmnt); 6195361Smckusic panic("alloccgblk: cyl groups corrupted"); 6206716Smckusick } 6216294Smckusick for (i = fs->fs_postbl[pos][i];; ) { 6225361Smckusic if (isblock(fs, cgp->cg_free, bno + i)) { 62311638Ssam bno = blkstofrags(fs, (bno + i)); 6245361Smckusic goto gotit; 6255361Smckusic } 6266294Smckusick delta = fs->fs_rotbl[i]; 6276294Smckusick if (delta <= 0 || delta > MAXBPC - i) 6284651Smckusic break; 6296294Smckusick i += delta; 6304651Smckusic } 6316716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 6325361Smckusic panic("alloccgblk: can't find blk in cyl"); 6334359Smckusick } 6345361Smckusic norot: 6355361Smckusic /* 6365361Smckusic * no blocks in the requested cylinder, so take next 6375361Smckusic * available one in this cylinder group. 6385361Smckusic */ 6398628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 6406567Smckusic if (bno < 0) 6416294Smckusick return (NULL); 6424651Smckusic cgp->cg_rotor = bno; 6434359Smckusick gotit: 64411638Ssam clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno)); 6454792Smckusic cgp->cg_cs.cs_nbfree--; 6464792Smckusic fs->fs_cstotal.cs_nbfree--; 6475322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 6485375Smckusic cylno = cbtocylno(fs, bno); 6495375Smckusic cgp->cg_b[cylno][cbtorpos(fs, bno)]--; 6505375Smckusic cgp->cg_btot[cylno]--; 6514359Smckusick fs->fs_fmod++; 6524651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 6534359Smckusick } 6544359Smckusick 6555375Smckusic /* 6565375Smckusic * Determine whether an inode can be allocated. 6575375Smckusic * 6585375Smckusic * Check to see if an inode is available, and if it is, 6595375Smckusic * allocate it using the following policy: 6605375Smckusic * 1) allocate the requested inode. 6615375Smckusic * 2) allocate the next available inode after the requested 6625375Smckusic * inode in the specified cylinder group. 6635375Smckusic */ 6649163Ssam ino_t 6655965Smckusic ialloccg(ip, cg, ipref, mode) 6665965Smckusic struct inode *ip; 6674359Smckusick int cg; 6684359Smckusick daddr_t ipref; 6694359Smckusick int mode; 6704359Smckusick { 6715965Smckusic register struct fs *fs; 6724463Smckusic register struct buf *bp; 6734463Smckusic register struct cg *cgp; 6744359Smckusick int i; 6754359Smckusick 6765965Smckusic fs = ip->i_fs; 6775322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 6786294Smckusick return (NULL); 67910278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 6806531Smckusick cgp = bp->b_un.b_cg; 6816531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 6825960Smckusic brelse(bp); 6836294Smckusick return (NULL); 6845960Smckusic } 68510278Smckusick if (cgp->cg_cs.cs_nifree == 0) 68610278Smckusick return (NULL); 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; 6924359Smckusick } else 6934359Smckusick ipref = cgp->cg_irotor; 6944359Smckusick for (i = 0; i < fs->fs_ipg; i++) { 6954359Smckusick ipref++; 6964359Smckusick if (ipref >= fs->fs_ipg) 6974359Smckusick ipref = 0; 6984359Smckusick if (isclr(cgp->cg_iused, ipref)) { 6994359Smckusick cgp->cg_irotor = ipref; 7004359Smckusick goto gotit; 7014359Smckusick } 7024359Smckusick } 7034359Smckusick brelse(bp); 7046294Smckusick return (NULL); 7054359Smckusick gotit: 7064359Smckusick setbit(cgp->cg_iused, ipref); 7074792Smckusic cgp->cg_cs.cs_nifree--; 7084792Smckusic fs->fs_cstotal.cs_nifree--; 7095322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 7104359Smckusick fs->fs_fmod++; 7114359Smckusick if ((mode & IFMT) == IFDIR) { 7124792Smckusic cgp->cg_cs.cs_ndir++; 7134792Smckusic fs->fs_cstotal.cs_ndir++; 7145322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 7154359Smckusick } 7164359Smckusick bdwrite(bp); 7174359Smckusick return (cg * fs->fs_ipg + ipref); 7184359Smckusick } 7194359Smckusick 7205375Smckusic /* 7215375Smckusic * Free a block or fragment. 7225375Smckusic * 7235375Smckusic * The specified block or fragment is placed back in the 7245375Smckusic * free map. If a fragment is deallocated, a possible 7255375Smckusic * block reassembly is checked. 7265375Smckusic */ 7279163Ssam free(ip, bno, size) 7285965Smckusic register struct inode *ip; 7294359Smckusick daddr_t bno; 7305212Smckusic off_t size; 7314359Smckusick { 7324359Smckusick register struct fs *fs; 7334359Smckusick register struct cg *cgp; 7344359Smckusick register struct buf *bp; 7354463Smckusic int cg, blk, frags, bbase; 7364463Smckusic register int i; 7374359Smckusick 7385965Smckusic fs = ip->i_fs; 7396716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 7406716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 7416716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 7424426Smckusic panic("free: bad size"); 7436716Smckusick } 7445377Smckusic cg = dtog(fs, bno); 7456567Smckusic if (badblock(fs, bno)) { 7466567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 7474359Smckusick return; 7486567Smckusic } 74910278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 7506531Smckusick cgp = bp->b_un.b_cg; 7516531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 7525960Smckusic brelse(bp); 7534359Smckusick return; 7545960Smckusic } 7558105Sroot cgp->cg_time = time.tv_sec; 7565377Smckusic bno = dtogd(fs, bno); 7575322Smckusic if (size == fs->fs_bsize) { 75811638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) { 7596716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 7606716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 7614426Smckusic panic("free: freeing free block"); 7626567Smckusic } 76311638Ssam setblock(fs, cgp->cg_free, fragstoblks(fs, bno)); 7644792Smckusic cgp->cg_cs.cs_nbfree++; 7654792Smckusic fs->fs_cstotal.cs_nbfree++; 7665322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 7675375Smckusic i = cbtocylno(fs, bno); 7685375Smckusic cgp->cg_b[i][cbtorpos(fs, bno)]++; 7695375Smckusic cgp->cg_btot[i]++; 7704426Smckusic } else { 7715322Smckusic bbase = bno - (bno % fs->fs_frag); 7724463Smckusic /* 7734463Smckusic * decrement the counts associated with the old frags 7744463Smckusic */ 7756294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 7765322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 7774463Smckusic /* 7784463Smckusic * deallocate the fragment 7794463Smckusic */ 7805960Smckusic frags = numfrags(fs, size); 7814463Smckusic for (i = 0; i < frags; i++) { 7826716Smckusick if (isset(cgp->cg_free, bno + i)) { 7836716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 7846716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 7854426Smckusic panic("free: freeing free frag"); 7866716Smckusick } 7874426Smckusic setbit(cgp->cg_free, bno + i); 7884426Smckusic } 7896294Smckusick cgp->cg_cs.cs_nffree += i; 7906294Smckusick fs->fs_cstotal.cs_nffree += i; 7916294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 7924463Smckusic /* 7934463Smckusic * add back in counts associated with the new frags 7944463Smckusic */ 7956294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 7965322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 7974463Smckusic /* 7984463Smckusic * if a complete block has been reassembled, account for it 7994463Smckusic */ 80011638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) { 8015322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 8025322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 8035322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 8044792Smckusic cgp->cg_cs.cs_nbfree++; 8054792Smckusic fs->fs_cstotal.cs_nbfree++; 8065322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 8075375Smckusic i = cbtocylno(fs, bbase); 8085375Smckusic cgp->cg_b[i][cbtorpos(fs, bbase)]++; 8095375Smckusic cgp->cg_btot[i]++; 8104426Smckusic } 8114426Smckusic } 8124359Smckusick fs->fs_fmod++; 8134359Smckusick bdwrite(bp); 8144359Smckusick } 8154359Smckusick 8165375Smckusic /* 8175375Smckusic * Free an inode. 8185375Smckusic * 8195375Smckusic * The specified inode is placed back in the free map. 8205375Smckusic */ 8215965Smckusic ifree(ip, ino, mode) 8225965Smckusic struct inode *ip; 8234359Smckusick ino_t ino; 8244359Smckusick int mode; 8254359Smckusick { 8264359Smckusick register struct fs *fs; 8274359Smckusick register struct cg *cgp; 8284359Smckusick register struct buf *bp; 8294359Smckusick int cg; 8304359Smckusick 8315965Smckusic fs = ip->i_fs; 8326716Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 8336716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 8346716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 8354359Smckusick panic("ifree: range"); 8366716Smckusick } 8375377Smckusic cg = itog(fs, ino); 83810278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 8396531Smckusick cgp = bp->b_un.b_cg; 8406531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 8415960Smckusic brelse(bp); 8424359Smckusick return; 8435960Smckusic } 8448105Sroot cgp->cg_time = time.tv_sec; 8454359Smckusick ino %= fs->fs_ipg; 8466716Smckusick if (isclr(cgp->cg_iused, ino)) { 8476716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 8486716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 8494359Smckusick panic("ifree: freeing free inode"); 8506716Smckusick } 8514359Smckusick clrbit(cgp->cg_iused, ino); 8524792Smckusic cgp->cg_cs.cs_nifree++; 8534792Smckusic fs->fs_cstotal.cs_nifree++; 8545322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 8554359Smckusick if ((mode & IFMT) == IFDIR) { 8564792Smckusic cgp->cg_cs.cs_ndir--; 8574792Smckusic fs->fs_cstotal.cs_ndir--; 8585322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 8594359Smckusick } 8604359Smckusick fs->fs_fmod++; 8614359Smckusick bdwrite(bp); 8624359Smckusick } 8634359Smckusick 8644463Smckusic /* 8655375Smckusic * Find a block of the specified size in the specified cylinder group. 8665375Smckusic * 8674651Smckusic * It is a panic if a request is made to find a block if none are 8684651Smckusic * available. 8694651Smckusic */ 8704651Smckusic daddr_t 8714651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 8724651Smckusic register struct fs *fs; 8734651Smckusic register struct cg *cgp; 8744651Smckusic daddr_t bpref; 8754651Smckusic int allocsiz; 8764651Smckusic { 8774651Smckusic daddr_t bno; 8784651Smckusic int start, len, loc, i; 8794651Smckusic int blk, field, subfield, pos; 8804651Smckusic 8814651Smckusic /* 8824651Smckusic * find the fragment by searching through the free block 8834651Smckusic * map for an appropriate bit pattern 8844651Smckusic */ 8854651Smckusic if (bpref) 8865377Smckusic start = dtogd(fs, bpref) / NBBY; 8874651Smckusic else 8884651Smckusic start = cgp->cg_frotor / NBBY; 8895398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 89012755Ssam loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start], 89112755Ssam (caddr_t)fragtbl[fs->fs_frag], 89212755Ssam (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 8934651Smckusic if (loc == 0) { 8946531Smckusick len = start + 1; 8956531Smckusick start = 0; 89612755Ssam loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start], 89712755Ssam (caddr_t)fragtbl[fs->fs_frag], 89812755Ssam (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 8999762Ssam if (loc == 0) 9006531Smckusick return (-1); 9014651Smckusic } 9024651Smckusic bno = (start + len - loc) * NBBY; 9034651Smckusic cgp->cg_frotor = bno; 9044651Smckusic /* 9054651Smckusic * found the byte in the map 9064651Smckusic * sift through the bits to find the selected frag 9074651Smckusic */ 9086294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 9096294Smckusick blk = blkmap(fs, cgp->cg_free, bno); 9104651Smckusic blk <<= 1; 9114651Smckusic field = around[allocsiz]; 9124651Smckusic subfield = inside[allocsiz]; 9135322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 9146294Smckusick if ((blk & field) == subfield) 9156294Smckusick return (bno + pos); 9164651Smckusic field <<= 1; 9174651Smckusic subfield <<= 1; 9184651Smckusic } 9194651Smckusic } 9206716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 9214651Smckusic panic("alloccg: block not in map"); 9226531Smckusick return (-1); 9234651Smckusic } 9244651Smckusic 9254651Smckusic /* 9265375Smckusic * Fserr prints the name of a file system with an error diagnostic. 9275375Smckusic * 9285375Smckusic * The form of the error message is: 9294359Smckusick * fs: error message 9304359Smckusick */ 9314359Smckusick fserr(fs, cp) 9324359Smckusick struct fs *fs; 9334359Smckusick char *cp; 9344359Smckusick { 9354359Smckusick 9364359Smckusick printf("%s: %s\n", fs->fs_fsmnt, cp); 9374359Smckusick } 938