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*54653Smckusick * @(#)ffs_alloc.c 7.38 (Berkeley) 07/03/92 823394Smckusick */ 94359Smckusick 1051469Sbostic #include <sys/param.h> 1151469Sbostic #include <sys/systm.h> 1251469Sbostic #include <sys/buf.h> 1351469Sbostic #include <sys/proc.h> 1451469Sbostic #include <sys/vnode.h> 15*54653Smckusick #include <sys/mount.h> 1651469Sbostic #include <sys/kernel.h> 1751469Sbostic #include <sys/syslog.h> 184359Smckusick 1953317Smckusick #include <vm/vm.h> 2053317Smckusick 2151469Sbostic #include <ufs/ufs/quota.h> 2251469Sbostic #include <ufs/ufs/inode.h> 2347571Skarels 2451469Sbostic #include <ufs/ffs/fs.h> 2551469Sbostic #include <ufs/ffs/ffs_extern.h> 264359Smckusick 2751469Sbostic extern u_long nextgennumber; 2851469Sbostic 2951469Sbostic static daddr_t ffs_alloccg __P((struct inode *, int, daddr_t, int)); 3051469Sbostic static daddr_t ffs_alloccgblk __P((struct fs *, struct cg *, daddr_t)); 3151469Sbostic static ino_t ffs_dirpref __P((struct fs *)); 3251469Sbostic static daddr_t ffs_fragextend __P((struct inode *, int, long, int, int)); 3351469Sbostic static void ffs_fserr __P((struct fs *, u_int, char *)); 3451469Sbostic static u_long ffs_hashalloc 3551469Sbostic __P((struct inode *, int, long, int, u_long (*)())); 3651469Sbostic static ino_t ffs_ialloccg __P((struct inode *, int, daddr_t, int)); 3751469Sbostic static daddr_t ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int)); 3851469Sbostic 395375Smckusic /* 405375Smckusic * Allocate a block in the file system. 415375Smckusic * 425375Smckusic * The size of the requested block is given, which must be some 435375Smckusic * multiple of fs_fsize and <= fs_bsize. 445375Smckusic * A preference may be optionally specified. If a preference is given 455375Smckusic * the following hierarchy is used to allocate a block: 465375Smckusic * 1) allocate the requested block. 475375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 485375Smckusic * 3) allocate a block in the same cylinder group. 495375Smckusic * 4) quadradically rehash into other cylinder groups, until an 505375Smckusic * available block is located. 515375Smckusic * If no block preference is given the following heirarchy is used 525375Smckusic * to allocate a block: 535375Smckusic * 1) allocate a block in the cylinder group that contains the 545375Smckusic * inode for the file. 555375Smckusic * 2) quadradically rehash into other cylinder groups, until an 565375Smckusic * available block is located. 575375Smckusic */ 5853244Smckusick ffs_alloc(ip, lbn, bpref, size, cred, bnp) 594463Smckusic register struct inode *ip; 6039678Smckusick daddr_t lbn, bpref; 614359Smckusick int size; 6253244Smckusick struct ucred *cred; 6339678Smckusick daddr_t *bnp; 644359Smckusick { 654359Smckusick daddr_t bno; 664359Smckusick register struct fs *fs; 674463Smckusic register struct buf *bp; 6837735Smckusick int cg, error; 694359Smckusick 7039678Smckusick *bnp = 0; 715965Smckusic fs = ip->i_fs; 7253244Smckusick #ifdef DIAGNOSTIC 736716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 746716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 756716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 7651469Sbostic panic("ffs_alloc: bad size"); 776716Smckusick } 7853244Smckusick if (cred == NOCRED) 7953244Smckusick panic("ffs_alloc: missing credential\n"); 8053244Smckusick #endif /* DIAGNOSTIC */ 815322Smckusic if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 824359Smckusick goto nospace; 8341309Smckusick if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 844792Smckusic goto nospace; 857650Ssam #ifdef QUOTA 8641309Smckusick if (error = chkdq(ip, (long)btodb(size), cred, 0)) 8737735Smckusick return (error); 887483Skre #endif 894948Smckusic if (bpref >= fs->fs_size) 904948Smckusic bpref = 0; 914359Smckusick if (bpref == 0) 925377Smckusic cg = itog(fs, ip->i_number); 934359Smckusick else 945377Smckusic cg = dtog(fs, bpref); 9551469Sbostic bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size, 9651469Sbostic (u_long (*)())ffs_alloccg); 9739678Smckusick if (bno > 0) { 9839678Smckusick ip->i_blocks += btodb(size); 9939678Smckusick ip->i_flag |= IUPD|ICHG; 10039678Smckusick *bnp = bno; 10139678Smckusick return (0); 10239678Smckusick } 10345173Smckusick #ifdef QUOTA 10445173Smckusick /* 10545173Smckusick * Restore user's disk quota because allocation failed. 10645173Smckusick */ 10745173Smckusick (void) chkdq(ip, (long)-btodb(size), cred, FORCE); 10845173Smckusick #endif 1094359Smckusick nospace: 11051469Sbostic ffs_fserr(fs, cred->cr_uid, "file system full"); 1114359Smckusick uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 11237735Smckusick return (ENOSPC); 1134359Smckusick } 1144359Smckusick 1155375Smckusic /* 1165375Smckusic * Reallocate a fragment to a bigger size 1175375Smckusic * 1185375Smckusic * The number and size of the old block is given, and a preference 1195375Smckusic * and new size is also specified. The allocator attempts to extend 1205375Smckusic * the original block. Failing that, the regular block allocator is 1215375Smckusic * invoked to get an appropriate block. 1225375Smckusic */ 12353244Smckusick ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp) 1245965Smckusic register struct inode *ip; 12553059Smckusick daddr_t lbprev; 12639678Smckusick daddr_t bpref; 1274426Smckusic int osize, nsize; 12853244Smckusick struct ucred *cred; 12937735Smckusick struct buf **bpp; 1304426Smckusic { 1314426Smckusic register struct fs *fs; 13237735Smckusick struct buf *bp, *obp; 13345719Smckusick int cg, request, error; 13445719Smckusick daddr_t bprev, bno; 1354426Smckusic 13637735Smckusick *bpp = 0; 1375965Smckusic fs = ip->i_fs; 13853244Smckusick #ifdef DIAGNOSTIC 1395960Smckusic if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 1406716Smckusick (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 14151469Sbostic printf( 14251469Sbostic "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 1436716Smckusick ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 14451469Sbostic panic("ffs_realloccg: bad size"); 1456716Smckusick } 14653244Smckusick if (cred == NOCRED) 14753244Smckusick panic("ffs_realloccg: missing credential\n"); 14853244Smckusick #endif /* DIAGNOSTIC */ 14941309Smckusick if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 1504792Smckusic goto nospace; 15139678Smckusick if ((bprev = ip->i_db[lbprev]) == 0) { 1526716Smckusick printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 1536716Smckusick ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 15451469Sbostic panic("ffs_realloccg: bad bprev"); 1556716Smckusick } 15639678Smckusick /* 15739678Smckusick * Allocate the extra space in the buffer. 15839678Smckusick */ 15939678Smckusick if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) { 16039678Smckusick brelse(bp); 16139678Smckusick return (error); 16239678Smckusick } 16345173Smckusick #ifdef QUOTA 16445173Smckusick if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) { 16545173Smckusick brelse(bp); 16645173Smckusick return (error); 16745173Smckusick } 16845173Smckusick #endif 16939678Smckusick /* 17039678Smckusick * Check for extension in the existing location. 17139678Smckusick */ 1726294Smckusick cg = dtog(fs, bprev); 17351469Sbostic if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) { 17439887Smckusick if (bp->b_blkno != fsbtodb(fs, bno)) 17539678Smckusick panic("bad blockno"); 17639762Smckusick ip->i_blocks += btodb(nsize - osize); 17739762Smckusick ip->i_flag |= IUPD|ICHG; 17848948Smckusick allocbuf(bp, nsize); 17948948Smckusick bp->b_flags |= B_DONE; 18048948Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 18137735Smckusick *bpp = bp; 18237735Smckusick return (0); 1834463Smckusic } 18439678Smckusick /* 18539678Smckusick * Allocate a new disk location. 18639678Smckusick */ 1874948Smckusic if (bpref >= fs->fs_size) 1884948Smckusic bpref = 0; 18926253Skarels switch ((int)fs->fs_optim) { 19025256Smckusick case FS_OPTSPACE: 19125256Smckusick /* 19225256Smckusick * Allocate an exact sized fragment. Although this makes 19325256Smckusick * best use of space, we will waste time relocating it if 19425256Smckusick * the file continues to grow. If the fragmentation is 19525256Smckusick * less than half of the minimum free reserve, we choose 19625256Smckusick * to begin optimizing for time. 19725256Smckusick */ 19824698Smckusick request = nsize; 19925256Smckusick if (fs->fs_minfree < 5 || 20025256Smckusick fs->fs_cstotal.cs_nffree > 20125256Smckusick fs->fs_dsize * fs->fs_minfree / (2 * 100)) 20225256Smckusick break; 20325256Smckusick log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 20425256Smckusick fs->fs_fsmnt); 20525256Smckusick fs->fs_optim = FS_OPTTIME; 20625256Smckusick break; 20725256Smckusick case FS_OPTTIME: 20825256Smckusick /* 20951469Sbostic * At this point we have discovered a file that is trying to 21051469Sbostic * grow a small fragment to a larger fragment. To save time, 21151469Sbostic * we allocate a full sized block, then free the unused portion. 21251469Sbostic * If the file continues to grow, the `ffs_fragextend' call 21351469Sbostic * above will be able to grow it in place without further 21451469Sbostic * copying. If aberrant programs cause disk fragmentation to 21551469Sbostic * grow within 2% of the free reserve, we choose to begin 21651469Sbostic * optimizing for space. 21725256Smckusick */ 21824698Smckusick request = fs->fs_bsize; 21925256Smckusick if (fs->fs_cstotal.cs_nffree < 22025256Smckusick fs->fs_dsize * (fs->fs_minfree - 2) / 100) 22125256Smckusick break; 22225256Smckusick log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 22325256Smckusick fs->fs_fsmnt); 22425256Smckusick fs->fs_optim = FS_OPTSPACE; 22525256Smckusick break; 22625256Smckusick default: 22725256Smckusick printf("dev = 0x%x, optim = %d, fs = %s\n", 22825256Smckusick ip->i_dev, fs->fs_optim, fs->fs_fsmnt); 22951469Sbostic panic("ffs_realloccg: bad optim"); 23025256Smckusick /* NOTREACHED */ 23125256Smckusick } 23251469Sbostic bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request, 23351469Sbostic (u_long (*)())ffs_alloccg); 2346567Smckusic if (bno > 0) { 23545719Smckusick bp->b_blkno = fsbtodb(fs, bno); 23645719Smckusick (void) vnode_pager_uncache(ITOV(ip)); 23753059Smckusick ffs_blkfree(ip, bprev, (long)osize); 23824698Smckusick if (nsize < request) 23951469Sbostic ffs_blkfree(ip, bno + numfrags(fs, nsize), 24053059Smckusick (long)(request - nsize)); 24112643Ssam ip->i_blocks += btodb(nsize - osize); 24212643Ssam ip->i_flag |= IUPD|ICHG; 24348948Smckusick allocbuf(bp, nsize); 24448948Smckusick bp->b_flags |= B_DONE; 24548948Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 24637735Smckusick *bpp = bp; 24737735Smckusick return (0); 2484463Smckusic } 24945173Smckusick #ifdef QUOTA 25045173Smckusick /* 25145173Smckusick * Restore user's disk quota because allocation failed. 25245173Smckusick */ 25345173Smckusick (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE); 25445173Smckusick #endif 25539678Smckusick brelse(bp); 2564792Smckusic nospace: 2574463Smckusic /* 2584463Smckusic * no space available 2594463Smckusic */ 26051469Sbostic ffs_fserr(fs, cred->cr_uid, "file system full"); 2614426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 26237735Smckusick return (ENOSPC); 2634426Smckusic } 2644426Smckusic 2655375Smckusic /* 2665375Smckusic * Allocate an inode in the file system. 2675375Smckusic * 26851469Sbostic * If allocating a directory, use ffs_dirpref to select the inode. 26951469Sbostic * If allocating in a directory, the following hierarchy is followed: 27051469Sbostic * 1) allocate the preferred inode. 2715375Smckusic * 2) allocate an inode in the same cylinder group. 2725375Smckusic * 3) quadradically rehash into other cylinder groups, until an 2735375Smckusic * available inode is located. 2745375Smckusic * If no inode preference is given the following heirarchy is used 2755375Smckusic * to allocate an inode: 2765375Smckusic * 1) allocate an inode in cylinder group 0. 2775375Smckusic * 2) quadradically rehash into other cylinder groups, until an 2785375Smckusic * available inode is located. 2795375Smckusic */ 280*54653Smckusick ffs_valloc(ap) 281*54653Smckusick struct vop_valloc_args /* { 282*54653Smckusick struct vnode *a_pvp; 283*54653Smckusick int a_mode; 284*54653Smckusick struct ucred *a_cred; 285*54653Smckusick struct vnode **a_vpp; 286*54653Smckusick } */ *ap; 2874359Smckusick { 28853865Sheideman register struct vnode *pvp = ap->a_pvp; 28951541Smckusick register struct inode *pip; 2904359Smckusick register struct fs *fs; 2914359Smckusick register struct inode *ip; 292*54653Smckusick mode_t mode = ap->a_mode; 29351469Sbostic ino_t ino, ipref; 29437735Smckusick int cg, error; 2954359Smckusick 29653583Sheideman *ap->a_vpp = NULL; 29753865Sheideman pip = VTOI(pvp); 2985965Smckusic fs = pip->i_fs; 2994792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 3004359Smckusick goto noinodes; 30151469Sbostic 302*54653Smckusick if ((mode & IFMT) == IFDIR) 30351541Smckusick ipref = ffs_dirpref(fs); 30451469Sbostic else 30551469Sbostic ipref = pip->i_number; 3064948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 3074948Smckusic ipref = 0; 3085377Smckusic cg = itog(fs, ipref); 309*54653Smckusick ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_ialloccg); 3104359Smckusick if (ino == 0) 3114359Smckusick goto noinodes; 312*54653Smckusick error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp); 31337735Smckusick if (error) { 314*54653Smckusick VOP_VFREE(pvp, ino, mode); 31537735Smckusick return (error); 3164359Smckusick } 31753583Sheideman ip = VTOI(*ap->a_vpp); 3186716Smckusick if (ip->i_mode) { 319*54653Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 3206716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 32151541Smckusick panic("ffs_valloc: dup alloc"); 3226716Smckusick } 32312643Ssam if (ip->i_blocks) { /* XXX */ 32412643Ssam printf("free inode %s/%d had %d blocks\n", 32512643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 32612643Ssam ip->i_blocks = 0; 32712643Ssam } 32839516Smckusick ip->i_flags = 0; 32938255Smckusick /* 33038255Smckusick * Set up a new generation number for this inode. 33138255Smckusick */ 33238255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 33338255Smckusick nextgennumber = time.tv_sec; 33438255Smckusick ip->i_gen = nextgennumber; 33537735Smckusick return (0); 3364359Smckusick noinodes: 33753583Sheideman ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes"); 3386294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 33937735Smckusick return (ENOSPC); 3404359Smckusick } 3414359Smckusick 3424651Smckusic /* 3435375Smckusic * Find a cylinder to place a directory. 3445375Smckusic * 3455375Smckusic * The policy implemented by this algorithm is to select from 3465375Smckusic * among those cylinder groups with above the average number of 3475375Smckusic * free inodes, the one with the smallest number of directories. 3484651Smckusic */ 34951469Sbostic static ino_t 35051469Sbostic ffs_dirpref(fs) 3515965Smckusic register struct fs *fs; 3524359Smckusick { 3534651Smckusic int cg, minndir, mincg, avgifree; 3544359Smckusick 3554792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 3564651Smckusic minndir = fs->fs_ipg; 3574359Smckusick mincg = 0; 3584651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 3595322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 3605322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 3614359Smckusick mincg = cg; 3625322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 3634359Smckusick } 3649163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 3654359Smckusick } 3664359Smckusick 3674651Smckusic /* 3689163Ssam * Select the desired position for the next block in a file. The file is 3699163Ssam * logically divided into sections. The first section is composed of the 3709163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 3719163Ssam * 3729163Ssam * If no blocks have been allocated in the first section, the policy is to 3739163Ssam * request a block in the same cylinder group as the inode that describes 3749163Ssam * the file. If no blocks have been allocated in any other section, the 3759163Ssam * policy is to place the section in a cylinder group with a greater than 3769163Ssam * average number of free blocks. An appropriate cylinder group is found 37717696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of 37817696Smckusick * blocks is needed, the sweep begins in the cylinder group following the 37917696Smckusick * cylinder group from which the previous allocation was made. The sweep 38017696Smckusick * continues until a cylinder group with greater than the average number 38117696Smckusick * of free blocks is found. If the allocation is for the first block in an 38217696Smckusick * indirect block, the information on the previous allocation is unavailable; 38317696Smckusick * here a best guess is made based upon the logical block number being 38417696Smckusick * allocated. 3859163Ssam * 3869163Ssam * If a section is already partially allocated, the policy is to 3879163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 3889163Ssam * contiguous blocks and the beginning of the next is physically separated 3899163Ssam * so that the disk head will be in transit between them for at least 3909163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 3919163Ssam * schedule another I/O transfer. 3924651Smckusic */ 3935212Smckusic daddr_t 39451469Sbostic ffs_blkpref(ip, lbn, indx, bap) 3959163Ssam struct inode *ip; 3969163Ssam daddr_t lbn; 3979163Ssam int indx; 3989163Ssam daddr_t *bap; 3999163Ssam { 4005965Smckusic register struct fs *fs; 40117696Smckusick register int cg; 40217696Smckusick int avgbfree, startcg; 4039163Ssam daddr_t nextblk; 4044651Smckusic 4059163Ssam fs = ip->i_fs; 4069163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 4079163Ssam if (lbn < NDADDR) { 4089163Ssam cg = itog(fs, ip->i_number); 4095322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 4104651Smckusic } 4119163Ssam /* 4129163Ssam * Find a cylinder with greater than average number of 4139163Ssam * unused data blocks. 4149163Ssam */ 41517696Smckusick if (indx == 0 || bap[indx - 1] == 0) 41617696Smckusick startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; 41717696Smckusick else 41817696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1; 41917696Smckusick startcg %= fs->fs_ncg; 4209163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 42117696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++) 4229163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4239163Ssam fs->fs_cgrotor = cg; 4249163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4259163Ssam } 42617696Smckusick for (cg = 0; cg <= startcg; cg++) 4279163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4289163Ssam fs->fs_cgrotor = cg; 4299163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4309163Ssam } 4319163Ssam return (NULL); 4329163Ssam } 4339163Ssam /* 4349163Ssam * One or more previous blocks have been laid out. If less 4359163Ssam * than fs_maxcontig previous blocks are contiguous, the 4369163Ssam * next block is requested contiguously, otherwise it is 4379163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 4389163Ssam */ 4399163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 4409163Ssam if (indx > fs->fs_maxcontig && 44111638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 4429163Ssam != nextblk) 4439163Ssam return (nextblk); 4449163Ssam if (fs->fs_rotdelay != 0) 4459163Ssam /* 4469163Ssam * Here we convert ms of delay to frags as: 4479163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 4489163Ssam * ((sect/frag) * (ms/sec)) 4499163Ssam * then round up to the next block. 4509163Ssam */ 4519163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 4529163Ssam (NSPF(fs) * 1000), fs->fs_frag); 4539163Ssam return (nextblk); 4544651Smckusic } 4554651Smckusic 4565375Smckusic /* 4575375Smckusic * Implement the cylinder overflow algorithm. 4585375Smckusic * 4595375Smckusic * The policy implemented by this algorithm is: 4605375Smckusic * 1) allocate the block in its requested cylinder group. 4615375Smckusic * 2) quadradically rehash on the cylinder group number. 4625375Smckusic * 3) brute force search for a free block. 4635375Smckusic */ 4645212Smckusic /*VARARGS5*/ 46551469Sbostic static u_long 46651469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator) 4675965Smckusic struct inode *ip; 4684359Smckusick int cg; 4694359Smckusick long pref; 4704359Smckusick int size; /* size for data blocks, mode for inodes */ 4715212Smckusic u_long (*allocator)(); 4724359Smckusick { 4735965Smckusic register struct fs *fs; 4744359Smckusick long result; 4754359Smckusick int i, icg = cg; 4764359Smckusick 4775965Smckusic fs = ip->i_fs; 4784359Smckusick /* 4794359Smckusick * 1: preferred cylinder group 4804359Smckusick */ 4815965Smckusic result = (*allocator)(ip, cg, pref, size); 4824359Smckusick if (result) 4834359Smckusick return (result); 4844359Smckusick /* 4854359Smckusick * 2: quadratic rehash 4864359Smckusick */ 4874359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 4884359Smckusick cg += i; 4894359Smckusick if (cg >= fs->fs_ncg) 4904359Smckusick cg -= fs->fs_ncg; 4915965Smckusic result = (*allocator)(ip, cg, 0, size); 4924359Smckusick if (result) 4934359Smckusick return (result); 4944359Smckusick } 4954359Smckusick /* 4964359Smckusick * 3: brute force search 49710847Ssam * Note that we start at i == 2, since 0 was checked initially, 49810847Ssam * and 1 is always checked in the quadratic rehash. 4994359Smckusick */ 50010848Smckusick cg = (icg + 2) % fs->fs_ncg; 50110847Ssam for (i = 2; i < fs->fs_ncg; i++) { 5025965Smckusic result = (*allocator)(ip, cg, 0, size); 5034359Smckusick if (result) 5044359Smckusick return (result); 5054359Smckusick cg++; 5064359Smckusick if (cg == fs->fs_ncg) 5074359Smckusick cg = 0; 5084359Smckusick } 5096294Smckusick return (NULL); 5104359Smckusick } 5114359Smckusick 5125375Smckusic /* 5135375Smckusic * Determine whether a fragment can be extended. 5145375Smckusic * 5155375Smckusic * Check to see if the necessary fragments are available, and 5165375Smckusic * if they are, allocate them. 5175375Smckusic */ 51851469Sbostic static daddr_t 51951469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize) 5205965Smckusic struct inode *ip; 5214426Smckusic int cg; 5224463Smckusic long bprev; 5234426Smckusic int osize, nsize; 5244426Smckusic { 5255965Smckusic register struct fs *fs; 5264463Smckusic register struct cg *cgp; 52737735Smckusick struct buf *bp; 5284463Smckusic long bno; 5294463Smckusic int frags, bbase; 53037735Smckusick int i, error; 5314426Smckusic 5325965Smckusic fs = ip->i_fs; 53317224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 5346531Smckusick return (NULL); 5355960Smckusic frags = numfrags(fs, nsize); 53617224Smckusick bbase = fragnum(fs, bprev); 53717224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 53830749Skarels /* cannot extend across a block boundary */ 5396294Smckusick return (NULL); 5404463Smckusic } 54137735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 54238776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 54337735Smckusick if (error) { 54437735Smckusick brelse(bp); 54537735Smckusick return (NULL); 54637735Smckusick } 5476531Smckusick cgp = bp->b_un.b_cg; 54837735Smckusick if (!cg_chkmagic(cgp)) { 5495960Smckusic brelse(bp); 5506294Smckusick return (NULL); 5515960Smckusic } 5528105Sroot cgp->cg_time = time.tv_sec; 5535377Smckusic bno = dtogd(fs, bprev); 5545960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 55534143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) { 5565361Smckusic brelse(bp); 5576294Smckusick return (NULL); 5585361Smckusic } 5595361Smckusic /* 5605361Smckusic * the current fragment can be extended 5615361Smckusic * deduct the count on fragment being extended into 5625361Smckusic * increase the count on the remaining fragment (if any) 5635361Smckusic * allocate the extended piece 5645361Smckusic */ 5655361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 56634143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) 5674463Smckusic break; 5685960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 5695361Smckusic if (i != frags) 5705361Smckusic cgp->cg_frsum[i - frags]++; 5715960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 57234143Smckusick clrbit(cg_blksfree(cgp), bno + i); 5735361Smckusic cgp->cg_cs.cs_nffree--; 5745361Smckusic fs->fs_cstotal.cs_nffree--; 5755361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 5764463Smckusic } 57750893Smckusick fs->fs_fmod = 1; 5785361Smckusic bdwrite(bp); 5795361Smckusic return (bprev); 5804426Smckusic } 5814426Smckusic 5825375Smckusic /* 5835375Smckusic * Determine whether a block can be allocated. 5845375Smckusic * 5855375Smckusic * Check to see if a block of the apprpriate size is available, 5865375Smckusic * and if it is, allocate it. 5875375Smckusic */ 58851779Smarc static daddr_t 58951469Sbostic ffs_alloccg(ip, cg, bpref, size) 5905965Smckusic struct inode *ip; 5914359Smckusick int cg; 5924359Smckusick daddr_t bpref; 5934359Smckusick int size; 5944359Smckusick { 5955965Smckusic register struct fs *fs; 5964463Smckusic register struct cg *cgp; 59737735Smckusick struct buf *bp; 5984463Smckusic register int i; 59937735Smckusick int error, bno, frags, allocsiz; 6004359Smckusick 6015965Smckusic fs = ip->i_fs; 6025322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 6036294Smckusick return (NULL); 60437735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 60538776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 60637735Smckusick if (error) { 60737735Smckusick brelse(bp); 60837735Smckusick return (NULL); 60937735Smckusick } 6106531Smckusick cgp = bp->b_un.b_cg; 61137735Smckusick if (!cg_chkmagic(cgp) || 61215950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 6135960Smckusic brelse(bp); 6146294Smckusick return (NULL); 6155960Smckusic } 6168105Sroot cgp->cg_time = time.tv_sec; 6175322Smckusic if (size == fs->fs_bsize) { 61851469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref); 6194463Smckusic bdwrite(bp); 6204463Smckusic return (bno); 6214463Smckusic } 6224463Smckusic /* 6234463Smckusic * check to see if any fragments are already available 6244463Smckusic * allocsiz is the size which will be allocated, hacking 6254463Smckusic * it down to a smaller size if necessary 6264463Smckusic */ 6275960Smckusic frags = numfrags(fs, size); 6285322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 6294463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 6304463Smckusic break; 6315322Smckusic if (allocsiz == fs->fs_frag) { 6324463Smckusic /* 6334463Smckusic * no fragments were available, so a block will be 6344463Smckusic * allocated, and hacked up 6354463Smckusic */ 6364792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 6374463Smckusic brelse(bp); 6386294Smckusick return (NULL); 6394463Smckusic } 64051469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref); 6415377Smckusic bpref = dtogd(fs, bno); 6425322Smckusic for (i = frags; i < fs->fs_frag; i++) 64334143Smckusick setbit(cg_blksfree(cgp), bpref + i); 6445322Smckusic i = fs->fs_frag - frags; 6454792Smckusic cgp->cg_cs.cs_nffree += i; 6464792Smckusic fs->fs_cstotal.cs_nffree += i; 6475322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 64850893Smckusick fs->fs_fmod = 1; 6494463Smckusic cgp->cg_frsum[i]++; 6504463Smckusic bdwrite(bp); 6514463Smckusic return (bno); 6524463Smckusic } 65351469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); 65415950Smckusick if (bno < 0) { 65515950Smckusick brelse(bp); 6566294Smckusick return (NULL); 65715950Smckusick } 6584463Smckusic for (i = 0; i < frags; i++) 65934143Smckusick clrbit(cg_blksfree(cgp), bno + i); 6604792Smckusic cgp->cg_cs.cs_nffree -= frags; 6614792Smckusic fs->fs_cstotal.cs_nffree -= frags; 6625322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 66350893Smckusick fs->fs_fmod = 1; 6644463Smckusic cgp->cg_frsum[allocsiz]--; 6654463Smckusic if (frags != allocsiz) 6664463Smckusic cgp->cg_frsum[allocsiz - frags]++; 6674463Smckusic bdwrite(bp); 6684463Smckusic return (cg * fs->fs_fpg + bno); 6694463Smckusic } 6704463Smckusic 6715375Smckusic /* 6725375Smckusic * Allocate a block in a cylinder group. 6735375Smckusic * 6745375Smckusic * This algorithm implements the following policy: 6755375Smckusic * 1) allocate the requested block. 6765375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 6775375Smckusic * 3) allocate the next available block on the block rotor for the 6785375Smckusic * specified cylinder group. 6795375Smckusic * Note that this routine only allocates fs_bsize blocks; these 6805375Smckusic * blocks may be fragmented by the routine that allocates them. 6815375Smckusic */ 68251469Sbostic static daddr_t 68351469Sbostic ffs_alloccgblk(fs, cgp, bpref) 6845965Smckusic register struct fs *fs; 6854463Smckusic register struct cg *cgp; 6864463Smckusic daddr_t bpref; 6874463Smckusic { 6884651Smckusic daddr_t bno; 6896294Smckusick int cylno, pos, delta; 6904651Smckusic short *cylbp; 6915361Smckusic register int i; 6924463Smckusic 6934651Smckusic if (bpref == 0) { 6944651Smckusic bpref = cgp->cg_rotor; 6955361Smckusic goto norot; 6965361Smckusic } 69717224Smckusick bpref = blknum(fs, bpref); 6985377Smckusic bpref = dtogd(fs, bpref); 6995361Smckusic /* 7005361Smckusic * if the requested block is available, use it 7015361Smckusic */ 70251469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 7035361Smckusic bno = bpref; 7045361Smckusic goto gotit; 7055361Smckusic } 7065361Smckusic /* 7075361Smckusic * check for a block available on the same cylinder 7085361Smckusic */ 7095361Smckusic cylno = cbtocylno(fs, bpref); 71034143Smckusick if (cg_blktot(cgp)[cylno] == 0) 7115375Smckusic goto norot; 7125375Smckusic if (fs->fs_cpc == 0) { 7135375Smckusic /* 7145375Smckusic * block layout info is not available, so just have 7155375Smckusic * to take any block in this cylinder. 7165375Smckusic */ 7175375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 7185375Smckusic goto norot; 7195375Smckusic } 7205375Smckusic /* 7215361Smckusic * check the summary information to see if a block is 7225361Smckusic * available in the requested cylinder starting at the 7239163Ssam * requested rotational position and proceeding around. 7245361Smckusic */ 72534143Smckusick cylbp = cg_blks(fs, cgp, cylno); 7269163Ssam pos = cbtorpos(fs, bpref); 72734143Smckusick for (i = pos; i < fs->fs_nrpos; i++) 7285361Smckusic if (cylbp[i] > 0) 7295361Smckusic break; 73034143Smckusick if (i == fs->fs_nrpos) 7315361Smckusic for (i = 0; i < pos; i++) 7325361Smckusic if (cylbp[i] > 0) 7335361Smckusic break; 7345361Smckusic if (cylbp[i] > 0) { 7354651Smckusic /* 7365361Smckusic * found a rotational position, now find the actual 7375361Smckusic * block. A panic if none is actually there. 7384651Smckusic */ 7395361Smckusic pos = cylno % fs->fs_cpc; 7405361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 74134143Smckusick if (fs_postbl(fs, pos)[i] == -1) { 7426716Smckusick printf("pos = %d, i = %d, fs = %s\n", 7436716Smckusick pos, i, fs->fs_fsmnt); 74451469Sbostic panic("ffs_alloccgblk: cyl groups corrupted"); 7456716Smckusick } 74634143Smckusick for (i = fs_postbl(fs, pos)[i];; ) { 74751469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) { 74811638Ssam bno = blkstofrags(fs, (bno + i)); 7495361Smckusic goto gotit; 7505361Smckusic } 75134143Smckusick delta = fs_rotbl(fs)[i]; 75234143Smckusick if (delta <= 0 || 75334143Smckusick delta + i > fragstoblks(fs, fs->fs_fpg)) 7544651Smckusic break; 7556294Smckusick i += delta; 7564651Smckusic } 7576716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 75851469Sbostic panic("ffs_alloccgblk: can't find blk in cyl"); 7594359Smckusick } 7605361Smckusic norot: 7615361Smckusic /* 7625361Smckusic * no blocks in the requested cylinder, so take next 7635361Smckusic * available one in this cylinder group. 7645361Smckusic */ 76551469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 7666567Smckusic if (bno < 0) 7676294Smckusick return (NULL); 7684651Smckusic cgp->cg_rotor = bno; 7694359Smckusick gotit: 77051469Sbostic ffs_clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno)); 7714792Smckusic cgp->cg_cs.cs_nbfree--; 7724792Smckusic fs->fs_cstotal.cs_nbfree--; 7735322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 7745375Smckusic cylno = cbtocylno(fs, bno); 77534143Smckusick cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 77634143Smckusick cg_blktot(cgp)[cylno]--; 77750893Smckusick fs->fs_fmod = 1; 7784651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 7794359Smckusick } 78034143Smckusick 7815375Smckusic /* 7825375Smckusic * Determine whether an inode can be allocated. 7835375Smckusic * 7845375Smckusic * Check to see if an inode is available, and if it is, 7855375Smckusic * allocate it using the following policy: 7865375Smckusic * 1) allocate the requested inode. 7875375Smckusic * 2) allocate the next available inode after the requested 7885375Smckusic * inode in the specified cylinder group. 7895375Smckusic */ 79051469Sbostic static ino_t 79151469Sbostic ffs_ialloccg(ip, cg, ipref, mode) 7925965Smckusic struct inode *ip; 7934359Smckusick int cg; 7944359Smckusick daddr_t ipref; 7954359Smckusick int mode; 7964359Smckusick { 7975965Smckusic register struct fs *fs; 7984463Smckusic register struct cg *cgp; 79916784Smckusick struct buf *bp; 80037735Smckusick int error, start, len, loc, map, i; 8014359Smckusick 8025965Smckusic fs = ip->i_fs; 8035322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 8046294Smckusick return (NULL); 80537735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 80638776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 80737735Smckusick if (error) { 80837735Smckusick brelse(bp); 80937735Smckusick return (NULL); 81037735Smckusick } 8116531Smckusick cgp = bp->b_un.b_cg; 81237735Smckusick if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 8135960Smckusic brelse(bp); 8146294Smckusick return (NULL); 8155960Smckusic } 8168105Sroot cgp->cg_time = time.tv_sec; 8174359Smckusick if (ipref) { 8184359Smckusick ipref %= fs->fs_ipg; 81934143Smckusick if (isclr(cg_inosused(cgp), ipref)) 8204359Smckusick goto gotit; 82116784Smckusick } 82216784Smckusick start = cgp->cg_irotor / NBBY; 82316784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 82434143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 82516784Smckusick if (loc == 0) { 82617697Smckusick len = start + 1; 82717697Smckusick start = 0; 82834143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 82917697Smckusick if (loc == 0) { 83017697Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 83117697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 83251469Sbostic panic("ffs_ialloccg: map corrupted"); 83317697Smckusick /* NOTREACHED */ 83417697Smckusick } 83516784Smckusick } 83616784Smckusick i = start + len - loc; 83734143Smckusick map = cg_inosused(cgp)[i]; 83816784Smckusick ipref = i * NBBY; 83916784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 84016784Smckusick if ((map & i) == 0) { 8414359Smckusick cgp->cg_irotor = ipref; 8424359Smckusick goto gotit; 8434359Smckusick } 8444359Smckusick } 84516784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 84651469Sbostic panic("ffs_ialloccg: block not in map"); 84716784Smckusick /* NOTREACHED */ 8484359Smckusick gotit: 84934143Smckusick setbit(cg_inosused(cgp), ipref); 8504792Smckusic cgp->cg_cs.cs_nifree--; 8514792Smckusic fs->fs_cstotal.cs_nifree--; 8525322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 85350893Smckusick fs->fs_fmod = 1; 8544359Smckusick if ((mode & IFMT) == IFDIR) { 8554792Smckusic cgp->cg_cs.cs_ndir++; 8564792Smckusic fs->fs_cstotal.cs_ndir++; 8575322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 8584359Smckusick } 8594359Smckusick bdwrite(bp); 8604359Smckusick return (cg * fs->fs_ipg + ipref); 8614359Smckusick } 8624359Smckusick 8635375Smckusic /* 8645375Smckusic * Free a block or fragment. 8655375Smckusic * 8665375Smckusic * The specified block or fragment is placed back in the 8675375Smckusic * free map. If a fragment is deallocated, a possible 8685375Smckusic * block reassembly is checked. 8695375Smckusic */ 87051469Sbostic ffs_blkfree(ip, bno, size) 8715965Smckusic register struct inode *ip; 8724359Smckusick daddr_t bno; 87353059Smckusick long size; 8744359Smckusick { 8754359Smckusick register struct fs *fs; 8764359Smckusick register struct cg *cgp; 87737735Smckusick struct buf *bp; 87837735Smckusick int error, cg, blk, frags, bbase; 8794463Smckusic register int i; 8804359Smckusick 8815965Smckusic fs = ip->i_fs; 8826716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 8836716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 8846716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 88531402Smckusick panic("blkfree: bad size"); 8866716Smckusick } 8875377Smckusic cg = dtog(fs, bno); 88842318Smckusick if ((unsigned)bno >= fs->fs_size) { 8896567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 89053244Smckusick ffs_fserr(fs, ip->i_uid, "bad block"); 8914359Smckusick return; 8926567Smckusic } 89337735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 89438776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 89537735Smckusick if (error) { 89637735Smckusick brelse(bp); 89737735Smckusick return; 89837735Smckusick } 8996531Smckusick cgp = bp->b_un.b_cg; 90037735Smckusick if (!cg_chkmagic(cgp)) { 9015960Smckusic brelse(bp); 9024359Smckusick return; 9035960Smckusic } 9048105Sroot cgp->cg_time = time.tv_sec; 9055377Smckusic bno = dtogd(fs, bno); 9065322Smckusic if (size == fs->fs_bsize) { 90751469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) { 9086716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9096716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 91031402Smckusick panic("blkfree: freeing free block"); 9116567Smckusic } 91251469Sbostic ffs_setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno)); 9134792Smckusic cgp->cg_cs.cs_nbfree++; 9144792Smckusic fs->fs_cstotal.cs_nbfree++; 9155322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9165375Smckusic i = cbtocylno(fs, bno); 91734143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; 91834143Smckusick cg_blktot(cgp)[i]++; 9194426Smckusic } else { 92017224Smckusick bbase = bno - fragnum(fs, bno); 9214463Smckusic /* 9224463Smckusic * decrement the counts associated with the old frags 9234463Smckusic */ 92434143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 92551469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 9264463Smckusic /* 9274463Smckusic * deallocate the fragment 9284463Smckusic */ 9295960Smckusic frags = numfrags(fs, size); 9304463Smckusic for (i = 0; i < frags; i++) { 93134143Smckusick if (isset(cg_blksfree(cgp), bno + i)) { 9326716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9336716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 93431402Smckusick panic("blkfree: freeing free frag"); 9356716Smckusick } 93634143Smckusick setbit(cg_blksfree(cgp), bno + i); 9374426Smckusic } 9386294Smckusick cgp->cg_cs.cs_nffree += i; 9396294Smckusick fs->fs_cstotal.cs_nffree += i; 9406294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 9414463Smckusic /* 9424463Smckusic * add back in counts associated with the new frags 9434463Smckusic */ 94434143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 94551469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 9464463Smckusic /* 9474463Smckusic * if a complete block has been reassembled, account for it 9484463Smckusic */ 94951469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), 95034476Smckusick (daddr_t)fragstoblks(fs, bbase))) { 9515322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 9525322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 9535322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 9544792Smckusic cgp->cg_cs.cs_nbfree++; 9554792Smckusic fs->fs_cstotal.cs_nbfree++; 9565322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9575375Smckusic i = cbtocylno(fs, bbase); 95834143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 95934143Smckusick cg_blktot(cgp)[i]++; 9604426Smckusic } 9614426Smckusic } 96250893Smckusick fs->fs_fmod = 1; 9634359Smckusick bdwrite(bp); 9644359Smckusick } 9654359Smckusick 9665375Smckusic /* 9675375Smckusic * Free an inode. 9685375Smckusic * 9695375Smckusic * The specified inode is placed back in the free map. 9705375Smckusic */ 97153577Sheideman int 972*54653Smckusick ffs_vfree(ap) 973*54653Smckusick struct vop_vfree_args /* { 974*54653Smckusick struct vnode *a_pvp; 975*54653Smckusick ino_t a_ino; 976*54653Smckusick int a_mode; 977*54653Smckusick } */ *ap; 9784359Smckusick { 9794359Smckusick register struct fs *fs; 9804359Smckusick register struct cg *cgp; 98151541Smckusick register struct inode *pip; 982*54653Smckusick ino_t ino = ap->a_ino; 98337735Smckusick struct buf *bp; 98437735Smckusick int error, cg; 9854359Smckusick 98653583Sheideman pip = VTOI(ap->a_pvp); 98751469Sbostic fs = pip->i_fs; 988*54653Smckusick if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg) 989*54653Smckusick panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n", 990*54653Smckusick pip->i_dev, ino, fs->fs_fsmnt); 991*54653Smckusick cg = itog(fs, ino); 99251469Sbostic error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 99338776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 99437735Smckusick if (error) { 99537735Smckusick brelse(bp); 99653577Sheideman return (0); 99737735Smckusick } 9986531Smckusick cgp = bp->b_un.b_cg; 99937735Smckusick if (!cg_chkmagic(cgp)) { 10005960Smckusic brelse(bp); 100153577Sheideman return (0); 10025960Smckusic } 10038105Sroot cgp->cg_time = time.tv_sec; 1004*54653Smckusick ino %= fs->fs_ipg; 1005*54653Smckusick if (isclr(cg_inosused(cgp), ino)) { 1006*54653Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 1007*54653Smckusick pip->i_dev, ino, fs->fs_fsmnt); 100848057Smckusick if (fs->fs_ronly == 0) 100948057Smckusick panic("ifree: freeing free inode"); 10106716Smckusick } 1011*54653Smckusick clrbit(cg_inosused(cgp), ino); 1012*54653Smckusick if (ino < cgp->cg_irotor) 1013*54653Smckusick cgp->cg_irotor = ino; 10144792Smckusic cgp->cg_cs.cs_nifree++; 10154792Smckusic fs->fs_cstotal.cs_nifree++; 10165322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 101753583Sheideman if ((ap->a_mode & IFMT) == IFDIR) { 10184792Smckusic cgp->cg_cs.cs_ndir--; 10194792Smckusic fs->fs_cstotal.cs_ndir--; 10205322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 10214359Smckusick } 102250893Smckusick fs->fs_fmod = 1; 10234359Smckusick bdwrite(bp); 102453577Sheideman return (0); 10254359Smckusick } 10264359Smckusick 10274463Smckusic /* 10285375Smckusic * Find a block of the specified size in the specified cylinder group. 10295375Smckusic * 10304651Smckusic * It is a panic if a request is made to find a block if none are 10314651Smckusic * available. 10324651Smckusic */ 103351469Sbostic static daddr_t 103451469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz) 10354651Smckusic register struct fs *fs; 10364651Smckusic register struct cg *cgp; 10374651Smckusic daddr_t bpref; 10384651Smckusic int allocsiz; 10394651Smckusic { 10404651Smckusic daddr_t bno; 10414651Smckusic int start, len, loc, i; 10424651Smckusic int blk, field, subfield, pos; 10434651Smckusic 10444651Smckusic /* 10454651Smckusic * find the fragment by searching through the free block 10464651Smckusic * map for an appropriate bit pattern 10474651Smckusic */ 10484651Smckusic if (bpref) 10495377Smckusic start = dtogd(fs, bpref) / NBBY; 10504651Smckusic else 10514651Smckusic start = cgp->cg_frotor / NBBY; 10525398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 105334476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start], 105434476Smckusick (u_char *)fragtbl[fs->fs_frag], 105534476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 10564651Smckusic if (loc == 0) { 10576531Smckusick len = start + 1; 10586531Smckusick start = 0; 105934476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0], 106034476Smckusick (u_char *)fragtbl[fs->fs_frag], 106134476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 106216784Smckusick if (loc == 0) { 106316784Smckusick printf("start = %d, len = %d, fs = %s\n", 106416784Smckusick start, len, fs->fs_fsmnt); 106551469Sbostic panic("ffs_alloccg: map corrupted"); 106617697Smckusick /* NOTREACHED */ 106716784Smckusick } 10684651Smckusic } 10694651Smckusic bno = (start + len - loc) * NBBY; 10704651Smckusic cgp->cg_frotor = bno; 10714651Smckusic /* 10724651Smckusic * found the byte in the map 10734651Smckusic * sift through the bits to find the selected frag 10744651Smckusic */ 10756294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 107634143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bno); 10774651Smckusic blk <<= 1; 10784651Smckusic field = around[allocsiz]; 10794651Smckusic subfield = inside[allocsiz]; 10805322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 10816294Smckusick if ((blk & field) == subfield) 10826294Smckusick return (bno + pos); 10834651Smckusic field <<= 1; 10844651Smckusic subfield <<= 1; 10854651Smckusic } 10864651Smckusic } 10876716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 108851469Sbostic panic("ffs_alloccg: block not in map"); 10896531Smckusick return (-1); 10904651Smckusic } 10914651Smckusic 10924651Smckusic /* 10935375Smckusic * Fserr prints the name of a file system with an error diagnostic. 10945375Smckusic * 10955375Smckusic * The form of the error message is: 10964359Smckusick * fs: error message 10974359Smckusick */ 109851469Sbostic static void 109951469Sbostic ffs_fserr(fs, uid, cp) 11004359Smckusick struct fs *fs; 110151469Sbostic u_int uid; 11024359Smckusick char *cp; 11034359Smckusick { 11044359Smckusick 110542318Smckusick log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp); 11064359Smckusick } 1107