123394Smckusick /* 237735Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 334432Sbostic * All rights reserved. 423394Smckusick * 544536Sbostic * %sccs.include.redist.c% 634432Sbostic * 7*51469Sbostic * @(#)ffs_alloc.c 7.28 (Berkeley) 11/01/91 823394Smckusick */ 94359Smckusick 10*51469Sbostic #include <sys/param.h> 11*51469Sbostic #include <sys/systm.h> 12*51469Sbostic #include <sys/buf.h> 13*51469Sbostic #include <sys/proc.h> 14*51469Sbostic #include <sys/vnode.h> 15*51469Sbostic #include <sys/kernel.h> 16*51469Sbostic #include <sys/syslog.h> 174359Smckusick 18*51469Sbostic #include <ufs/ufs/quota.h> 19*51469Sbostic #include <ufs/ufs/inode.h> 2047571Skarels 21*51469Sbostic #include <ufs/ffs/fs.h> 22*51469Sbostic #include <ufs/ffs/ffs_extern.h> 234359Smckusick 24*51469Sbostic extern u_long nextgennumber; 25*51469Sbostic 26*51469Sbostic static daddr_t ffs_alloccg __P((struct inode *, int, daddr_t, int)); 27*51469Sbostic static daddr_t ffs_alloccgblk __P((struct fs *, struct cg *, daddr_t)); 28*51469Sbostic static ino_t ffs_dirpref __P((struct fs *)); 29*51469Sbostic static daddr_t ffs_fragextend __P((struct inode *, int, long, int, int)); 30*51469Sbostic static void ffs_fserr __P((struct fs *, u_int, char *)); 31*51469Sbostic static u_long ffs_hashalloc 32*51469Sbostic __P((struct inode *, int, long, int, u_long (*)())); 33*51469Sbostic static ino_t ffs_ialloccg __P((struct inode *, int, daddr_t, int)); 34*51469Sbostic static daddr_t ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int)); 35*51469Sbostic 365375Smckusic /* 375375Smckusic * Allocate a block in the file system. 385375Smckusic * 395375Smckusic * The size of the requested block is given, which must be some 405375Smckusic * multiple of fs_fsize and <= fs_bsize. 415375Smckusic * A preference may be optionally specified. If a preference is given 425375Smckusic * the following hierarchy is used to allocate a block: 435375Smckusic * 1) allocate the requested block. 445375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 455375Smckusic * 3) allocate a block in the same cylinder group. 465375Smckusic * 4) quadradically rehash into other cylinder groups, until an 475375Smckusic * available block is located. 485375Smckusic * If no block preference is given the following heirarchy is used 495375Smckusic * to allocate a block: 505375Smckusic * 1) allocate a block in the cylinder group that contains the 515375Smckusic * inode for the file. 525375Smckusic * 2) quadradically rehash into other cylinder groups, until an 535375Smckusic * available block is located. 545375Smckusic */ 55*51469Sbostic ffs_alloc(ip, lbn, bpref, size, bnp) 564463Smckusic register struct inode *ip; 5739678Smckusick daddr_t lbn, bpref; 584359Smckusick int size; 5939678Smckusick daddr_t *bnp; 604359Smckusick { 614359Smckusick daddr_t bno; 624359Smckusick register struct fs *fs; 634463Smckusic register struct buf *bp; 6437735Smckusick int cg, error; 6547571Skarels struct ucred *cred = curproc->p_ucred; /* XXX */ 664359Smckusick 6739678Smckusick *bnp = 0; 685965Smckusic fs = ip->i_fs; 696716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 706716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 716716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 72*51469Sbostic panic("ffs_alloc: bad size"); 736716Smckusick } 745322Smckusic if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 754359Smckusick goto nospace; 7641309Smckusick if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 774792Smckusic goto nospace; 787650Ssam #ifdef QUOTA 7941309Smckusick if (error = chkdq(ip, (long)btodb(size), cred, 0)) 8037735Smckusick return (error); 817483Skre #endif 824948Smckusic if (bpref >= fs->fs_size) 834948Smckusic bpref = 0; 844359Smckusick if (bpref == 0) 855377Smckusic cg = itog(fs, ip->i_number); 864359Smckusick else 875377Smckusic cg = dtog(fs, bpref); 88*51469Sbostic bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size, 89*51469Sbostic (u_long (*)())ffs_alloccg); 9039678Smckusick if (bno > 0) { 9139678Smckusick ip->i_blocks += btodb(size); 9239678Smckusick ip->i_flag |= IUPD|ICHG; 9339678Smckusick *bnp = bno; 9439678Smckusick return (0); 9539678Smckusick } 9645173Smckusick #ifdef QUOTA 9745173Smckusick /* 9845173Smckusick * Restore user's disk quota because allocation failed. 9945173Smckusick */ 10045173Smckusick (void) chkdq(ip, (long)-btodb(size), cred, FORCE); 10145173Smckusick #endif 1024359Smckusick nospace: 103*51469Sbostic ffs_fserr(fs, cred->cr_uid, "file system full"); 1044359Smckusick uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 10537735Smckusick return (ENOSPC); 1064359Smckusick } 1074359Smckusick 1085375Smckusic /* 1095375Smckusic * Reallocate a fragment to a bigger size 1105375Smckusic * 1115375Smckusic * The number and size of the old block is given, and a preference 1125375Smckusic * and new size is also specified. The allocator attempts to extend 1135375Smckusic * the original block. Failing that, the regular block allocator is 1145375Smckusic * invoked to get an appropriate block. 1155375Smckusic */ 116*51469Sbostic ffs_realloccg(ip, lbprev, bpref, osize, nsize, bpp) 1175965Smckusic register struct inode *ip; 11839678Smckusick off_t lbprev; 11939678Smckusick daddr_t bpref; 1204426Smckusic int osize, nsize; 12137735Smckusick struct buf **bpp; 1224426Smckusic { 1234426Smckusic register struct fs *fs; 12437735Smckusick struct buf *bp, *obp; 12545719Smckusick int cg, request, error; 12645719Smckusick daddr_t bprev, bno; 12747571Skarels struct ucred *cred = curproc->p_ucred; /* XXX */ 1284426Smckusic 12937735Smckusick *bpp = 0; 1305965Smckusic fs = ip->i_fs; 1315960Smckusic if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 1326716Smckusick (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 133*51469Sbostic printf( 134*51469Sbostic "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 1356716Smckusick ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 136*51469Sbostic panic("ffs_realloccg: bad size"); 1376716Smckusick } 13841309Smckusick if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 1394792Smckusic goto nospace; 14039678Smckusick if ((bprev = ip->i_db[lbprev]) == 0) { 1416716Smckusick printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 1426716Smckusick ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 143*51469Sbostic panic("ffs_realloccg: bad bprev"); 1446716Smckusick } 14539678Smckusick /* 14639678Smckusick * Allocate the extra space in the buffer. 14739678Smckusick */ 14839678Smckusick if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) { 14939678Smckusick brelse(bp); 15039678Smckusick return (error); 15139678Smckusick } 15245173Smckusick #ifdef QUOTA 15345173Smckusick if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) { 15445173Smckusick brelse(bp); 15545173Smckusick return (error); 15645173Smckusick } 15745173Smckusick #endif 15839678Smckusick /* 15939678Smckusick * Check for extension in the existing location. 16039678Smckusick */ 1616294Smckusick cg = dtog(fs, bprev); 162*51469Sbostic if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) { 16339887Smckusick if (bp->b_blkno != fsbtodb(fs, bno)) 16439678Smckusick panic("bad blockno"); 16539762Smckusick ip->i_blocks += btodb(nsize - osize); 16639762Smckusick ip->i_flag |= IUPD|ICHG; 16748948Smckusick allocbuf(bp, nsize); 16848948Smckusick bp->b_flags |= B_DONE; 16948948Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 17037735Smckusick *bpp = bp; 17137735Smckusick return (0); 1724463Smckusic } 17339678Smckusick /* 17439678Smckusick * Allocate a new disk location. 17539678Smckusick */ 1764948Smckusic if (bpref >= fs->fs_size) 1774948Smckusic bpref = 0; 17826253Skarels switch ((int)fs->fs_optim) { 17925256Smckusick case FS_OPTSPACE: 18025256Smckusick /* 18125256Smckusick * Allocate an exact sized fragment. Although this makes 18225256Smckusick * best use of space, we will waste time relocating it if 18325256Smckusick * the file continues to grow. If the fragmentation is 18425256Smckusick * less than half of the minimum free reserve, we choose 18525256Smckusick * to begin optimizing for time. 18625256Smckusick */ 18724698Smckusick request = nsize; 18825256Smckusick if (fs->fs_minfree < 5 || 18925256Smckusick fs->fs_cstotal.cs_nffree > 19025256Smckusick fs->fs_dsize * fs->fs_minfree / (2 * 100)) 19125256Smckusick break; 19225256Smckusick log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 19325256Smckusick fs->fs_fsmnt); 19425256Smckusick fs->fs_optim = FS_OPTTIME; 19525256Smckusick break; 19625256Smckusick case FS_OPTTIME: 19725256Smckusick /* 198*51469Sbostic * At this point we have discovered a file that is trying to 199*51469Sbostic * grow a small fragment to a larger fragment. To save time, 200*51469Sbostic * we allocate a full sized block, then free the unused portion. 201*51469Sbostic * If the file continues to grow, the `ffs_fragextend' call 202*51469Sbostic * above will be able to grow it in place without further 203*51469Sbostic * copying. If aberrant programs cause disk fragmentation to 204*51469Sbostic * grow within 2% of the free reserve, we choose to begin 205*51469Sbostic * optimizing for space. 20625256Smckusick */ 20724698Smckusick request = fs->fs_bsize; 20825256Smckusick if (fs->fs_cstotal.cs_nffree < 20925256Smckusick fs->fs_dsize * (fs->fs_minfree - 2) / 100) 21025256Smckusick break; 21125256Smckusick log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 21225256Smckusick fs->fs_fsmnt); 21325256Smckusick fs->fs_optim = FS_OPTSPACE; 21425256Smckusick break; 21525256Smckusick default: 21625256Smckusick printf("dev = 0x%x, optim = %d, fs = %s\n", 21725256Smckusick ip->i_dev, fs->fs_optim, fs->fs_fsmnt); 218*51469Sbostic panic("ffs_realloccg: bad optim"); 21925256Smckusick /* NOTREACHED */ 22025256Smckusick } 221*51469Sbostic bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request, 222*51469Sbostic (u_long (*)())ffs_alloccg); 2236567Smckusic if (bno > 0) { 22445719Smckusick bp->b_blkno = fsbtodb(fs, bno); 22545719Smckusick (void) vnode_pager_uncache(ITOV(ip)); 226*51469Sbostic ffs_blkfree(ip, bprev, (off_t)osize); 22724698Smckusick if (nsize < request) 228*51469Sbostic ffs_blkfree(ip, bno + numfrags(fs, nsize), 229*51469Sbostic (off_t)(request - nsize)); 23012643Ssam ip->i_blocks += btodb(nsize - osize); 23112643Ssam ip->i_flag |= IUPD|ICHG; 23248948Smckusick allocbuf(bp, nsize); 23348948Smckusick bp->b_flags |= B_DONE; 23448948Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 23537735Smckusick *bpp = bp; 23637735Smckusick return (0); 2374463Smckusic } 23845173Smckusick #ifdef QUOTA 23945173Smckusick /* 24045173Smckusick * Restore user's disk quota because allocation failed. 24145173Smckusick */ 24245173Smckusick (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE); 24345173Smckusick #endif 24439678Smckusick brelse(bp); 2454792Smckusic nospace: 2464463Smckusic /* 2474463Smckusic * no space available 2484463Smckusic */ 249*51469Sbostic ffs_fserr(fs, cred->cr_uid, "file system full"); 2504426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 25137735Smckusick return (ENOSPC); 2524426Smckusic } 2534426Smckusic 2545375Smckusic /* 2555375Smckusic * Allocate an inode in the file system. 2565375Smckusic * 257*51469Sbostic * If allocating a directory, use ffs_dirpref to select the inode. 258*51469Sbostic * If allocating in a directory, the following hierarchy is followed: 259*51469Sbostic * 1) allocate the preferred inode. 2605375Smckusic * 2) allocate an inode in the same cylinder group. 2615375Smckusic * 3) quadradically rehash into other cylinder groups, until an 2625375Smckusic * available inode is located. 2635375Smckusic * If no inode preference is given the following heirarchy is used 2645375Smckusic * to allocate an inode: 2655375Smckusic * 1) allocate an inode in cylinder group 0. 2665375Smckusic * 2) quadradically rehash into other cylinder groups, until an 2675375Smckusic * available inode is located. 2685375Smckusic */ 269*51469Sbostic ffs_ialloc(pip, mode, cred, ipp) 2705965Smckusic register struct inode *pip; 2714359Smckusick int mode; 27241309Smckusick struct ucred *cred; 27337735Smckusick struct inode **ipp; 2744359Smckusick { 2754359Smckusick register struct fs *fs; 2764359Smckusick register struct inode *ip; 277*51469Sbostic ino_t ino, ipref; 27837735Smckusick int cg, error; 2794359Smckusick 280*51469Sbostic *ipp = NULL; 2815965Smckusic fs = pip->i_fs; 2824792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 2834359Smckusick goto noinodes; 284*51469Sbostic 285*51469Sbostic if ((mode & IFMT) == IFDIR) 286*51469Sbostic ipref = ffs_dirpref(pip->i_fs); 287*51469Sbostic else 288*51469Sbostic ipref = pip->i_number; 2894948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 2904948Smckusic ipref = 0; 2915377Smckusic cg = itog(fs, ipref); 292*51469Sbostic ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_ialloccg); 2934359Smckusick if (ino == 0) 2944359Smckusick goto noinodes; 295*51469Sbostic error = ffs_iget(pip, ino, ipp); 29637735Smckusick if (error) { 297*51469Sbostic ffs_ifree(pip, ino, mode); /* XXX already freed? */ 29837735Smckusick return (error); 2994359Smckusick } 30038255Smckusick ip = *ipp; 3016716Smckusick if (ip->i_mode) { 3026716Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 3036716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 304*51469Sbostic panic("ffs_ialloc: dup alloc"); 3056716Smckusick } 30612643Ssam if (ip->i_blocks) { /* XXX */ 30712643Ssam printf("free inode %s/%d had %d blocks\n", 30812643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 30912643Ssam ip->i_blocks = 0; 31012643Ssam } 31139516Smckusick ip->i_flags = 0; 31238255Smckusick /* 31338255Smckusick * Set up a new generation number for this inode. 31438255Smckusick */ 31538255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 31638255Smckusick nextgennumber = time.tv_sec; 31738255Smckusick ip->i_gen = nextgennumber; 31837735Smckusick return (0); 3194359Smckusick noinodes: 320*51469Sbostic ffs_fserr(fs, cred->cr_uid, "out of inodes"); 3216294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 32237735Smckusick return (ENOSPC); 3234359Smckusick } 3244359Smckusick 3254651Smckusic /* 3265375Smckusic * Find a cylinder to place a directory. 3275375Smckusic * 3285375Smckusic * The policy implemented by this algorithm is to select from 3295375Smckusic * among those cylinder groups with above the average number of 3305375Smckusic * free inodes, the one with the smallest number of directories. 3314651Smckusic */ 332*51469Sbostic static ino_t 333*51469Sbostic ffs_dirpref(fs) 3345965Smckusic register struct fs *fs; 3354359Smckusick { 3364651Smckusic int cg, minndir, mincg, avgifree; 3374359Smckusick 3384792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 3394651Smckusic minndir = fs->fs_ipg; 3404359Smckusick mincg = 0; 3414651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 3425322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 3435322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 3444359Smckusick mincg = cg; 3455322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 3464359Smckusick } 3479163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 3484359Smckusick } 3494359Smckusick 3504651Smckusic /* 3519163Ssam * Select the desired position for the next block in a file. The file is 3529163Ssam * logically divided into sections. The first section is composed of the 3539163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 3549163Ssam * 3559163Ssam * If no blocks have been allocated in the first section, the policy is to 3569163Ssam * request a block in the same cylinder group as the inode that describes 3579163Ssam * the file. If no blocks have been allocated in any other section, the 3589163Ssam * policy is to place the section in a cylinder group with a greater than 3599163Ssam * average number of free blocks. An appropriate cylinder group is found 36017696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of 36117696Smckusick * blocks is needed, the sweep begins in the cylinder group following the 36217696Smckusick * cylinder group from which the previous allocation was made. The sweep 36317696Smckusick * continues until a cylinder group with greater than the average number 36417696Smckusick * of free blocks is found. If the allocation is for the first block in an 36517696Smckusick * indirect block, the information on the previous allocation is unavailable; 36617696Smckusick * here a best guess is made based upon the logical block number being 36717696Smckusick * allocated. 3689163Ssam * 3699163Ssam * If a section is already partially allocated, the policy is to 3709163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 3719163Ssam * contiguous blocks and the beginning of the next is physically separated 3729163Ssam * so that the disk head will be in transit between them for at least 3739163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 3749163Ssam * schedule another I/O transfer. 3754651Smckusic */ 3765212Smckusic daddr_t 377*51469Sbostic ffs_blkpref(ip, lbn, indx, bap) 3789163Ssam struct inode *ip; 3799163Ssam daddr_t lbn; 3809163Ssam int indx; 3819163Ssam daddr_t *bap; 3829163Ssam { 3835965Smckusic register struct fs *fs; 38417696Smckusick register int cg; 38517696Smckusick int avgbfree, startcg; 3869163Ssam daddr_t nextblk; 3874651Smckusic 3889163Ssam fs = ip->i_fs; 3899163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 3909163Ssam if (lbn < NDADDR) { 3919163Ssam cg = itog(fs, ip->i_number); 3925322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 3934651Smckusic } 3949163Ssam /* 3959163Ssam * Find a cylinder with greater than average number of 3969163Ssam * unused data blocks. 3979163Ssam */ 39817696Smckusick if (indx == 0 || bap[indx - 1] == 0) 39917696Smckusick startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; 40017696Smckusick else 40117696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1; 40217696Smckusick startcg %= fs->fs_ncg; 4039163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 40417696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++) 4059163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4069163Ssam fs->fs_cgrotor = cg; 4079163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4089163Ssam } 40917696Smckusick for (cg = 0; cg <= startcg; cg++) 4109163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4119163Ssam fs->fs_cgrotor = cg; 4129163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4139163Ssam } 4149163Ssam return (NULL); 4159163Ssam } 4169163Ssam /* 4179163Ssam * One or more previous blocks have been laid out. If less 4189163Ssam * than fs_maxcontig previous blocks are contiguous, the 4199163Ssam * next block is requested contiguously, otherwise it is 4209163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 4219163Ssam */ 4229163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 4239163Ssam if (indx > fs->fs_maxcontig && 42411638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 4259163Ssam != nextblk) 4269163Ssam return (nextblk); 4279163Ssam if (fs->fs_rotdelay != 0) 4289163Ssam /* 4299163Ssam * Here we convert ms of delay to frags as: 4309163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 4319163Ssam * ((sect/frag) * (ms/sec)) 4329163Ssam * then round up to the next block. 4339163Ssam */ 4349163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 4359163Ssam (NSPF(fs) * 1000), fs->fs_frag); 4369163Ssam return (nextblk); 4374651Smckusic } 4384651Smckusic 4395375Smckusic /* 4405375Smckusic * Implement the cylinder overflow algorithm. 4415375Smckusic * 4425375Smckusic * The policy implemented by this algorithm is: 4435375Smckusic * 1) allocate the block in its requested cylinder group. 4445375Smckusic * 2) quadradically rehash on the cylinder group number. 4455375Smckusic * 3) brute force search for a free block. 4465375Smckusic */ 4475212Smckusic /*VARARGS5*/ 448*51469Sbostic static u_long 449*51469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator) 4505965Smckusic struct inode *ip; 4514359Smckusick int cg; 4524359Smckusick long pref; 4534359Smckusick int size; /* size for data blocks, mode for inodes */ 4545212Smckusic u_long (*allocator)(); 4554359Smckusick { 4565965Smckusic register struct fs *fs; 4574359Smckusick long result; 4584359Smckusick int i, icg = cg; 4594359Smckusick 4605965Smckusic fs = ip->i_fs; 4614359Smckusick /* 4624359Smckusick * 1: preferred cylinder group 4634359Smckusick */ 4645965Smckusic result = (*allocator)(ip, cg, pref, size); 4654359Smckusick if (result) 4664359Smckusick return (result); 4674359Smckusick /* 4684359Smckusick * 2: quadratic rehash 4694359Smckusick */ 4704359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 4714359Smckusick cg += i; 4724359Smckusick if (cg >= fs->fs_ncg) 4734359Smckusick cg -= fs->fs_ncg; 4745965Smckusic result = (*allocator)(ip, cg, 0, size); 4754359Smckusick if (result) 4764359Smckusick return (result); 4774359Smckusick } 4784359Smckusick /* 4794359Smckusick * 3: brute force search 48010847Ssam * Note that we start at i == 2, since 0 was checked initially, 48110847Ssam * and 1 is always checked in the quadratic rehash. 4824359Smckusick */ 48310848Smckusick cg = (icg + 2) % fs->fs_ncg; 48410847Ssam for (i = 2; i < fs->fs_ncg; i++) { 4855965Smckusic result = (*allocator)(ip, cg, 0, size); 4864359Smckusick if (result) 4874359Smckusick return (result); 4884359Smckusick cg++; 4894359Smckusick if (cg == fs->fs_ncg) 4904359Smckusick cg = 0; 4914359Smckusick } 4926294Smckusick return (NULL); 4934359Smckusick } 4944359Smckusick 4955375Smckusic /* 4965375Smckusic * Determine whether a fragment can be extended. 4975375Smckusic * 4985375Smckusic * Check to see if the necessary fragments are available, and 4995375Smckusic * if they are, allocate them. 5005375Smckusic */ 501*51469Sbostic static daddr_t 502*51469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize) 5035965Smckusic struct inode *ip; 5044426Smckusic int cg; 5054463Smckusic long bprev; 5064426Smckusic int osize, nsize; 5074426Smckusic { 5085965Smckusic register struct fs *fs; 5094463Smckusic register struct cg *cgp; 51037735Smckusick struct buf *bp; 5114463Smckusic long bno; 5124463Smckusic int frags, bbase; 51337735Smckusick int i, error; 5144426Smckusic 5155965Smckusic fs = ip->i_fs; 51617224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 5176531Smckusick return (NULL); 5185960Smckusic frags = numfrags(fs, nsize); 51917224Smckusick bbase = fragnum(fs, bprev); 52017224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 52130749Skarels /* cannot extend across a block boundary */ 5226294Smckusick return (NULL); 5234463Smckusic } 52437735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 52538776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 52637735Smckusick if (error) { 52737735Smckusick brelse(bp); 52837735Smckusick return (NULL); 52937735Smckusick } 5306531Smckusick cgp = bp->b_un.b_cg; 53137735Smckusick if (!cg_chkmagic(cgp)) { 5325960Smckusic brelse(bp); 5336294Smckusick return (NULL); 5345960Smckusic } 5358105Sroot cgp->cg_time = time.tv_sec; 5365377Smckusic bno = dtogd(fs, bprev); 5375960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 53834143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) { 5395361Smckusic brelse(bp); 5406294Smckusick return (NULL); 5415361Smckusic } 5425361Smckusic /* 5435361Smckusic * the current fragment can be extended 5445361Smckusic * deduct the count on fragment being extended into 5455361Smckusic * increase the count on the remaining fragment (if any) 5465361Smckusic * allocate the extended piece 5475361Smckusic */ 5485361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 54934143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) 5504463Smckusic break; 5515960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 5525361Smckusic if (i != frags) 5535361Smckusic cgp->cg_frsum[i - frags]++; 5545960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 55534143Smckusick clrbit(cg_blksfree(cgp), bno + i); 5565361Smckusic cgp->cg_cs.cs_nffree--; 5575361Smckusic fs->fs_cstotal.cs_nffree--; 5585361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 5594463Smckusic } 56050893Smckusick fs->fs_fmod = 1; 5615361Smckusic bdwrite(bp); 5625361Smckusic return (bprev); 5634426Smckusic } 5644426Smckusic 5655375Smckusic /* 5665375Smckusic * Determine whether a block can be allocated. 5675375Smckusic * 5685375Smckusic * Check to see if a block of the apprpriate size is available, 5695375Smckusic * and if it is, allocate it. 5705375Smckusic */ 5719163Ssam daddr_t 572*51469Sbostic ffs_alloccg(ip, cg, bpref, size) 5735965Smckusic struct inode *ip; 5744359Smckusick int cg; 5754359Smckusick daddr_t bpref; 5764359Smckusick int size; 5774359Smckusick { 5785965Smckusic register struct fs *fs; 5794463Smckusic register struct cg *cgp; 58037735Smckusick struct buf *bp; 5814463Smckusic register int i; 58237735Smckusick int error, bno, frags, allocsiz; 5834359Smckusick 5845965Smckusic fs = ip->i_fs; 5855322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 5866294Smckusick return (NULL); 58737735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 58838776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 58937735Smckusick if (error) { 59037735Smckusick brelse(bp); 59137735Smckusick return (NULL); 59237735Smckusick } 5936531Smckusick cgp = bp->b_un.b_cg; 59437735Smckusick if (!cg_chkmagic(cgp) || 59515950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 5965960Smckusic brelse(bp); 5976294Smckusick return (NULL); 5985960Smckusic } 5998105Sroot cgp->cg_time = time.tv_sec; 6005322Smckusic if (size == fs->fs_bsize) { 601*51469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref); 6024463Smckusic bdwrite(bp); 6034463Smckusic return (bno); 6044463Smckusic } 6054463Smckusic /* 6064463Smckusic * check to see if any fragments are already available 6074463Smckusic * allocsiz is the size which will be allocated, hacking 6084463Smckusic * it down to a smaller size if necessary 6094463Smckusic */ 6105960Smckusic frags = numfrags(fs, size); 6115322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 6124463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 6134463Smckusic break; 6145322Smckusic if (allocsiz == fs->fs_frag) { 6154463Smckusic /* 6164463Smckusic * no fragments were available, so a block will be 6174463Smckusic * allocated, and hacked up 6184463Smckusic */ 6194792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 6204463Smckusic brelse(bp); 6216294Smckusick return (NULL); 6224463Smckusic } 623*51469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref); 6245377Smckusic bpref = dtogd(fs, bno); 6255322Smckusic for (i = frags; i < fs->fs_frag; i++) 62634143Smckusick setbit(cg_blksfree(cgp), bpref + i); 6275322Smckusic i = fs->fs_frag - frags; 6284792Smckusic cgp->cg_cs.cs_nffree += i; 6294792Smckusic fs->fs_cstotal.cs_nffree += i; 6305322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 63150893Smckusick fs->fs_fmod = 1; 6324463Smckusic cgp->cg_frsum[i]++; 6334463Smckusic bdwrite(bp); 6344463Smckusic return (bno); 6354463Smckusic } 636*51469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); 63715950Smckusick if (bno < 0) { 63815950Smckusick brelse(bp); 6396294Smckusick return (NULL); 64015950Smckusick } 6414463Smckusic for (i = 0; i < frags; i++) 64234143Smckusick clrbit(cg_blksfree(cgp), bno + i); 6434792Smckusic cgp->cg_cs.cs_nffree -= frags; 6444792Smckusic fs->fs_cstotal.cs_nffree -= frags; 6455322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 64650893Smckusick fs->fs_fmod = 1; 6474463Smckusic cgp->cg_frsum[allocsiz]--; 6484463Smckusic if (frags != allocsiz) 6494463Smckusic cgp->cg_frsum[allocsiz - frags]++; 6504463Smckusic bdwrite(bp); 6514463Smckusic return (cg * fs->fs_fpg + bno); 6524463Smckusic } 6534463Smckusic 6545375Smckusic /* 6555375Smckusic * Allocate a block in a cylinder group. 6565375Smckusic * 6575375Smckusic * This algorithm implements the following policy: 6585375Smckusic * 1) allocate the requested block. 6595375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 6605375Smckusic * 3) allocate the next available block on the block rotor for the 6615375Smckusic * specified cylinder group. 6625375Smckusic * Note that this routine only allocates fs_bsize blocks; these 6635375Smckusic * blocks may be fragmented by the routine that allocates them. 6645375Smckusic */ 665*51469Sbostic static daddr_t 666*51469Sbostic ffs_alloccgblk(fs, cgp, bpref) 6675965Smckusic register struct fs *fs; 6684463Smckusic register struct cg *cgp; 6694463Smckusic daddr_t bpref; 6704463Smckusic { 6714651Smckusic daddr_t bno; 6726294Smckusick int cylno, pos, delta; 6734651Smckusic short *cylbp; 6745361Smckusic register int i; 6754463Smckusic 6764651Smckusic if (bpref == 0) { 6774651Smckusic bpref = cgp->cg_rotor; 6785361Smckusic goto norot; 6795361Smckusic } 68017224Smckusick bpref = blknum(fs, bpref); 6815377Smckusic bpref = dtogd(fs, bpref); 6825361Smckusic /* 6835361Smckusic * if the requested block is available, use it 6845361Smckusic */ 685*51469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 6865361Smckusic bno = bpref; 6875361Smckusic goto gotit; 6885361Smckusic } 6895361Smckusic /* 6905361Smckusic * check for a block available on the same cylinder 6915361Smckusic */ 6925361Smckusic cylno = cbtocylno(fs, bpref); 69334143Smckusick if (cg_blktot(cgp)[cylno] == 0) 6945375Smckusic goto norot; 6955375Smckusic if (fs->fs_cpc == 0) { 6965375Smckusic /* 6975375Smckusic * block layout info is not available, so just have 6985375Smckusic * to take any block in this cylinder. 6995375Smckusic */ 7005375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 7015375Smckusic goto norot; 7025375Smckusic } 7035375Smckusic /* 7045361Smckusic * check the summary information to see if a block is 7055361Smckusic * available in the requested cylinder starting at the 7069163Ssam * requested rotational position and proceeding around. 7075361Smckusic */ 70834143Smckusick cylbp = cg_blks(fs, cgp, cylno); 7099163Ssam pos = cbtorpos(fs, bpref); 71034143Smckusick for (i = pos; i < fs->fs_nrpos; i++) 7115361Smckusic if (cylbp[i] > 0) 7125361Smckusic break; 71334143Smckusick if (i == fs->fs_nrpos) 7145361Smckusic for (i = 0; i < pos; i++) 7155361Smckusic if (cylbp[i] > 0) 7165361Smckusic break; 7175361Smckusic if (cylbp[i] > 0) { 7184651Smckusic /* 7195361Smckusic * found a rotational position, now find the actual 7205361Smckusic * block. A panic if none is actually there. 7214651Smckusic */ 7225361Smckusic pos = cylno % fs->fs_cpc; 7235361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 72434143Smckusick if (fs_postbl(fs, pos)[i] == -1) { 7256716Smckusick printf("pos = %d, i = %d, fs = %s\n", 7266716Smckusick pos, i, fs->fs_fsmnt); 727*51469Sbostic panic("ffs_alloccgblk: cyl groups corrupted"); 7286716Smckusick } 72934143Smckusick for (i = fs_postbl(fs, pos)[i];; ) { 730*51469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) { 73111638Ssam bno = blkstofrags(fs, (bno + i)); 7325361Smckusic goto gotit; 7335361Smckusic } 73434143Smckusick delta = fs_rotbl(fs)[i]; 73534143Smckusick if (delta <= 0 || 73634143Smckusick delta + i > fragstoblks(fs, fs->fs_fpg)) 7374651Smckusic break; 7386294Smckusick i += delta; 7394651Smckusic } 7406716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 741*51469Sbostic panic("ffs_alloccgblk: can't find blk in cyl"); 7424359Smckusick } 7435361Smckusic norot: 7445361Smckusic /* 7455361Smckusic * no blocks in the requested cylinder, so take next 7465361Smckusic * available one in this cylinder group. 7475361Smckusic */ 748*51469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 7496567Smckusic if (bno < 0) 7506294Smckusick return (NULL); 7514651Smckusic cgp->cg_rotor = bno; 7524359Smckusick gotit: 753*51469Sbostic ffs_clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno)); 7544792Smckusic cgp->cg_cs.cs_nbfree--; 7554792Smckusic fs->fs_cstotal.cs_nbfree--; 7565322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 7575375Smckusic cylno = cbtocylno(fs, bno); 75834143Smckusick cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 75934143Smckusick cg_blktot(cgp)[cylno]--; 76050893Smckusick fs->fs_fmod = 1; 7614651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 7624359Smckusick } 76334143Smckusick 7645375Smckusic /* 7655375Smckusic * Determine whether an inode can be allocated. 7665375Smckusic * 7675375Smckusic * Check to see if an inode is available, and if it is, 7685375Smckusic * allocate it using the following policy: 7695375Smckusic * 1) allocate the requested inode. 7705375Smckusic * 2) allocate the next available inode after the requested 7715375Smckusic * inode in the specified cylinder group. 7725375Smckusic */ 773*51469Sbostic static ino_t 774*51469Sbostic ffs_ialloccg(ip, cg, ipref, mode) 7755965Smckusic struct inode *ip; 7764359Smckusick int cg; 7774359Smckusick daddr_t ipref; 7784359Smckusick int mode; 7794359Smckusick { 7805965Smckusic register struct fs *fs; 7814463Smckusic register struct cg *cgp; 78216784Smckusick struct buf *bp; 78337735Smckusick int error, start, len, loc, map, i; 7844359Smckusick 7855965Smckusic fs = ip->i_fs; 7865322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 7876294Smckusick return (NULL); 78837735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 78938776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 79037735Smckusick if (error) { 79137735Smckusick brelse(bp); 79237735Smckusick return (NULL); 79337735Smckusick } 7946531Smckusick cgp = bp->b_un.b_cg; 79537735Smckusick if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 7965960Smckusic brelse(bp); 7976294Smckusick return (NULL); 7985960Smckusic } 7998105Sroot cgp->cg_time = time.tv_sec; 8004359Smckusick if (ipref) { 8014359Smckusick ipref %= fs->fs_ipg; 80234143Smckusick if (isclr(cg_inosused(cgp), ipref)) 8034359Smckusick goto gotit; 80416784Smckusick } 80516784Smckusick start = cgp->cg_irotor / NBBY; 80616784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 80734143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 80816784Smckusick if (loc == 0) { 80917697Smckusick len = start + 1; 81017697Smckusick start = 0; 81134143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 81217697Smckusick if (loc == 0) { 81317697Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 81417697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 815*51469Sbostic panic("ffs_ialloccg: map corrupted"); 81617697Smckusick /* NOTREACHED */ 81717697Smckusick } 81816784Smckusick } 81916784Smckusick i = start + len - loc; 82034143Smckusick map = cg_inosused(cgp)[i]; 82116784Smckusick ipref = i * NBBY; 82216784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 82316784Smckusick if ((map & i) == 0) { 8244359Smckusick cgp->cg_irotor = ipref; 8254359Smckusick goto gotit; 8264359Smckusick } 8274359Smckusick } 82816784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 829*51469Sbostic panic("ffs_ialloccg: block not in map"); 83016784Smckusick /* NOTREACHED */ 8314359Smckusick gotit: 83234143Smckusick setbit(cg_inosused(cgp), ipref); 8334792Smckusic cgp->cg_cs.cs_nifree--; 8344792Smckusic fs->fs_cstotal.cs_nifree--; 8355322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 83650893Smckusick fs->fs_fmod = 1; 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 bdwrite(bp); 8434359Smckusick return (cg * fs->fs_ipg + ipref); 8444359Smckusick } 8454359Smckusick 8465375Smckusic /* 8475375Smckusic * Free a block or fragment. 8485375Smckusic * 8495375Smckusic * The specified block or fragment is placed back in the 8505375Smckusic * free map. If a fragment is deallocated, a possible 8515375Smckusic * block reassembly is checked. 8525375Smckusic */ 853*51469Sbostic ffs_blkfree(ip, bno, size) 8545965Smckusic register struct inode *ip; 8554359Smckusick daddr_t bno; 8565212Smckusic off_t size; 8574359Smckusick { 8584359Smckusick register struct fs *fs; 8594359Smckusick register struct cg *cgp; 86037735Smckusick struct buf *bp; 86137735Smckusick int error, cg, blk, frags, bbase; 8624463Smckusic register int i; 86347571Skarels struct ucred *cred = curproc->p_ucred; /* XXX */ 8644359Smckusick 8655965Smckusic fs = ip->i_fs; 8666716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 8676716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 8686716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 86931402Smckusick panic("blkfree: bad size"); 8706716Smckusick } 8715377Smckusic cg = dtog(fs, bno); 87242318Smckusick if ((unsigned)bno >= fs->fs_size) { 8736567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 874*51469Sbostic ffs_fserr(fs, cred->cr_uid, "bad block"); 8754359Smckusick return; 8766567Smckusic } 87737735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 87838776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 87937735Smckusick if (error) { 88037735Smckusick brelse(bp); 88137735Smckusick return; 88237735Smckusick } 8836531Smckusick cgp = bp->b_un.b_cg; 88437735Smckusick if (!cg_chkmagic(cgp)) { 8855960Smckusic brelse(bp); 8864359Smckusick return; 8875960Smckusic } 8888105Sroot cgp->cg_time = time.tv_sec; 8895377Smckusic bno = dtogd(fs, bno); 8905322Smckusic if (size == fs->fs_bsize) { 891*51469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) { 8926716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 8936716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 89431402Smckusick panic("blkfree: freeing free block"); 8956567Smckusic } 896*51469Sbostic ffs_setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno)); 8974792Smckusic cgp->cg_cs.cs_nbfree++; 8984792Smckusic fs->fs_cstotal.cs_nbfree++; 8995322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9005375Smckusic i = cbtocylno(fs, bno); 90134143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; 90234143Smckusick cg_blktot(cgp)[i]++; 9034426Smckusic } else { 90417224Smckusick bbase = bno - fragnum(fs, bno); 9054463Smckusic /* 9064463Smckusic * decrement the counts associated with the old frags 9074463Smckusic */ 90834143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 909*51469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 9104463Smckusic /* 9114463Smckusic * deallocate the fragment 9124463Smckusic */ 9135960Smckusic frags = numfrags(fs, size); 9144463Smckusic for (i = 0; i < frags; i++) { 91534143Smckusick if (isset(cg_blksfree(cgp), bno + i)) { 9166716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9176716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 91831402Smckusick panic("blkfree: freeing free frag"); 9196716Smckusick } 92034143Smckusick setbit(cg_blksfree(cgp), bno + i); 9214426Smckusic } 9226294Smckusick cgp->cg_cs.cs_nffree += i; 9236294Smckusick fs->fs_cstotal.cs_nffree += i; 9246294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 9254463Smckusic /* 9264463Smckusic * add back in counts associated with the new frags 9274463Smckusic */ 92834143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 929*51469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 9304463Smckusic /* 9314463Smckusic * if a complete block has been reassembled, account for it 9324463Smckusic */ 933*51469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), 93434476Smckusick (daddr_t)fragstoblks(fs, bbase))) { 9355322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 9365322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 9375322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 9384792Smckusic cgp->cg_cs.cs_nbfree++; 9394792Smckusic fs->fs_cstotal.cs_nbfree++; 9405322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9415375Smckusic i = cbtocylno(fs, bbase); 94234143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 94334143Smckusick cg_blktot(cgp)[i]++; 9444426Smckusic } 9454426Smckusic } 94650893Smckusick fs->fs_fmod = 1; 9474359Smckusick bdwrite(bp); 9484359Smckusick } 9494359Smckusick 9505375Smckusic /* 9515375Smckusic * Free an inode. 9525375Smckusic * 9535375Smckusic * The specified inode is placed back in the free map. 9545375Smckusic */ 955*51469Sbostic void 956*51469Sbostic ffs_ifree(pip, ino, mode) 957*51469Sbostic struct inode *pip; 9584359Smckusick ino_t ino; 9594359Smckusick int mode; 9604359Smckusick { 9614359Smckusick register struct fs *fs; 9624359Smckusick register struct cg *cgp; 96337735Smckusick struct buf *bp; 96437735Smckusick int error, cg; 9654359Smckusick 966*51469Sbostic fs = pip->i_fs; 967*51469Sbostic if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg) 968*51469Sbostic panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n", 969*51469Sbostic pip->i_dev, ino, fs->fs_fsmnt); 9705377Smckusic cg = itog(fs, ino); 971*51469Sbostic error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 97238776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 97337735Smckusick if (error) { 97437735Smckusick brelse(bp); 97537735Smckusick return; 97637735Smckusick } 9776531Smckusick cgp = bp->b_un.b_cg; 97837735Smckusick if (!cg_chkmagic(cgp)) { 9795960Smckusic brelse(bp); 9804359Smckusick return; 9815960Smckusic } 9828105Sroot cgp->cg_time = time.tv_sec; 9834359Smckusick ino %= fs->fs_ipg; 98434143Smckusick if (isclr(cg_inosused(cgp), ino)) { 9856716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 986*51469Sbostic pip->i_dev, ino, fs->fs_fsmnt); 98748057Smckusick if (fs->fs_ronly == 0) 98848057Smckusick panic("ifree: freeing free inode"); 9896716Smckusick } 99034143Smckusick clrbit(cg_inosused(cgp), ino); 99116784Smckusick if (ino < cgp->cg_irotor) 99216784Smckusick cgp->cg_irotor = ino; 9934792Smckusic cgp->cg_cs.cs_nifree++; 9944792Smckusic fs->fs_cstotal.cs_nifree++; 9955322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 9964359Smckusick if ((mode & IFMT) == IFDIR) { 9974792Smckusic cgp->cg_cs.cs_ndir--; 9984792Smckusic fs->fs_cstotal.cs_ndir--; 9995322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 10004359Smckusick } 100150893Smckusick fs->fs_fmod = 1; 10024359Smckusick bdwrite(bp); 10034359Smckusick } 10044359Smckusick 10054463Smckusic /* 10065375Smckusic * Find a block of the specified size in the specified cylinder group. 10075375Smckusic * 10084651Smckusic * It is a panic if a request is made to find a block if none are 10094651Smckusic * available. 10104651Smckusic */ 1011*51469Sbostic static daddr_t 1012*51469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz) 10134651Smckusic register struct fs *fs; 10144651Smckusic register struct cg *cgp; 10154651Smckusic daddr_t bpref; 10164651Smckusic int allocsiz; 10174651Smckusic { 10184651Smckusic daddr_t bno; 10194651Smckusic int start, len, loc, i; 10204651Smckusic int blk, field, subfield, pos; 10214651Smckusic 10224651Smckusic /* 10234651Smckusic * find the fragment by searching through the free block 10244651Smckusic * map for an appropriate bit pattern 10254651Smckusic */ 10264651Smckusic if (bpref) 10275377Smckusic start = dtogd(fs, bpref) / NBBY; 10284651Smckusic else 10294651Smckusic start = cgp->cg_frotor / NBBY; 10305398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 103134476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start], 103234476Smckusick (u_char *)fragtbl[fs->fs_frag], 103334476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 10344651Smckusic if (loc == 0) { 10356531Smckusick len = start + 1; 10366531Smckusick start = 0; 103734476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0], 103834476Smckusick (u_char *)fragtbl[fs->fs_frag], 103934476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 104016784Smckusick if (loc == 0) { 104116784Smckusick printf("start = %d, len = %d, fs = %s\n", 104216784Smckusick start, len, fs->fs_fsmnt); 1043*51469Sbostic panic("ffs_alloccg: map corrupted"); 104417697Smckusick /* NOTREACHED */ 104516784Smckusick } 10464651Smckusic } 10474651Smckusic bno = (start + len - loc) * NBBY; 10484651Smckusic cgp->cg_frotor = bno; 10494651Smckusic /* 10504651Smckusic * found the byte in the map 10514651Smckusic * sift through the bits to find the selected frag 10524651Smckusic */ 10536294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 105434143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bno); 10554651Smckusic blk <<= 1; 10564651Smckusic field = around[allocsiz]; 10574651Smckusic subfield = inside[allocsiz]; 10585322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 10596294Smckusick if ((blk & field) == subfield) 10606294Smckusick return (bno + pos); 10614651Smckusic field <<= 1; 10624651Smckusic subfield <<= 1; 10634651Smckusic } 10644651Smckusic } 10656716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 1066*51469Sbostic panic("ffs_alloccg: block not in map"); 10676531Smckusick return (-1); 10684651Smckusic } 10694651Smckusic 10704651Smckusic /* 10715375Smckusic * Fserr prints the name of a file system with an error diagnostic. 10725375Smckusic * 10735375Smckusic * The form of the error message is: 10744359Smckusick * fs: error message 10754359Smckusick */ 1076*51469Sbostic static void 1077*51469Sbostic ffs_fserr(fs, uid, cp) 10784359Smckusick struct fs *fs; 1079*51469Sbostic u_int uid; 10804359Smckusick char *cp; 10814359Smckusick { 10824359Smckusick 108342318Smckusick log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp); 10844359Smckusick } 1085