1*9163Ssam /* lfs_alloc.c 2.19 82/11/13 */ 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(); 16*9163Ssam extern ino_t ialloccg(); 17*9163Ssam 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); 77*9163Ssam bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, 78*9163Ssam (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; 141*9163Ssam bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 1424463Smckusic return (bp); 1434463Smckusic } 1444948Smckusic if (bpref >= fs->fs_size) 1454948Smckusic bpref = 0; 146*9163Ssam bno = (daddr_t)hashalloc(ip, cg, (long)bpref, nsize, 147*9163Ssam (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); 156*9163Ssam bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 1574463Smckusic brelse(obp); 158*9163Ssam 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 201*9163Ssam 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 */ 235*9163Ssam 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 } 250*9163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 2514359Smckusick } 2524359Smckusick 2534651Smckusic /* 254*9163Ssam * Select the desired position for the next block in a file. The file is 255*9163Ssam * logically divided into sections. The first section is composed of the 256*9163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 257*9163Ssam * 258*9163Ssam * If no blocks have been allocated in the first section, the policy is to 259*9163Ssam * request a block in the same cylinder group as the inode that describes 260*9163Ssam * the file. If no blocks have been allocated in any other section, the 261*9163Ssam * policy is to place the section in a cylinder group with a greater than 262*9163Ssam * average number of free blocks. An appropriate cylinder group is found 263*9163Ssam * by maintaining a rotor that sweeps the cylinder groups. When a new 264*9163Ssam * group of blocks is needed, the rotor is advanced until a cylinder group 265*9163Ssam * with greater than the average number of free blocks is found. 266*9163Ssam * 267*9163Ssam * If a section is already partially allocated, the policy is to 268*9163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 269*9163Ssam * contiguous blocks and the beginning of the next is physically separated 270*9163Ssam * so that the disk head will be in transit between them for at least 271*9163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 272*9163Ssam * schedule another I/O transfer. 2734651Smckusic */ 2745212Smckusic daddr_t 275*9163Ssam blkpref(ip, lbn, indx, bap) 276*9163Ssam struct inode *ip; 277*9163Ssam daddr_t lbn; 278*9163Ssam int indx; 279*9163Ssam daddr_t *bap; 280*9163Ssam { 2815965Smckusic register struct fs *fs; 2824651Smckusic int cg, avgbfree; 283*9163Ssam daddr_t nextblk; 2844651Smckusic 285*9163Ssam fs = ip->i_fs; 286*9163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 287*9163Ssam if (lbn < NDADDR) { 288*9163Ssam cg = itog(fs, ip->i_number); 2895322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 2904651Smckusic } 291*9163Ssam /* 292*9163Ssam * Find a cylinder with greater than average number of 293*9163Ssam * unused data blocks. 294*9163Ssam */ 295*9163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 296*9163Ssam for (cg = fs->fs_cgrotor + 1; cg < fs->fs_ncg; cg++) 297*9163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 298*9163Ssam fs->fs_cgrotor = cg; 299*9163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 300*9163Ssam } 301*9163Ssam for (cg = 0; cg <= fs->fs_cgrotor; cg++) 302*9163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 303*9163Ssam fs->fs_cgrotor = cg; 304*9163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 305*9163Ssam } 306*9163Ssam return (NULL); 307*9163Ssam } 308*9163Ssam /* 309*9163Ssam * One or more previous blocks have been laid out. If less 310*9163Ssam * than fs_maxcontig previous blocks are contiguous, the 311*9163Ssam * next block is requested contiguously, otherwise it is 312*9163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 313*9163Ssam */ 314*9163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 315*9163Ssam if (indx > fs->fs_maxcontig && 316*9163Ssam bap[indx - fs->fs_maxcontig] + fs->fs_frag * fs->fs_maxcontig 317*9163Ssam != nextblk) 318*9163Ssam return (nextblk); 319*9163Ssam if (fs->fs_rotdelay != 0) 320*9163Ssam /* 321*9163Ssam * Here we convert ms of delay to frags as: 322*9163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 323*9163Ssam * ((sect/frag) * (ms/sec)) 324*9163Ssam * then round up to the next block. 325*9163Ssam */ 326*9163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 327*9163Ssam (NSPF(fs) * 1000), fs->fs_frag); 328*9163Ssam 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 */ 456*9163Ssam 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; 5114463Smckusic cgp->cg_frsum[i]++; 5124463Smckusic bdwrite(bp); 5134463Smckusic return (bno); 5144463Smckusic } 5154651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 5166567Smckusic if (bno < 0) 5176294Smckusick return (NULL); 5184463Smckusic for (i = 0; i < frags; i++) 5194463Smckusic clrbit(cgp->cg_free, bno + i); 5204792Smckusic cgp->cg_cs.cs_nffree -= frags; 5214792Smckusic fs->fs_cstotal.cs_nffree -= frags; 5225322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 5234463Smckusic cgp->cg_frsum[allocsiz]--; 5244463Smckusic if (frags != allocsiz) 5254463Smckusic cgp->cg_frsum[allocsiz - frags]++; 5264463Smckusic bdwrite(bp); 5274463Smckusic return (cg * fs->fs_fpg + bno); 5284463Smckusic } 5294463Smckusic 5305375Smckusic /* 5315375Smckusic * Allocate a block in a cylinder group. 5325375Smckusic * 5335375Smckusic * This algorithm implements the following policy: 5345375Smckusic * 1) allocate the requested block. 5355375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 5365375Smckusic * 3) allocate the next available block on the block rotor for the 5375375Smckusic * specified cylinder group. 5385375Smckusic * Note that this routine only allocates fs_bsize blocks; these 5395375Smckusic * blocks may be fragmented by the routine that allocates them. 5405375Smckusic */ 5414463Smckusic daddr_t 5425212Smckusic alloccgblk(fs, cgp, bpref) 5435965Smckusic register struct fs *fs; 5444463Smckusic register struct cg *cgp; 5454463Smckusic daddr_t bpref; 5464463Smckusic { 5474651Smckusic daddr_t bno; 5486294Smckusick int cylno, pos, delta; 5494651Smckusic short *cylbp; 5505361Smckusic register int i; 5514463Smckusic 5524651Smckusic if (bpref == 0) { 5534651Smckusic bpref = cgp->cg_rotor; 5545361Smckusic goto norot; 5555361Smckusic } 5565361Smckusic bpref &= ~(fs->fs_frag - 1); 5575377Smckusic bpref = dtogd(fs, bpref); 5585361Smckusic /* 5595361Smckusic * if the requested block is available, use it 5605361Smckusic */ 5615361Smckusic if (isblock(fs, cgp->cg_free, bpref/fs->fs_frag)) { 5625361Smckusic bno = bpref; 5635361Smckusic goto gotit; 5645361Smckusic } 5655361Smckusic /* 5665361Smckusic * check for a block available on the same cylinder 5675361Smckusic */ 5685361Smckusic cylno = cbtocylno(fs, bpref); 5695375Smckusic if (cgp->cg_btot[cylno] == 0) 5705375Smckusic goto norot; 5715375Smckusic if (fs->fs_cpc == 0) { 5725375Smckusic /* 5735375Smckusic * block layout info is not available, so just have 5745375Smckusic * to take any block in this cylinder. 5755375Smckusic */ 5765375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 5775375Smckusic goto norot; 5785375Smckusic } 5795375Smckusic /* 5805361Smckusic * check the summary information to see if a block is 5815361Smckusic * available in the requested cylinder starting at the 582*9163Ssam * requested rotational position and proceeding around. 5835361Smckusic */ 584*9163Ssam cylbp = cgp->cg_b[cylno]; 585*9163Ssam pos = cbtorpos(fs, bpref); 5865361Smckusic for (i = pos; i < NRPOS; i++) 5875361Smckusic if (cylbp[i] > 0) 5885361Smckusic break; 5895361Smckusic if (i == NRPOS) 5905361Smckusic for (i = 0; i < pos; i++) 5915361Smckusic if (cylbp[i] > 0) 5925361Smckusic break; 5935361Smckusic if (cylbp[i] > 0) { 5944651Smckusic /* 5955361Smckusic * found a rotational position, now find the actual 5965361Smckusic * block. A panic if none is actually there. 5974651Smckusic */ 5985361Smckusic pos = cylno % fs->fs_cpc; 5995361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 6006716Smckusick if (fs->fs_postbl[pos][i] == -1) { 6016716Smckusick printf("pos = %d, i = %d, fs = %s\n", 6026716Smckusick pos, i, fs->fs_fsmnt); 6035361Smckusic panic("alloccgblk: cyl groups corrupted"); 6046716Smckusick } 6056294Smckusick for (i = fs->fs_postbl[pos][i];; ) { 6065361Smckusic if (isblock(fs, cgp->cg_free, bno + i)) { 6075361Smckusic bno = (bno + i) * fs->fs_frag; 6085361Smckusic goto gotit; 6095361Smckusic } 6106294Smckusick delta = fs->fs_rotbl[i]; 6116294Smckusick if (delta <= 0 || delta > MAXBPC - i) 6124651Smckusic break; 6136294Smckusick i += delta; 6144651Smckusic } 6156716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 6165361Smckusic panic("alloccgblk: can't find blk in cyl"); 6174359Smckusick } 6185361Smckusic norot: 6195361Smckusic /* 6205361Smckusic * no blocks in the requested cylinder, so take next 6215361Smckusic * available one in this cylinder group. 6225361Smckusic */ 6238628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 6246567Smckusic if (bno < 0) 6256294Smckusick return (NULL); 6264651Smckusic cgp->cg_rotor = bno; 6274359Smckusick gotit: 6288769Sroot clrblock(fs, cgp->cg_free, (long)(bno/fs->fs_frag)); 6294792Smckusic cgp->cg_cs.cs_nbfree--; 6304792Smckusic fs->fs_cstotal.cs_nbfree--; 6315322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 6325375Smckusic cylno = cbtocylno(fs, bno); 6335375Smckusic cgp->cg_b[cylno][cbtorpos(fs, bno)]--; 6345375Smckusic cgp->cg_btot[cylno]--; 6354359Smckusick fs->fs_fmod++; 6364651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 6374359Smckusick } 6384359Smckusick 6395375Smckusic /* 6405375Smckusic * Determine whether an inode can be allocated. 6415375Smckusic * 6425375Smckusic * Check to see if an inode is available, and if it is, 6435375Smckusic * allocate it using the following policy: 6445375Smckusic * 1) allocate the requested inode. 6455375Smckusic * 2) allocate the next available inode after the requested 6465375Smckusic * inode in the specified cylinder group. 6475375Smckusic */ 648*9163Ssam ino_t 6495965Smckusic ialloccg(ip, cg, ipref, mode) 6505965Smckusic struct inode *ip; 6514359Smckusick int cg; 6524359Smckusick daddr_t ipref; 6534359Smckusick int mode; 6544359Smckusick { 6555965Smckusic register struct fs *fs; 6564463Smckusic register struct buf *bp; 6574463Smckusic register struct cg *cgp; 6584359Smckusick int i; 6594359Smckusick 6605965Smckusic fs = ip->i_fs; 6615322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 6626294Smckusick return (NULL); 6638617Sroot bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_bsize); 6646531Smckusick cgp = bp->b_un.b_cg; 6656531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 6665960Smckusic brelse(bp); 6676294Smckusick return (NULL); 6685960Smckusic } 6698105Sroot cgp->cg_time = time.tv_sec; 6704359Smckusick if (ipref) { 6714359Smckusick ipref %= fs->fs_ipg; 6724359Smckusick if (isclr(cgp->cg_iused, ipref)) 6734359Smckusick goto gotit; 6744359Smckusick } else 6754359Smckusick ipref = cgp->cg_irotor; 6764359Smckusick for (i = 0; i < fs->fs_ipg; i++) { 6774359Smckusick ipref++; 6784359Smckusick if (ipref >= fs->fs_ipg) 6794359Smckusick ipref = 0; 6804359Smckusick if (isclr(cgp->cg_iused, ipref)) { 6814359Smckusick cgp->cg_irotor = ipref; 6824359Smckusick goto gotit; 6834359Smckusick } 6844359Smckusick } 6854359Smckusick brelse(bp); 6866294Smckusick return (NULL); 6874359Smckusick gotit: 6884359Smckusick setbit(cgp->cg_iused, ipref); 6894792Smckusic cgp->cg_cs.cs_nifree--; 6904792Smckusic fs->fs_cstotal.cs_nifree--; 6915322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 6924359Smckusick fs->fs_fmod++; 6934359Smckusick if ((mode & IFMT) == IFDIR) { 6944792Smckusic cgp->cg_cs.cs_ndir++; 6954792Smckusic fs->fs_cstotal.cs_ndir++; 6965322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 6974359Smckusick } 6984359Smckusick bdwrite(bp); 6994359Smckusick return (cg * fs->fs_ipg + ipref); 7004359Smckusick } 7014359Smckusick 7025375Smckusic /* 7035375Smckusic * Free a block or fragment. 7045375Smckusic * 7055375Smckusic * The specified block or fragment is placed back in the 7065375Smckusic * free map. If a fragment is deallocated, a possible 7075375Smckusic * block reassembly is checked. 7085375Smckusic */ 709*9163Ssam free(ip, bno, size) 7105965Smckusic register struct inode *ip; 7114359Smckusick daddr_t bno; 7125212Smckusic off_t size; 7134359Smckusick { 7144359Smckusick register struct fs *fs; 7154359Smckusick register struct cg *cgp; 7164359Smckusick register struct buf *bp; 7174463Smckusic int cg, blk, frags, bbase; 7184463Smckusic register int i; 7194359Smckusick 7205965Smckusic fs = ip->i_fs; 7216716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 7226716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 7236716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 7244426Smckusic panic("free: bad size"); 7256716Smckusick } 7265377Smckusic cg = dtog(fs, bno); 7276567Smckusic if (badblock(fs, bno)) { 7286567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 7294359Smckusick return; 7306567Smckusic } 7318617Sroot bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_bsize); 7326531Smckusick cgp = bp->b_un.b_cg; 7336531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 7345960Smckusic brelse(bp); 7354359Smckusick return; 7365960Smckusic } 7378105Sroot cgp->cg_time = time.tv_sec; 7385377Smckusic bno = dtogd(fs, bno); 7395322Smckusic if (size == fs->fs_bsize) { 7406567Smckusic if (isblock(fs, cgp->cg_free, bno/fs->fs_frag)) { 7416716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 7426716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 7434426Smckusic panic("free: freeing free block"); 7446567Smckusic } 7455322Smckusic setblock(fs, cgp->cg_free, bno/fs->fs_frag); 7464792Smckusic cgp->cg_cs.cs_nbfree++; 7474792Smckusic fs->fs_cstotal.cs_nbfree++; 7485322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 7495375Smckusic i = cbtocylno(fs, bno); 7505375Smckusic cgp->cg_b[i][cbtorpos(fs, bno)]++; 7515375Smckusic cgp->cg_btot[i]++; 7524426Smckusic } else { 7535322Smckusic bbase = bno - (bno % fs->fs_frag); 7544463Smckusic /* 7554463Smckusic * decrement the counts associated with the old frags 7564463Smckusic */ 7576294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 7585322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 7594463Smckusic /* 7604463Smckusic * deallocate the fragment 7614463Smckusic */ 7625960Smckusic frags = numfrags(fs, size); 7634463Smckusic for (i = 0; i < frags; i++) { 7646716Smckusick if (isset(cgp->cg_free, bno + i)) { 7656716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 7666716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 7674426Smckusic panic("free: freeing free frag"); 7686716Smckusick } 7694426Smckusic setbit(cgp->cg_free, bno + i); 7704426Smckusic } 7716294Smckusick cgp->cg_cs.cs_nffree += i; 7726294Smckusick fs->fs_cstotal.cs_nffree += i; 7736294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 7744463Smckusic /* 7754463Smckusic * add back in counts associated with the new frags 7764463Smckusic */ 7776294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 7785322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 7794463Smckusic /* 7804463Smckusic * if a complete block has been reassembled, account for it 7814463Smckusic */ 7825322Smckusic if (isblock(fs, cgp->cg_free, bbase / fs->fs_frag)) { 7835322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 7845322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 7855322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 7864792Smckusic cgp->cg_cs.cs_nbfree++; 7874792Smckusic fs->fs_cstotal.cs_nbfree++; 7885322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 7895375Smckusic i = cbtocylno(fs, bbase); 7905375Smckusic cgp->cg_b[i][cbtorpos(fs, bbase)]++; 7915375Smckusic cgp->cg_btot[i]++; 7924426Smckusic } 7934426Smckusic } 7944359Smckusick fs->fs_fmod++; 7954359Smckusick bdwrite(bp); 7964359Smckusick } 7974359Smckusick 7985375Smckusic /* 7995375Smckusic * Free an inode. 8005375Smckusic * 8015375Smckusic * The specified inode is placed back in the free map. 8025375Smckusic */ 8035965Smckusic ifree(ip, ino, mode) 8045965Smckusic struct inode *ip; 8054359Smckusick ino_t ino; 8064359Smckusick int mode; 8074359Smckusick { 8084359Smckusick register struct fs *fs; 8094359Smckusick register struct cg *cgp; 8104359Smckusick register struct buf *bp; 8114359Smckusick int cg; 8124359Smckusick 8135965Smckusic fs = ip->i_fs; 8146716Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 8156716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 8166716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 8174359Smckusick panic("ifree: range"); 8186716Smckusick } 8195377Smckusic cg = itog(fs, ino); 8208617Sroot bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_bsize); 8216531Smckusick cgp = bp->b_un.b_cg; 8226531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 8235960Smckusic brelse(bp); 8244359Smckusick return; 8255960Smckusic } 8268105Sroot cgp->cg_time = time.tv_sec; 8274359Smckusick ino %= fs->fs_ipg; 8286716Smckusick if (isclr(cgp->cg_iused, ino)) { 8296716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 8306716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 8314359Smckusick panic("ifree: freeing free inode"); 8326716Smckusick } 8334359Smckusick clrbit(cgp->cg_iused, ino); 8344792Smckusic cgp->cg_cs.cs_nifree++; 8354792Smckusic fs->fs_cstotal.cs_nifree++; 8365322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 8374359Smckusick if ((mode & IFMT) == IFDIR) { 8384792Smckusic cgp->cg_cs.cs_ndir--; 8394792Smckusic fs->fs_cstotal.cs_ndir--; 8405322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 8414359Smckusick } 8424359Smckusick fs->fs_fmod++; 8434359Smckusick bdwrite(bp); 8444359Smckusick } 8454359Smckusick 8464463Smckusic /* 8475375Smckusic * Find a block of the specified size in the specified cylinder group. 8485375Smckusic * 8494651Smckusic * It is a panic if a request is made to find a block if none are 8504651Smckusic * available. 8514651Smckusic */ 8524651Smckusic daddr_t 8534651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 8544651Smckusic register struct fs *fs; 8554651Smckusic register struct cg *cgp; 8564651Smckusic daddr_t bpref; 8574651Smckusic int allocsiz; 8584651Smckusic { 8594651Smckusic daddr_t bno; 8604651Smckusic int start, len, loc, i; 8614651Smckusic int blk, field, subfield, pos; 8624651Smckusic 8634651Smckusic /* 8644651Smckusic * find the fragment by searching through the free block 8654651Smckusic * map for an appropriate bit pattern 8664651Smckusic */ 8674651Smckusic if (bpref) 8685377Smckusic start = dtogd(fs, bpref) / NBBY; 8694651Smckusic else 8704651Smckusic start = cgp->cg_frotor / NBBY; 8715398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 8725322Smckusic loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag], 8736292Smckusick 1 << (allocsiz - 1 + (fs->fs_frag % NBBY))); 8744651Smckusic if (loc == 0) { 8756531Smckusick len = start + 1; 8766531Smckusick start = 0; 8775322Smckusic loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag], 8786292Smckusick 1 << (allocsiz - 1 + (fs->fs_frag % NBBY))); 8794651Smckusic if (loc == 0) { 8806716Smckusick printf("start = %d, len = %d, fs = %s\n", 8816716Smckusick start, len, fs->fs_fsmnt); 8824651Smckusic panic("alloccg: map corrupted"); 8836531Smckusick return (-1); 8844651Smckusic } 8854651Smckusic } 8864651Smckusic bno = (start + len - loc) * NBBY; 8874651Smckusic cgp->cg_frotor = bno; 8884651Smckusic /* 8894651Smckusic * found the byte in the map 8904651Smckusic * sift through the bits to find the selected frag 8914651Smckusic */ 8926294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 8936294Smckusick blk = blkmap(fs, cgp->cg_free, bno); 8944651Smckusic blk <<= 1; 8954651Smckusic field = around[allocsiz]; 8964651Smckusic subfield = inside[allocsiz]; 8975322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 8986294Smckusick if ((blk & field) == subfield) 8996294Smckusick return (bno + pos); 9004651Smckusic field <<= 1; 9014651Smckusic subfield <<= 1; 9024651Smckusic } 9034651Smckusic } 9046716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 9054651Smckusic panic("alloccg: block not in map"); 9066531Smckusick return (-1); 9074651Smckusic } 9084651Smckusic 9094651Smckusic /* 9105375Smckusic * Fserr prints the name of a file system with an error diagnostic. 9115375Smckusic * 9125375Smckusic * The form of the error message is: 9134359Smckusick * fs: error message 9144359Smckusick */ 9154359Smckusick fserr(fs, cp) 9164359Smckusick struct fs *fs; 9174359Smckusick char *cp; 9184359Smckusick { 9194359Smckusick 9204359Smckusick printf("%s: %s\n", fs->fs_fsmnt, cp); 9214359Smckusick } 922