1*10847Ssam /* lfs_alloc.c 2.22 83/02/10 */ 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 372*10847Ssam * Note that we start at i == 2, since 0 was checked initially, 373*10847Ssam * and 1 is always checked in the quadratic rehash. 3744359Smckusick */ 3754359Smckusick cg = icg; 376*10847Ssam for (i = 2; i < fs->fs_ncg; i++) { 3775965Smckusic result = (*allocator)(ip, cg, 0, size); 3784359Smckusick if (result) 3794359Smckusick return (result); 3804359Smckusick cg++; 3814359Smckusick if (cg == fs->fs_ncg) 3824359Smckusick cg = 0; 3834359Smckusick } 3846294Smckusick return (NULL); 3854359Smckusick } 3864359Smckusick 3875375Smckusic /* 3885375Smckusic * Determine whether a fragment can be extended. 3895375Smckusic * 3905375Smckusic * Check to see if the necessary fragments are available, and 3915375Smckusic * if they are, allocate them. 3925375Smckusic */ 3934359Smckusick daddr_t 3945965Smckusic fragextend(ip, cg, bprev, osize, nsize) 3955965Smckusic struct inode *ip; 3964426Smckusic int cg; 3974463Smckusic long bprev; 3984426Smckusic int osize, nsize; 3994426Smckusic { 4005965Smckusic register struct fs *fs; 4014463Smckusic register struct buf *bp; 4024463Smckusic register struct cg *cgp; 4034463Smckusic long bno; 4044463Smckusic int frags, bbase; 4054426Smckusic int i; 4064426Smckusic 4075965Smckusic fs = ip->i_fs; 4086531Smckusick if (fs->fs_cs(fs, cg).cs_nffree < nsize - osize) 4096531Smckusick return (NULL); 4105960Smckusic frags = numfrags(fs, nsize); 4115960Smckusic bbase = fragoff(fs, bprev); 4125322Smckusic if (bbase > (bprev + frags - 1) % fs->fs_frag) { 4134463Smckusic /* cannot extend across a block boundry */ 4146294Smckusick return (NULL); 4154463Smckusic } 41610278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 4176531Smckusick cgp = bp->b_un.b_cg; 4186531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 4195960Smckusic brelse(bp); 4206294Smckusick return (NULL); 4215960Smckusic } 4228105Sroot cgp->cg_time = time.tv_sec; 4235377Smckusic bno = dtogd(fs, bprev); 4245960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 4255361Smckusic if (isclr(cgp->cg_free, bno + i)) { 4265361Smckusic brelse(bp); 4276294Smckusick return (NULL); 4285361Smckusic } 4295361Smckusic /* 4305361Smckusic * the current fragment can be extended 4315361Smckusic * deduct the count on fragment being extended into 4325361Smckusic * increase the count on the remaining fragment (if any) 4335361Smckusic * allocate the extended piece 4345361Smckusic */ 4355361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 4364463Smckusic if (isclr(cgp->cg_free, bno + i)) 4374463Smckusic break; 4385960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 4395361Smckusic if (i != frags) 4405361Smckusic cgp->cg_frsum[i - frags]++; 4415960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 4425361Smckusic clrbit(cgp->cg_free, bno + i); 4435361Smckusic cgp->cg_cs.cs_nffree--; 4445361Smckusic fs->fs_cstotal.cs_nffree--; 4455361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 4464463Smckusic } 4475361Smckusic fs->fs_fmod++; 4485361Smckusic bdwrite(bp); 4495361Smckusic return (bprev); 4504426Smckusic } 4514426Smckusic 4525375Smckusic /* 4535375Smckusic * Determine whether a block can be allocated. 4545375Smckusic * 4555375Smckusic * Check to see if a block of the apprpriate size is available, 4565375Smckusic * and if it is, allocate it. 4575375Smckusic */ 4589163Ssam daddr_t 4595965Smckusic alloccg(ip, cg, bpref, size) 4605965Smckusic struct inode *ip; 4614359Smckusick int cg; 4624359Smckusick daddr_t bpref; 4634359Smckusick int size; 4644359Smckusick { 4655965Smckusic register struct fs *fs; 4664463Smckusic register struct buf *bp; 4674463Smckusic register struct cg *cgp; 4684463Smckusic int bno, frags; 4694463Smckusic int allocsiz; 4704463Smckusic register int i; 4714359Smckusick 4725965Smckusic fs = ip->i_fs; 4735322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 4746294Smckusick return (NULL); 47510278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 4766531Smckusick cgp = bp->b_un.b_cg; 4776531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 4785960Smckusic brelse(bp); 4796294Smckusick return (NULL); 4805960Smckusic } 48110278Smckusick if (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize) 48210278Smckusick return (NULL); 4838105Sroot cgp->cg_time = time.tv_sec; 4845322Smckusic if (size == fs->fs_bsize) { 4855212Smckusic bno = alloccgblk(fs, cgp, bpref); 4864463Smckusic bdwrite(bp); 4874463Smckusic return (bno); 4884463Smckusic } 4894463Smckusic /* 4904463Smckusic * check to see if any fragments are already available 4914463Smckusic * allocsiz is the size which will be allocated, hacking 4924463Smckusic * it down to a smaller size if necessary 4934463Smckusic */ 4945960Smckusic frags = numfrags(fs, size); 4955322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 4964463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 4974463Smckusic break; 4985322Smckusic if (allocsiz == fs->fs_frag) { 4994463Smckusic /* 5004463Smckusic * no fragments were available, so a block will be 5014463Smckusic * allocated, and hacked up 5024463Smckusic */ 5034792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 5044463Smckusic brelse(bp); 5056294Smckusick return (NULL); 5064463Smckusic } 5075212Smckusic bno = alloccgblk(fs, cgp, bpref); 5085377Smckusic bpref = dtogd(fs, bno); 5095322Smckusic for (i = frags; i < fs->fs_frag; i++) 5104463Smckusic setbit(cgp->cg_free, bpref + i); 5115322Smckusic i = fs->fs_frag - frags; 5124792Smckusic cgp->cg_cs.cs_nffree += i; 5134792Smckusic fs->fs_cstotal.cs_nffree += i; 5145322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 5159762Ssam fs->fs_fmod++; 5164463Smckusic cgp->cg_frsum[i]++; 5174463Smckusic bdwrite(bp); 5184463Smckusic return (bno); 5194463Smckusic } 5204651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 5216567Smckusic if (bno < 0) 5226294Smckusick return (NULL); 5234463Smckusic for (i = 0; i < frags; i++) 5244463Smckusic clrbit(cgp->cg_free, bno + i); 5254792Smckusic cgp->cg_cs.cs_nffree -= frags; 5264792Smckusic fs->fs_cstotal.cs_nffree -= frags; 5275322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 5289762Ssam fs->fs_fmod++; 5294463Smckusic cgp->cg_frsum[allocsiz]--; 5304463Smckusic if (frags != allocsiz) 5314463Smckusic cgp->cg_frsum[allocsiz - frags]++; 5324463Smckusic bdwrite(bp); 5334463Smckusic return (cg * fs->fs_fpg + bno); 5344463Smckusic } 5354463Smckusic 5365375Smckusic /* 5375375Smckusic * Allocate a block in a cylinder group. 5385375Smckusic * 5395375Smckusic * This algorithm implements the following policy: 5405375Smckusic * 1) allocate the requested block. 5415375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 5425375Smckusic * 3) allocate the next available block on the block rotor for the 5435375Smckusic * specified cylinder group. 5445375Smckusic * Note that this routine only allocates fs_bsize blocks; these 5455375Smckusic * blocks may be fragmented by the routine that allocates them. 5465375Smckusic */ 5474463Smckusic daddr_t 5485212Smckusic alloccgblk(fs, cgp, bpref) 5495965Smckusic register struct fs *fs; 5504463Smckusic register struct cg *cgp; 5514463Smckusic daddr_t bpref; 5524463Smckusic { 5534651Smckusic daddr_t bno; 5546294Smckusick int cylno, pos, delta; 5554651Smckusic short *cylbp; 5565361Smckusic register int i; 5574463Smckusic 5584651Smckusic if (bpref == 0) { 5594651Smckusic bpref = cgp->cg_rotor; 5605361Smckusic goto norot; 5615361Smckusic } 5625361Smckusic bpref &= ~(fs->fs_frag - 1); 5635377Smckusic bpref = dtogd(fs, bpref); 5645361Smckusic /* 5655361Smckusic * if the requested block is available, use it 5665361Smckusic */ 5675361Smckusic if (isblock(fs, cgp->cg_free, bpref/fs->fs_frag)) { 5685361Smckusic bno = bpref; 5695361Smckusic goto gotit; 5705361Smckusic } 5715361Smckusic /* 5725361Smckusic * check for a block available on the same cylinder 5735361Smckusic */ 5745361Smckusic cylno = cbtocylno(fs, bpref); 5755375Smckusic if (cgp->cg_btot[cylno] == 0) 5765375Smckusic goto norot; 5775375Smckusic if (fs->fs_cpc == 0) { 5785375Smckusic /* 5795375Smckusic * block layout info is not available, so just have 5805375Smckusic * to take any block in this cylinder. 5815375Smckusic */ 5825375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 5835375Smckusic goto norot; 5845375Smckusic } 5855375Smckusic /* 5865361Smckusic * check the summary information to see if a block is 5875361Smckusic * available in the requested cylinder starting at the 5889163Ssam * requested rotational position and proceeding around. 5895361Smckusic */ 5909163Ssam cylbp = cgp->cg_b[cylno]; 5919163Ssam pos = cbtorpos(fs, bpref); 5925361Smckusic for (i = pos; i < NRPOS; i++) 5935361Smckusic if (cylbp[i] > 0) 5945361Smckusic break; 5955361Smckusic if (i == NRPOS) 5965361Smckusic for (i = 0; i < pos; i++) 5975361Smckusic if (cylbp[i] > 0) 5985361Smckusic break; 5995361Smckusic if (cylbp[i] > 0) { 6004651Smckusic /* 6015361Smckusic * found a rotational position, now find the actual 6025361Smckusic * block. A panic if none is actually there. 6034651Smckusic */ 6045361Smckusic pos = cylno % fs->fs_cpc; 6055361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 6066716Smckusick if (fs->fs_postbl[pos][i] == -1) { 6076716Smckusick printf("pos = %d, i = %d, fs = %s\n", 6086716Smckusick pos, i, fs->fs_fsmnt); 6095361Smckusic panic("alloccgblk: cyl groups corrupted"); 6106716Smckusick } 6116294Smckusick for (i = fs->fs_postbl[pos][i];; ) { 6125361Smckusic if (isblock(fs, cgp->cg_free, bno + i)) { 6135361Smckusic bno = (bno + i) * fs->fs_frag; 6145361Smckusic goto gotit; 6155361Smckusic } 6166294Smckusick delta = fs->fs_rotbl[i]; 6176294Smckusick if (delta <= 0 || delta > MAXBPC - i) 6184651Smckusic break; 6196294Smckusick i += delta; 6204651Smckusic } 6216716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 6225361Smckusic panic("alloccgblk: can't find blk in cyl"); 6234359Smckusick } 6245361Smckusic norot: 6255361Smckusic /* 6265361Smckusic * no blocks in the requested cylinder, so take next 6275361Smckusic * available one in this cylinder group. 6285361Smckusic */ 6298628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 6306567Smckusic if (bno < 0) 6316294Smckusick return (NULL); 6324651Smckusic cgp->cg_rotor = bno; 6334359Smckusick gotit: 6348769Sroot clrblock(fs, cgp->cg_free, (long)(bno/fs->fs_frag)); 6354792Smckusic cgp->cg_cs.cs_nbfree--; 6364792Smckusic fs->fs_cstotal.cs_nbfree--; 6375322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 6385375Smckusic cylno = cbtocylno(fs, bno); 6395375Smckusic cgp->cg_b[cylno][cbtorpos(fs, bno)]--; 6405375Smckusic cgp->cg_btot[cylno]--; 6414359Smckusick fs->fs_fmod++; 6424651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 6434359Smckusick } 6444359Smckusick 6455375Smckusic /* 6465375Smckusic * Determine whether an inode can be allocated. 6475375Smckusic * 6485375Smckusic * Check to see if an inode is available, and if it is, 6495375Smckusic * allocate it using the following policy: 6505375Smckusic * 1) allocate the requested inode. 6515375Smckusic * 2) allocate the next available inode after the requested 6525375Smckusic * inode in the specified cylinder group. 6535375Smckusic */ 6549163Ssam ino_t 6555965Smckusic ialloccg(ip, cg, ipref, mode) 6565965Smckusic struct inode *ip; 6574359Smckusick int cg; 6584359Smckusick daddr_t ipref; 6594359Smckusick int mode; 6604359Smckusick { 6615965Smckusic register struct fs *fs; 6624463Smckusic register struct buf *bp; 6634463Smckusic register struct cg *cgp; 6644359Smckusick int i; 6654359Smckusick 6665965Smckusic fs = ip->i_fs; 6675322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 6686294Smckusick return (NULL); 66910278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 6706531Smckusick cgp = bp->b_un.b_cg; 6716531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 6725960Smckusic brelse(bp); 6736294Smckusick return (NULL); 6745960Smckusic } 67510278Smckusick if (cgp->cg_cs.cs_nifree == 0) 67610278Smckusick return (NULL); 6778105Sroot cgp->cg_time = time.tv_sec; 6784359Smckusick if (ipref) { 6794359Smckusick ipref %= fs->fs_ipg; 6804359Smckusick if (isclr(cgp->cg_iused, ipref)) 6814359Smckusick goto gotit; 6824359Smckusick } else 6834359Smckusick ipref = cgp->cg_irotor; 6844359Smckusick for (i = 0; i < fs->fs_ipg; i++) { 6854359Smckusick ipref++; 6864359Smckusick if (ipref >= fs->fs_ipg) 6874359Smckusick ipref = 0; 6884359Smckusick if (isclr(cgp->cg_iused, ipref)) { 6894359Smckusick cgp->cg_irotor = ipref; 6904359Smckusick goto gotit; 6914359Smckusick } 6924359Smckusick } 6934359Smckusick brelse(bp); 6946294Smckusick return (NULL); 6954359Smckusick gotit: 6964359Smckusick setbit(cgp->cg_iused, ipref); 6974792Smckusic cgp->cg_cs.cs_nifree--; 6984792Smckusic fs->fs_cstotal.cs_nifree--; 6995322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 7004359Smckusick fs->fs_fmod++; 7014359Smckusick if ((mode & IFMT) == IFDIR) { 7024792Smckusic cgp->cg_cs.cs_ndir++; 7034792Smckusic fs->fs_cstotal.cs_ndir++; 7045322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 7054359Smckusick } 7064359Smckusick bdwrite(bp); 7074359Smckusick return (cg * fs->fs_ipg + ipref); 7084359Smckusick } 7094359Smckusick 7105375Smckusic /* 7115375Smckusic * Free a block or fragment. 7125375Smckusic * 7135375Smckusic * The specified block or fragment is placed back in the 7145375Smckusic * free map. If a fragment is deallocated, a possible 7155375Smckusic * block reassembly is checked. 7165375Smckusic */ 7179163Ssam free(ip, bno, size) 7185965Smckusic register struct inode *ip; 7194359Smckusick daddr_t bno; 7205212Smckusic off_t size; 7214359Smckusick { 7224359Smckusick register struct fs *fs; 7234359Smckusick register struct cg *cgp; 7244359Smckusick register struct buf *bp; 7254463Smckusic int cg, blk, frags, bbase; 7264463Smckusic register int i; 7274359Smckusick 7285965Smckusic fs = ip->i_fs; 7296716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 7306716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 7316716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 7324426Smckusic panic("free: bad size"); 7336716Smckusick } 7345377Smckusic cg = dtog(fs, bno); 7356567Smckusic if (badblock(fs, bno)) { 7366567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 7374359Smckusick return; 7386567Smckusic } 73910278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 7406531Smckusick cgp = bp->b_un.b_cg; 7416531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 7425960Smckusic brelse(bp); 7434359Smckusick return; 7445960Smckusic } 7458105Sroot cgp->cg_time = time.tv_sec; 7465377Smckusic bno = dtogd(fs, bno); 7475322Smckusic if (size == fs->fs_bsize) { 7486567Smckusic if (isblock(fs, cgp->cg_free, bno/fs->fs_frag)) { 7496716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 7506716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 7514426Smckusic panic("free: freeing free block"); 7526567Smckusic } 7535322Smckusic setblock(fs, cgp->cg_free, bno/fs->fs_frag); 7544792Smckusic cgp->cg_cs.cs_nbfree++; 7554792Smckusic fs->fs_cstotal.cs_nbfree++; 7565322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 7575375Smckusic i = cbtocylno(fs, bno); 7585375Smckusic cgp->cg_b[i][cbtorpos(fs, bno)]++; 7595375Smckusic cgp->cg_btot[i]++; 7604426Smckusic } else { 7615322Smckusic bbase = bno - (bno % fs->fs_frag); 7624463Smckusic /* 7634463Smckusic * decrement the counts associated with the old frags 7644463Smckusic */ 7656294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 7665322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 7674463Smckusic /* 7684463Smckusic * deallocate the fragment 7694463Smckusic */ 7705960Smckusic frags = numfrags(fs, size); 7714463Smckusic for (i = 0; i < frags; i++) { 7726716Smckusick if (isset(cgp->cg_free, bno + i)) { 7736716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 7746716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 7754426Smckusic panic("free: freeing free frag"); 7766716Smckusick } 7774426Smckusic setbit(cgp->cg_free, bno + i); 7784426Smckusic } 7796294Smckusick cgp->cg_cs.cs_nffree += i; 7806294Smckusick fs->fs_cstotal.cs_nffree += i; 7816294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 7824463Smckusic /* 7834463Smckusic * add back in counts associated with the new frags 7844463Smckusic */ 7856294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 7865322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 7874463Smckusic /* 7884463Smckusic * if a complete block has been reassembled, account for it 7894463Smckusic */ 7905322Smckusic if (isblock(fs, cgp->cg_free, bbase / fs->fs_frag)) { 7915322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 7925322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 7935322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 7944792Smckusic cgp->cg_cs.cs_nbfree++; 7954792Smckusic fs->fs_cstotal.cs_nbfree++; 7965322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 7975375Smckusic i = cbtocylno(fs, bbase); 7985375Smckusic cgp->cg_b[i][cbtorpos(fs, bbase)]++; 7995375Smckusic cgp->cg_btot[i]++; 8004426Smckusic } 8014426Smckusic } 8024359Smckusick fs->fs_fmod++; 8034359Smckusick bdwrite(bp); 8044359Smckusick } 8054359Smckusick 8065375Smckusic /* 8075375Smckusic * Free an inode. 8085375Smckusic * 8095375Smckusic * The specified inode is placed back in the free map. 8105375Smckusic */ 8115965Smckusic ifree(ip, ino, mode) 8125965Smckusic struct inode *ip; 8134359Smckusick ino_t ino; 8144359Smckusick int mode; 8154359Smckusick { 8164359Smckusick register struct fs *fs; 8174359Smckusick register struct cg *cgp; 8184359Smckusick register struct buf *bp; 8194359Smckusick int cg; 8204359Smckusick 8215965Smckusic fs = ip->i_fs; 8226716Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 8236716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 8246716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 8254359Smckusick panic("ifree: range"); 8266716Smckusick } 8275377Smckusic cg = itog(fs, ino); 82810278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 8296531Smckusick cgp = bp->b_un.b_cg; 8306531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 8315960Smckusic brelse(bp); 8324359Smckusick return; 8335960Smckusic } 8348105Sroot cgp->cg_time = time.tv_sec; 8354359Smckusick ino %= fs->fs_ipg; 8366716Smckusick if (isclr(cgp->cg_iused, ino)) { 8376716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 8386716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 8394359Smckusick panic("ifree: freeing free inode"); 8406716Smckusick } 8414359Smckusick clrbit(cgp->cg_iused, ino); 8424792Smckusic cgp->cg_cs.cs_nifree++; 8434792Smckusic fs->fs_cstotal.cs_nifree++; 8445322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 8454359Smckusick if ((mode & IFMT) == IFDIR) { 8464792Smckusic cgp->cg_cs.cs_ndir--; 8474792Smckusic fs->fs_cstotal.cs_ndir--; 8485322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 8494359Smckusick } 8504359Smckusick fs->fs_fmod++; 8514359Smckusick bdwrite(bp); 8524359Smckusick } 8534359Smckusick 8544463Smckusic /* 8555375Smckusic * Find a block of the specified size in the specified cylinder group. 8565375Smckusic * 8574651Smckusic * It is a panic if a request is made to find a block if none are 8584651Smckusic * available. 8594651Smckusic */ 8604651Smckusic daddr_t 8614651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 8624651Smckusic register struct fs *fs; 8634651Smckusic register struct cg *cgp; 8644651Smckusic daddr_t bpref; 8654651Smckusic int allocsiz; 8664651Smckusic { 8674651Smckusic daddr_t bno; 8684651Smckusic int start, len, loc, i; 8694651Smckusic int blk, field, subfield, pos; 8704651Smckusic 8714651Smckusic /* 8724651Smckusic * find the fragment by searching through the free block 8734651Smckusic * map for an appropriate bit pattern 8744651Smckusic */ 8754651Smckusic if (bpref) 8765377Smckusic start = dtogd(fs, bpref) / NBBY; 8774651Smckusic else 8784651Smckusic start = cgp->cg_frotor / NBBY; 8795398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 8805322Smckusic loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag], 8816292Smckusick 1 << (allocsiz - 1 + (fs->fs_frag % NBBY))); 8824651Smckusic if (loc == 0) { 8836531Smckusick len = start + 1; 8846531Smckusick start = 0; 8855322Smckusic loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag], 8866292Smckusick 1 << (allocsiz - 1 + (fs->fs_frag % NBBY))); 8879762Ssam if (loc == 0) 8886531Smckusick return (-1); 8894651Smckusic } 8904651Smckusic bno = (start + len - loc) * NBBY; 8914651Smckusic cgp->cg_frotor = bno; 8924651Smckusic /* 8934651Smckusic * found the byte in the map 8944651Smckusic * sift through the bits to find the selected frag 8954651Smckusic */ 8966294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 8976294Smckusick blk = blkmap(fs, cgp->cg_free, bno); 8984651Smckusic blk <<= 1; 8994651Smckusic field = around[allocsiz]; 9004651Smckusic subfield = inside[allocsiz]; 9015322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 9026294Smckusick if ((blk & field) == subfield) 9036294Smckusick return (bno + pos); 9044651Smckusic field <<= 1; 9054651Smckusic subfield <<= 1; 9064651Smckusic } 9074651Smckusic } 9086716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 9094651Smckusic panic("alloccg: block not in map"); 9106531Smckusick return (-1); 9114651Smckusic } 9124651Smckusic 9134651Smckusic /* 9145375Smckusic * Fserr prints the name of a file system with an error diagnostic. 9155375Smckusic * 9165375Smckusic * The form of the error message is: 9174359Smckusick * fs: error message 9184359Smckusick */ 9194359Smckusick fserr(fs, cp) 9204359Smckusick struct fs *fs; 9214359Smckusick char *cp; 9224359Smckusick { 9234359Smckusick 9244359Smckusick printf("%s: %s\n", fs->fs_fsmnt, cp); 9254359Smckusick } 926