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*56665Smargo * @(#)ffs_alloc.c 7.39 (Berkeley) 11/02/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> 1554653Smckusick #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 */ 28054653Smckusick ffs_valloc(ap) 28154653Smckusick struct vop_valloc_args /* { 28254653Smckusick struct vnode *a_pvp; 28354653Smckusick int a_mode; 28454653Smckusick struct ucred *a_cred; 28554653Smckusick struct vnode **a_vpp; 28654653Smckusick } */ *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; 29254653Smckusick 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 30254653Smckusick 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); 30954653Smckusick ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_ialloccg); 3104359Smckusick if (ino == 0) 3114359Smckusick goto noinodes; 31254653Smckusick error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp); 31337735Smckusick if (error) { 31454653Smckusick VOP_VFREE(pvp, ino, mode); 31537735Smckusick return (error); 3164359Smckusick } 31753583Sheideman ip = VTOI(*ap->a_vpp); 3186716Smckusick if (ip->i_mode) { 31954653Smckusick 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; 440*56665Smargo if (indx < fs->fs_maxcontig || bap[indx - fs->fs_maxcontig] + 441*56665Smargo blkstofrags(fs, fs->fs_maxcontig) != nextblk) 4429163Ssam return (nextblk); 4439163Ssam if (fs->fs_rotdelay != 0) 4449163Ssam /* 4459163Ssam * Here we convert ms of delay to frags as: 4469163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 4479163Ssam * ((sect/frag) * (ms/sec)) 4489163Ssam * then round up to the next block. 4499163Ssam */ 4509163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 4519163Ssam (NSPF(fs) * 1000), fs->fs_frag); 4529163Ssam return (nextblk); 4534651Smckusic } 4544651Smckusic 4555375Smckusic /* 4565375Smckusic * Implement the cylinder overflow algorithm. 4575375Smckusic * 4585375Smckusic * The policy implemented by this algorithm is: 4595375Smckusic * 1) allocate the block in its requested cylinder group. 4605375Smckusic * 2) quadradically rehash on the cylinder group number. 4615375Smckusic * 3) brute force search for a free block. 4625375Smckusic */ 4635212Smckusic /*VARARGS5*/ 46451469Sbostic static u_long 46551469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator) 4665965Smckusic struct inode *ip; 4674359Smckusick int cg; 4684359Smckusick long pref; 4694359Smckusick int size; /* size for data blocks, mode for inodes */ 4705212Smckusic u_long (*allocator)(); 4714359Smckusick { 4725965Smckusic register struct fs *fs; 4734359Smckusick long result; 4744359Smckusick int i, icg = cg; 4754359Smckusick 4765965Smckusic fs = ip->i_fs; 4774359Smckusick /* 4784359Smckusick * 1: preferred cylinder group 4794359Smckusick */ 4805965Smckusic result = (*allocator)(ip, cg, pref, size); 4814359Smckusick if (result) 4824359Smckusick return (result); 4834359Smckusick /* 4844359Smckusick * 2: quadratic rehash 4854359Smckusick */ 4864359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 4874359Smckusick cg += i; 4884359Smckusick if (cg >= fs->fs_ncg) 4894359Smckusick cg -= fs->fs_ncg; 4905965Smckusic result = (*allocator)(ip, cg, 0, size); 4914359Smckusick if (result) 4924359Smckusick return (result); 4934359Smckusick } 4944359Smckusick /* 4954359Smckusick * 3: brute force search 49610847Ssam * Note that we start at i == 2, since 0 was checked initially, 49710847Ssam * and 1 is always checked in the quadratic rehash. 4984359Smckusick */ 49910848Smckusick cg = (icg + 2) % fs->fs_ncg; 50010847Ssam for (i = 2; i < fs->fs_ncg; i++) { 5015965Smckusic result = (*allocator)(ip, cg, 0, size); 5024359Smckusick if (result) 5034359Smckusick return (result); 5044359Smckusick cg++; 5054359Smckusick if (cg == fs->fs_ncg) 5064359Smckusick cg = 0; 5074359Smckusick } 5086294Smckusick return (NULL); 5094359Smckusick } 5104359Smckusick 5115375Smckusic /* 5125375Smckusic * Determine whether a fragment can be extended. 5135375Smckusic * 5145375Smckusic * Check to see if the necessary fragments are available, and 5155375Smckusic * if they are, allocate them. 5165375Smckusic */ 51751469Sbostic static daddr_t 51851469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize) 5195965Smckusic struct inode *ip; 5204426Smckusic int cg; 5214463Smckusic long bprev; 5224426Smckusic int osize, nsize; 5234426Smckusic { 5245965Smckusic register struct fs *fs; 5254463Smckusic register struct cg *cgp; 52637735Smckusick struct buf *bp; 5274463Smckusic long bno; 5284463Smckusic int frags, bbase; 52937735Smckusick int i, error; 5304426Smckusic 5315965Smckusic fs = ip->i_fs; 53217224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 5336531Smckusick return (NULL); 5345960Smckusic frags = numfrags(fs, nsize); 53517224Smckusick bbase = fragnum(fs, bprev); 53617224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 53730749Skarels /* cannot extend across a block boundary */ 5386294Smckusick return (NULL); 5394463Smckusic } 54037735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 54138776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 54237735Smckusick if (error) { 54337735Smckusick brelse(bp); 54437735Smckusick return (NULL); 54537735Smckusick } 5466531Smckusick cgp = bp->b_un.b_cg; 54737735Smckusick if (!cg_chkmagic(cgp)) { 5485960Smckusic brelse(bp); 5496294Smckusick return (NULL); 5505960Smckusic } 5518105Sroot cgp->cg_time = time.tv_sec; 5525377Smckusic bno = dtogd(fs, bprev); 5535960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 55434143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) { 5555361Smckusic brelse(bp); 5566294Smckusick return (NULL); 5575361Smckusic } 5585361Smckusic /* 5595361Smckusic * the current fragment can be extended 5605361Smckusic * deduct the count on fragment being extended into 5615361Smckusic * increase the count on the remaining fragment (if any) 5625361Smckusic * allocate the extended piece 5635361Smckusic */ 5645361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 56534143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) 5664463Smckusic break; 5675960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 5685361Smckusic if (i != frags) 5695361Smckusic cgp->cg_frsum[i - frags]++; 5705960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 57134143Smckusick clrbit(cg_blksfree(cgp), bno + i); 5725361Smckusic cgp->cg_cs.cs_nffree--; 5735361Smckusic fs->fs_cstotal.cs_nffree--; 5745361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 5754463Smckusic } 57650893Smckusick fs->fs_fmod = 1; 5775361Smckusic bdwrite(bp); 5785361Smckusic return (bprev); 5794426Smckusic } 5804426Smckusic 5815375Smckusic /* 5825375Smckusic * Determine whether a block can be allocated. 5835375Smckusic * 5845375Smckusic * Check to see if a block of the apprpriate size is available, 5855375Smckusic * and if it is, allocate it. 5865375Smckusic */ 58751779Smarc static daddr_t 58851469Sbostic ffs_alloccg(ip, cg, bpref, size) 5895965Smckusic struct inode *ip; 5904359Smckusick int cg; 5914359Smckusick daddr_t bpref; 5924359Smckusick int size; 5934359Smckusick { 5945965Smckusic register struct fs *fs; 5954463Smckusic register struct cg *cgp; 59637735Smckusick struct buf *bp; 5974463Smckusic register int i; 59837735Smckusick int error, bno, frags, allocsiz; 5994359Smckusick 6005965Smckusic fs = ip->i_fs; 6015322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 6026294Smckusick return (NULL); 60337735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 60438776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 60537735Smckusick if (error) { 60637735Smckusick brelse(bp); 60737735Smckusick return (NULL); 60837735Smckusick } 6096531Smckusick cgp = bp->b_un.b_cg; 61037735Smckusick if (!cg_chkmagic(cgp) || 61115950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 6125960Smckusic brelse(bp); 6136294Smckusick return (NULL); 6145960Smckusic } 6158105Sroot cgp->cg_time = time.tv_sec; 6165322Smckusic if (size == fs->fs_bsize) { 61751469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref); 6184463Smckusic bdwrite(bp); 6194463Smckusic return (bno); 6204463Smckusic } 6214463Smckusic /* 6224463Smckusic * check to see if any fragments are already available 6234463Smckusic * allocsiz is the size which will be allocated, hacking 6244463Smckusic * it down to a smaller size if necessary 6254463Smckusic */ 6265960Smckusic frags = numfrags(fs, size); 6275322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 6284463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 6294463Smckusic break; 6305322Smckusic if (allocsiz == fs->fs_frag) { 6314463Smckusic /* 6324463Smckusic * no fragments were available, so a block will be 6334463Smckusic * allocated, and hacked up 6344463Smckusic */ 6354792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 6364463Smckusic brelse(bp); 6376294Smckusick return (NULL); 6384463Smckusic } 63951469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref); 6405377Smckusic bpref = dtogd(fs, bno); 6415322Smckusic for (i = frags; i < fs->fs_frag; i++) 64234143Smckusick setbit(cg_blksfree(cgp), bpref + i); 6435322Smckusic i = fs->fs_frag - frags; 6444792Smckusic cgp->cg_cs.cs_nffree += i; 6454792Smckusic fs->fs_cstotal.cs_nffree += i; 6465322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 64750893Smckusick fs->fs_fmod = 1; 6484463Smckusic cgp->cg_frsum[i]++; 6494463Smckusic bdwrite(bp); 6504463Smckusic return (bno); 6514463Smckusic } 65251469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); 65315950Smckusick if (bno < 0) { 65415950Smckusick brelse(bp); 6556294Smckusick return (NULL); 65615950Smckusick } 6574463Smckusic for (i = 0; i < frags; i++) 65834143Smckusick clrbit(cg_blksfree(cgp), bno + i); 6594792Smckusic cgp->cg_cs.cs_nffree -= frags; 6604792Smckusic fs->fs_cstotal.cs_nffree -= frags; 6615322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 66250893Smckusick fs->fs_fmod = 1; 6634463Smckusic cgp->cg_frsum[allocsiz]--; 6644463Smckusic if (frags != allocsiz) 6654463Smckusic cgp->cg_frsum[allocsiz - frags]++; 6664463Smckusic bdwrite(bp); 6674463Smckusic return (cg * fs->fs_fpg + bno); 6684463Smckusic } 6694463Smckusic 6705375Smckusic /* 6715375Smckusic * Allocate a block in a cylinder group. 6725375Smckusic * 6735375Smckusic * This algorithm implements the following policy: 6745375Smckusic * 1) allocate the requested block. 6755375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 6765375Smckusic * 3) allocate the next available block on the block rotor for the 6775375Smckusic * specified cylinder group. 6785375Smckusic * Note that this routine only allocates fs_bsize blocks; these 6795375Smckusic * blocks may be fragmented by the routine that allocates them. 6805375Smckusic */ 68151469Sbostic static daddr_t 68251469Sbostic ffs_alloccgblk(fs, cgp, bpref) 6835965Smckusic register struct fs *fs; 6844463Smckusic register struct cg *cgp; 6854463Smckusic daddr_t bpref; 6864463Smckusic { 6874651Smckusic daddr_t bno; 6886294Smckusick int cylno, pos, delta; 6894651Smckusic short *cylbp; 6905361Smckusic register int i; 6914463Smckusic 6924651Smckusic if (bpref == 0) { 6934651Smckusic bpref = cgp->cg_rotor; 6945361Smckusic goto norot; 6955361Smckusic } 69617224Smckusick bpref = blknum(fs, bpref); 6975377Smckusic bpref = dtogd(fs, bpref); 6985361Smckusic /* 6995361Smckusic * if the requested block is available, use it 7005361Smckusic */ 70151469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 7025361Smckusic bno = bpref; 7035361Smckusic goto gotit; 7045361Smckusic } 7055361Smckusic /* 7065361Smckusic * check for a block available on the same cylinder 7075361Smckusic */ 7085361Smckusic cylno = cbtocylno(fs, bpref); 70934143Smckusick if (cg_blktot(cgp)[cylno] == 0) 7105375Smckusic goto norot; 7115375Smckusic if (fs->fs_cpc == 0) { 7125375Smckusic /* 7135375Smckusic * block layout info is not available, so just have 7145375Smckusic * to take any block in this cylinder. 7155375Smckusic */ 7165375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 7175375Smckusic goto norot; 7185375Smckusic } 7195375Smckusic /* 7205361Smckusic * check the summary information to see if a block is 7215361Smckusic * available in the requested cylinder starting at the 7229163Ssam * requested rotational position and proceeding around. 7235361Smckusic */ 72434143Smckusick cylbp = cg_blks(fs, cgp, cylno); 7259163Ssam pos = cbtorpos(fs, bpref); 72634143Smckusick for (i = pos; i < fs->fs_nrpos; i++) 7275361Smckusic if (cylbp[i] > 0) 7285361Smckusic break; 72934143Smckusick if (i == fs->fs_nrpos) 7305361Smckusic for (i = 0; i < pos; i++) 7315361Smckusic if (cylbp[i] > 0) 7325361Smckusic break; 7335361Smckusic if (cylbp[i] > 0) { 7344651Smckusic /* 7355361Smckusic * found a rotational position, now find the actual 7365361Smckusic * block. A panic if none is actually there. 7374651Smckusic */ 7385361Smckusic pos = cylno % fs->fs_cpc; 7395361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 74034143Smckusick if (fs_postbl(fs, pos)[i] == -1) { 7416716Smckusick printf("pos = %d, i = %d, fs = %s\n", 7426716Smckusick pos, i, fs->fs_fsmnt); 74351469Sbostic panic("ffs_alloccgblk: cyl groups corrupted"); 7446716Smckusick } 74534143Smckusick for (i = fs_postbl(fs, pos)[i];; ) { 74651469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) { 74711638Ssam bno = blkstofrags(fs, (bno + i)); 7485361Smckusic goto gotit; 7495361Smckusic } 75034143Smckusick delta = fs_rotbl(fs)[i]; 75134143Smckusick if (delta <= 0 || 75234143Smckusick delta + i > fragstoblks(fs, fs->fs_fpg)) 7534651Smckusic break; 7546294Smckusick i += delta; 7554651Smckusic } 7566716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 75751469Sbostic panic("ffs_alloccgblk: can't find blk in cyl"); 7584359Smckusick } 7595361Smckusic norot: 7605361Smckusic /* 7615361Smckusic * no blocks in the requested cylinder, so take next 7625361Smckusic * available one in this cylinder group. 7635361Smckusic */ 76451469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 7656567Smckusic if (bno < 0) 7666294Smckusick return (NULL); 7674651Smckusic cgp->cg_rotor = bno; 7684359Smckusick gotit: 76951469Sbostic ffs_clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno)); 7704792Smckusic cgp->cg_cs.cs_nbfree--; 7714792Smckusic fs->fs_cstotal.cs_nbfree--; 7725322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 7735375Smckusic cylno = cbtocylno(fs, bno); 77434143Smckusick cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 77534143Smckusick cg_blktot(cgp)[cylno]--; 77650893Smckusick fs->fs_fmod = 1; 7774651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 7784359Smckusick } 77934143Smckusick 7805375Smckusic /* 7815375Smckusic * Determine whether an inode can be allocated. 7825375Smckusic * 7835375Smckusic * Check to see if an inode is available, and if it is, 7845375Smckusic * allocate it using the following policy: 7855375Smckusic * 1) allocate the requested inode. 7865375Smckusic * 2) allocate the next available inode after the requested 7875375Smckusic * inode in the specified cylinder group. 7885375Smckusic */ 78951469Sbostic static ino_t 79051469Sbostic ffs_ialloccg(ip, cg, ipref, mode) 7915965Smckusic struct inode *ip; 7924359Smckusick int cg; 7934359Smckusick daddr_t ipref; 7944359Smckusick int mode; 7954359Smckusick { 7965965Smckusic register struct fs *fs; 7974463Smckusic register struct cg *cgp; 79816784Smckusick struct buf *bp; 79937735Smckusick int error, start, len, loc, map, i; 8004359Smckusick 8015965Smckusic fs = ip->i_fs; 8025322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 8036294Smckusick return (NULL); 80437735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 80538776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 80637735Smckusick if (error) { 80737735Smckusick brelse(bp); 80837735Smckusick return (NULL); 80937735Smckusick } 8106531Smckusick cgp = bp->b_un.b_cg; 81137735Smckusick if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 8125960Smckusic brelse(bp); 8136294Smckusick return (NULL); 8145960Smckusic } 8158105Sroot cgp->cg_time = time.tv_sec; 8164359Smckusick if (ipref) { 8174359Smckusick ipref %= fs->fs_ipg; 81834143Smckusick if (isclr(cg_inosused(cgp), ipref)) 8194359Smckusick goto gotit; 82016784Smckusick } 82116784Smckusick start = cgp->cg_irotor / NBBY; 82216784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 82334143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 82416784Smckusick if (loc == 0) { 82517697Smckusick len = start + 1; 82617697Smckusick start = 0; 82734143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 82817697Smckusick if (loc == 0) { 82917697Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 83017697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 83151469Sbostic panic("ffs_ialloccg: map corrupted"); 83217697Smckusick /* NOTREACHED */ 83317697Smckusick } 83416784Smckusick } 83516784Smckusick i = start + len - loc; 83634143Smckusick map = cg_inosused(cgp)[i]; 83716784Smckusick ipref = i * NBBY; 83816784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 83916784Smckusick if ((map & i) == 0) { 8404359Smckusick cgp->cg_irotor = ipref; 8414359Smckusick goto gotit; 8424359Smckusick } 8434359Smckusick } 84416784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 84551469Sbostic panic("ffs_ialloccg: block not in map"); 84616784Smckusick /* NOTREACHED */ 8474359Smckusick gotit: 84834143Smckusick setbit(cg_inosused(cgp), ipref); 8494792Smckusic cgp->cg_cs.cs_nifree--; 8504792Smckusic fs->fs_cstotal.cs_nifree--; 8515322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 85250893Smckusick fs->fs_fmod = 1; 8534359Smckusick if ((mode & IFMT) == IFDIR) { 8544792Smckusic cgp->cg_cs.cs_ndir++; 8554792Smckusic fs->fs_cstotal.cs_ndir++; 8565322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 8574359Smckusick } 8584359Smckusick bdwrite(bp); 8594359Smckusick return (cg * fs->fs_ipg + ipref); 8604359Smckusick } 8614359Smckusick 8625375Smckusic /* 8635375Smckusic * Free a block or fragment. 8645375Smckusic * 8655375Smckusic * The specified block or fragment is placed back in the 8665375Smckusic * free map. If a fragment is deallocated, a possible 8675375Smckusic * block reassembly is checked. 8685375Smckusic */ 86951469Sbostic ffs_blkfree(ip, bno, size) 8705965Smckusic register struct inode *ip; 8714359Smckusick daddr_t bno; 87253059Smckusick long size; 8734359Smckusick { 8744359Smckusick register struct fs *fs; 8754359Smckusick register struct cg *cgp; 87637735Smckusick struct buf *bp; 87737735Smckusick int error, cg, blk, frags, bbase; 8784463Smckusic register int i; 8794359Smckusick 8805965Smckusic fs = ip->i_fs; 8816716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 8826716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 8836716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 88431402Smckusick panic("blkfree: bad size"); 8856716Smckusick } 8865377Smckusic cg = dtog(fs, bno); 88742318Smckusick if ((unsigned)bno >= fs->fs_size) { 8886567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 88953244Smckusick ffs_fserr(fs, ip->i_uid, "bad block"); 8904359Smckusick return; 8916567Smckusic } 89237735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 89338776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 89437735Smckusick if (error) { 89537735Smckusick brelse(bp); 89637735Smckusick return; 89737735Smckusick } 8986531Smckusick cgp = bp->b_un.b_cg; 89937735Smckusick if (!cg_chkmagic(cgp)) { 9005960Smckusic brelse(bp); 9014359Smckusick return; 9025960Smckusic } 9038105Sroot cgp->cg_time = time.tv_sec; 9045377Smckusic bno = dtogd(fs, bno); 9055322Smckusic if (size == fs->fs_bsize) { 90651469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) { 9076716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9086716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 90931402Smckusick panic("blkfree: freeing free block"); 9106567Smckusic } 91151469Sbostic ffs_setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno)); 9124792Smckusic cgp->cg_cs.cs_nbfree++; 9134792Smckusic fs->fs_cstotal.cs_nbfree++; 9145322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9155375Smckusic i = cbtocylno(fs, bno); 91634143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; 91734143Smckusick cg_blktot(cgp)[i]++; 9184426Smckusic } else { 91917224Smckusick bbase = bno - fragnum(fs, bno); 9204463Smckusic /* 9214463Smckusic * decrement the counts associated with the old frags 9224463Smckusic */ 92334143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 92451469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 9254463Smckusic /* 9264463Smckusic * deallocate the fragment 9274463Smckusic */ 9285960Smckusic frags = numfrags(fs, size); 9294463Smckusic for (i = 0; i < frags; i++) { 93034143Smckusick if (isset(cg_blksfree(cgp), bno + i)) { 9316716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9326716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 93331402Smckusick panic("blkfree: freeing free frag"); 9346716Smckusick } 93534143Smckusick setbit(cg_blksfree(cgp), bno + i); 9364426Smckusic } 9376294Smckusick cgp->cg_cs.cs_nffree += i; 9386294Smckusick fs->fs_cstotal.cs_nffree += i; 9396294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 9404463Smckusic /* 9414463Smckusic * add back in counts associated with the new frags 9424463Smckusic */ 94334143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 94451469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 9454463Smckusic /* 9464463Smckusic * if a complete block has been reassembled, account for it 9474463Smckusic */ 94851469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), 94934476Smckusick (daddr_t)fragstoblks(fs, bbase))) { 9505322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 9515322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 9525322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 9534792Smckusic cgp->cg_cs.cs_nbfree++; 9544792Smckusic fs->fs_cstotal.cs_nbfree++; 9555322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9565375Smckusic i = cbtocylno(fs, bbase); 95734143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 95834143Smckusick cg_blktot(cgp)[i]++; 9594426Smckusic } 9604426Smckusic } 96150893Smckusick fs->fs_fmod = 1; 9624359Smckusick bdwrite(bp); 9634359Smckusick } 9644359Smckusick 9655375Smckusic /* 9665375Smckusic * Free an inode. 9675375Smckusic * 9685375Smckusic * The specified inode is placed back in the free map. 9695375Smckusic */ 97053577Sheideman int 97154653Smckusick ffs_vfree(ap) 97254653Smckusick struct vop_vfree_args /* { 97354653Smckusick struct vnode *a_pvp; 97454653Smckusick ino_t a_ino; 97554653Smckusick int a_mode; 97654653Smckusick } */ *ap; 9774359Smckusick { 9784359Smckusick register struct fs *fs; 9794359Smckusick register struct cg *cgp; 98051541Smckusick register struct inode *pip; 98154653Smckusick ino_t ino = ap->a_ino; 98237735Smckusick struct buf *bp; 98337735Smckusick int error, cg; 9844359Smckusick 98553583Sheideman pip = VTOI(ap->a_pvp); 98651469Sbostic fs = pip->i_fs; 98754653Smckusick if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg) 98854653Smckusick panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n", 98954653Smckusick pip->i_dev, ino, fs->fs_fsmnt); 99054653Smckusick cg = itog(fs, ino); 99151469Sbostic error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 99238776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 99337735Smckusick if (error) { 99437735Smckusick brelse(bp); 99553577Sheideman return (0); 99637735Smckusick } 9976531Smckusick cgp = bp->b_un.b_cg; 99837735Smckusick if (!cg_chkmagic(cgp)) { 9995960Smckusic brelse(bp); 100053577Sheideman return (0); 10015960Smckusic } 10028105Sroot cgp->cg_time = time.tv_sec; 100354653Smckusick ino %= fs->fs_ipg; 100454653Smckusick if (isclr(cg_inosused(cgp), ino)) { 100554653Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 100654653Smckusick pip->i_dev, ino, fs->fs_fsmnt); 100748057Smckusick if (fs->fs_ronly == 0) 100848057Smckusick panic("ifree: freeing free inode"); 10096716Smckusick } 101054653Smckusick clrbit(cg_inosused(cgp), ino); 101154653Smckusick if (ino < cgp->cg_irotor) 101254653Smckusick cgp->cg_irotor = ino; 10134792Smckusic cgp->cg_cs.cs_nifree++; 10144792Smckusic fs->fs_cstotal.cs_nifree++; 10155322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 101653583Sheideman if ((ap->a_mode & IFMT) == IFDIR) { 10174792Smckusic cgp->cg_cs.cs_ndir--; 10184792Smckusic fs->fs_cstotal.cs_ndir--; 10195322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 10204359Smckusick } 102150893Smckusick fs->fs_fmod = 1; 10224359Smckusick bdwrite(bp); 102353577Sheideman return (0); 10244359Smckusick } 10254359Smckusick 10264463Smckusic /* 10275375Smckusic * Find a block of the specified size in the specified cylinder group. 10285375Smckusic * 10294651Smckusic * It is a panic if a request is made to find a block if none are 10304651Smckusic * available. 10314651Smckusic */ 103251469Sbostic static daddr_t 103351469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz) 10344651Smckusic register struct fs *fs; 10354651Smckusic register struct cg *cgp; 10364651Smckusic daddr_t bpref; 10374651Smckusic int allocsiz; 10384651Smckusic { 10394651Smckusic daddr_t bno; 10404651Smckusic int start, len, loc, i; 10414651Smckusic int blk, field, subfield, pos; 10424651Smckusic 10434651Smckusic /* 10444651Smckusic * find the fragment by searching through the free block 10454651Smckusic * map for an appropriate bit pattern 10464651Smckusic */ 10474651Smckusic if (bpref) 10485377Smckusic start = dtogd(fs, bpref) / NBBY; 10494651Smckusic else 10504651Smckusic start = cgp->cg_frotor / NBBY; 10515398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 105234476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start], 105334476Smckusick (u_char *)fragtbl[fs->fs_frag], 105434476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 10554651Smckusic if (loc == 0) { 10566531Smckusick len = start + 1; 10576531Smckusick start = 0; 105834476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0], 105934476Smckusick (u_char *)fragtbl[fs->fs_frag], 106034476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 106116784Smckusick if (loc == 0) { 106216784Smckusick printf("start = %d, len = %d, fs = %s\n", 106316784Smckusick start, len, fs->fs_fsmnt); 106451469Sbostic panic("ffs_alloccg: map corrupted"); 106517697Smckusick /* NOTREACHED */ 106616784Smckusick } 10674651Smckusic } 10684651Smckusic bno = (start + len - loc) * NBBY; 10694651Smckusic cgp->cg_frotor = bno; 10704651Smckusic /* 10714651Smckusic * found the byte in the map 10724651Smckusic * sift through the bits to find the selected frag 10734651Smckusic */ 10746294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 107534143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bno); 10764651Smckusic blk <<= 1; 10774651Smckusic field = around[allocsiz]; 10784651Smckusic subfield = inside[allocsiz]; 10795322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 10806294Smckusick if ((blk & field) == subfield) 10816294Smckusick return (bno + pos); 10824651Smckusic field <<= 1; 10834651Smckusic subfield <<= 1; 10844651Smckusic } 10854651Smckusic } 10866716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 108751469Sbostic panic("ffs_alloccg: block not in map"); 10886531Smckusick return (-1); 10894651Smckusic } 10904651Smckusic 10914651Smckusic /* 10925375Smckusic * Fserr prints the name of a file system with an error diagnostic. 10935375Smckusic * 10945375Smckusic * The form of the error message is: 10954359Smckusick * fs: error message 10964359Smckusick */ 109751469Sbostic static void 109851469Sbostic ffs_fserr(fs, uid, cp) 10994359Smckusick struct fs *fs; 110051469Sbostic u_int uid; 11014359Smckusick char *cp; 11024359Smckusick { 11034359Smckusick 110442318Smckusick log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp); 11054359Smckusick } 1106