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*53583Sheideman * @(#)ffs_alloc.c 7.36 (Berkeley) 05/15/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> 1551469Sbostic #include <sys/kernel.h> 1651469Sbostic #include <sys/syslog.h> 174359Smckusick 1853317Smckusick #include <vm/vm.h> 1953317Smckusick 2051469Sbostic #include <ufs/ufs/quota.h> 2151469Sbostic #include <ufs/ufs/inode.h> 2247571Skarels 2351469Sbostic #include <ufs/ffs/fs.h> 2451469Sbostic #include <ufs/ffs/ffs_extern.h> 254359Smckusick 2651469Sbostic extern u_long nextgennumber; 2751469Sbostic 2851469Sbostic static daddr_t ffs_alloccg __P((struct inode *, int, daddr_t, int)); 2951469Sbostic static daddr_t ffs_alloccgblk __P((struct fs *, struct cg *, daddr_t)); 3051469Sbostic static ino_t ffs_dirpref __P((struct fs *)); 3151469Sbostic static daddr_t ffs_fragextend __P((struct inode *, int, long, int, int)); 3251469Sbostic static void ffs_fserr __P((struct fs *, u_int, char *)); 3351469Sbostic static u_long ffs_hashalloc 3451469Sbostic __P((struct inode *, int, long, int, u_long (*)())); 3551469Sbostic static ino_t ffs_ialloccg __P((struct inode *, int, daddr_t, int)); 3651469Sbostic static daddr_t ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int)); 3751469Sbostic 385375Smckusic /* 395375Smckusic * Allocate a block in the file system. 405375Smckusic * 415375Smckusic * The size of the requested block is given, which must be some 425375Smckusic * multiple of fs_fsize and <= fs_bsize. 435375Smckusic * A preference may be optionally specified. If a preference is given 445375Smckusic * the following hierarchy is used to allocate a block: 455375Smckusic * 1) allocate the requested block. 465375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 475375Smckusic * 3) allocate a block in the same cylinder group. 485375Smckusic * 4) quadradically rehash into other cylinder groups, until an 495375Smckusic * available block is located. 505375Smckusic * If no block preference is given the following heirarchy is used 515375Smckusic * to allocate a block: 525375Smckusic * 1) allocate a block in the cylinder group that contains the 535375Smckusic * inode for the file. 545375Smckusic * 2) quadradically rehash into other cylinder groups, until an 555375Smckusic * available block is located. 565375Smckusic */ 5753244Smckusick ffs_alloc(ip, lbn, bpref, size, cred, bnp) 584463Smckusic register struct inode *ip; 5939678Smckusick daddr_t lbn, bpref; 604359Smckusick int size; 6153244Smckusick struct ucred *cred; 6239678Smckusick daddr_t *bnp; 634359Smckusick { 644359Smckusick daddr_t bno; 654359Smckusick register struct fs *fs; 664463Smckusic register struct buf *bp; 6737735Smckusick int cg, error; 684359Smckusick 6939678Smckusick *bnp = 0; 705965Smckusic fs = ip->i_fs; 7153244Smckusick #ifdef DIAGNOSTIC 726716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 736716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 746716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 7551469Sbostic panic("ffs_alloc: bad size"); 766716Smckusick } 7753244Smckusick if (cred == NOCRED) 7853244Smckusick panic("ffs_alloc: missing credential\n"); 7953244Smckusick #endif /* DIAGNOSTIC */ 805322Smckusic if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 814359Smckusick goto nospace; 8241309Smckusick if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 834792Smckusic goto nospace; 847650Ssam #ifdef QUOTA 8541309Smckusick if (error = chkdq(ip, (long)btodb(size), cred, 0)) 8637735Smckusick return (error); 877483Skre #endif 884948Smckusic if (bpref >= fs->fs_size) 894948Smckusic bpref = 0; 904359Smckusick if (bpref == 0) 915377Smckusic cg = itog(fs, ip->i_number); 924359Smckusick else 935377Smckusic cg = dtog(fs, bpref); 9451469Sbostic bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size, 9551469Sbostic (u_long (*)())ffs_alloccg); 9639678Smckusick if (bno > 0) { 9739678Smckusick ip->i_blocks += btodb(size); 9839678Smckusick ip->i_flag |= IUPD|ICHG; 9939678Smckusick *bnp = bno; 10039678Smckusick return (0); 10139678Smckusick } 10245173Smckusick #ifdef QUOTA 10345173Smckusick /* 10445173Smckusick * Restore user's disk quota because allocation failed. 10545173Smckusick */ 10645173Smckusick (void) chkdq(ip, (long)-btodb(size), cred, FORCE); 10745173Smckusick #endif 1084359Smckusick nospace: 10951469Sbostic ffs_fserr(fs, cred->cr_uid, "file system full"); 1104359Smckusick uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 11137735Smckusick return (ENOSPC); 1124359Smckusick } 1134359Smckusick 1145375Smckusic /* 1155375Smckusic * Reallocate a fragment to a bigger size 1165375Smckusic * 1175375Smckusic * The number and size of the old block is given, and a preference 1185375Smckusic * and new size is also specified. The allocator attempts to extend 1195375Smckusic * the original block. Failing that, the regular block allocator is 1205375Smckusic * invoked to get an appropriate block. 1215375Smckusic */ 12253244Smckusick ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp) 1235965Smckusic register struct inode *ip; 12453059Smckusick daddr_t lbprev; 12539678Smckusick daddr_t bpref; 1264426Smckusic int osize, nsize; 12753244Smckusick struct ucred *cred; 12837735Smckusick struct buf **bpp; 1294426Smckusic { 1304426Smckusic register struct fs *fs; 13137735Smckusick struct buf *bp, *obp; 13245719Smckusick int cg, request, error; 13345719Smckusick daddr_t bprev, bno; 1344426Smckusic 13537735Smckusick *bpp = 0; 1365965Smckusic fs = ip->i_fs; 13753244Smckusick #ifdef DIAGNOSTIC 1385960Smckusic if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 1396716Smckusick (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 14051469Sbostic printf( 14151469Sbostic "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 1426716Smckusick ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 14351469Sbostic panic("ffs_realloccg: bad size"); 1446716Smckusick } 14553244Smckusick if (cred == NOCRED) 14653244Smckusick panic("ffs_realloccg: missing credential\n"); 14753244Smckusick #endif /* DIAGNOSTIC */ 14841309Smckusick if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 1494792Smckusic goto nospace; 15039678Smckusick if ((bprev = ip->i_db[lbprev]) == 0) { 1516716Smckusick printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 1526716Smckusick ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 15351469Sbostic panic("ffs_realloccg: bad bprev"); 1546716Smckusick } 15539678Smckusick /* 15639678Smckusick * Allocate the extra space in the buffer. 15739678Smckusick */ 15839678Smckusick if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) { 15939678Smckusick brelse(bp); 16039678Smckusick return (error); 16139678Smckusick } 16245173Smckusick #ifdef QUOTA 16345173Smckusick if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) { 16445173Smckusick brelse(bp); 16545173Smckusick return (error); 16645173Smckusick } 16745173Smckusick #endif 16839678Smckusick /* 16939678Smckusick * Check for extension in the existing location. 17039678Smckusick */ 1716294Smckusick cg = dtog(fs, bprev); 17251469Sbostic if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) { 17339887Smckusick if (bp->b_blkno != fsbtodb(fs, bno)) 17439678Smckusick panic("bad blockno"); 17539762Smckusick ip->i_blocks += btodb(nsize - osize); 17639762Smckusick ip->i_flag |= IUPD|ICHG; 17748948Smckusick allocbuf(bp, nsize); 17848948Smckusick bp->b_flags |= B_DONE; 17948948Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 18037735Smckusick *bpp = bp; 18137735Smckusick return (0); 1824463Smckusic } 18339678Smckusick /* 18439678Smckusick * Allocate a new disk location. 18539678Smckusick */ 1864948Smckusic if (bpref >= fs->fs_size) 1874948Smckusic bpref = 0; 18826253Skarels switch ((int)fs->fs_optim) { 18925256Smckusick case FS_OPTSPACE: 19025256Smckusick /* 19125256Smckusick * Allocate an exact sized fragment. Although this makes 19225256Smckusick * best use of space, we will waste time relocating it if 19325256Smckusick * the file continues to grow. If the fragmentation is 19425256Smckusick * less than half of the minimum free reserve, we choose 19525256Smckusick * to begin optimizing for time. 19625256Smckusick */ 19724698Smckusick request = nsize; 19825256Smckusick if (fs->fs_minfree < 5 || 19925256Smckusick fs->fs_cstotal.cs_nffree > 20025256Smckusick fs->fs_dsize * fs->fs_minfree / (2 * 100)) 20125256Smckusick break; 20225256Smckusick log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 20325256Smckusick fs->fs_fsmnt); 20425256Smckusick fs->fs_optim = FS_OPTTIME; 20525256Smckusick break; 20625256Smckusick case FS_OPTTIME: 20725256Smckusick /* 20851469Sbostic * At this point we have discovered a file that is trying to 20951469Sbostic * grow a small fragment to a larger fragment. To save time, 21051469Sbostic * we allocate a full sized block, then free the unused portion. 21151469Sbostic * If the file continues to grow, the `ffs_fragextend' call 21251469Sbostic * above will be able to grow it in place without further 21351469Sbostic * copying. If aberrant programs cause disk fragmentation to 21451469Sbostic * grow within 2% of the free reserve, we choose to begin 21551469Sbostic * optimizing for space. 21625256Smckusick */ 21724698Smckusick request = fs->fs_bsize; 21825256Smckusick if (fs->fs_cstotal.cs_nffree < 21925256Smckusick fs->fs_dsize * (fs->fs_minfree - 2) / 100) 22025256Smckusick break; 22125256Smckusick log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 22225256Smckusick fs->fs_fsmnt); 22325256Smckusick fs->fs_optim = FS_OPTSPACE; 22425256Smckusick break; 22525256Smckusick default: 22625256Smckusick printf("dev = 0x%x, optim = %d, fs = %s\n", 22725256Smckusick ip->i_dev, fs->fs_optim, fs->fs_fsmnt); 22851469Sbostic panic("ffs_realloccg: bad optim"); 22925256Smckusick /* NOTREACHED */ 23025256Smckusick } 23151469Sbostic bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request, 23251469Sbostic (u_long (*)())ffs_alloccg); 2336567Smckusic if (bno > 0) { 23445719Smckusick bp->b_blkno = fsbtodb(fs, bno); 23545719Smckusick (void) vnode_pager_uncache(ITOV(ip)); 23653059Smckusick ffs_blkfree(ip, bprev, (long)osize); 23724698Smckusick if (nsize < request) 23851469Sbostic ffs_blkfree(ip, bno + numfrags(fs, nsize), 23953059Smckusick (long)(request - nsize)); 24012643Ssam ip->i_blocks += btodb(nsize - osize); 24112643Ssam ip->i_flag |= IUPD|ICHG; 24248948Smckusick allocbuf(bp, nsize); 24348948Smckusick bp->b_flags |= B_DONE; 24448948Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 24537735Smckusick *bpp = bp; 24637735Smckusick return (0); 2474463Smckusic } 24845173Smckusick #ifdef QUOTA 24945173Smckusick /* 25045173Smckusick * Restore user's disk quota because allocation failed. 25145173Smckusick */ 25245173Smckusick (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE); 25345173Smckusick #endif 25439678Smckusick brelse(bp); 2554792Smckusic nospace: 2564463Smckusic /* 2574463Smckusic * no space available 2584463Smckusic */ 25951469Sbostic ffs_fserr(fs, cred->cr_uid, "file system full"); 2604426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 26137735Smckusick return (ENOSPC); 2624426Smckusic } 2634426Smckusic 2645375Smckusic /* 2655375Smckusic * Allocate an inode in the file system. 2665375Smckusic * 26751469Sbostic * If allocating a directory, use ffs_dirpref to select the inode. 26851469Sbostic * If allocating in a directory, the following hierarchy is followed: 26951469Sbostic * 1) allocate the preferred inode. 2705375Smckusic * 2) allocate an inode in the same cylinder group. 2715375Smckusic * 3) quadradically rehash into other cylinder groups, until an 2725375Smckusic * available inode is located. 2735375Smckusic * If no inode preference is given the following heirarchy is used 2745375Smckusic * to allocate an inode: 2755375Smckusic * 1) allocate an inode in cylinder group 0. 2765375Smckusic * 2) quadradically rehash into other cylinder groups, until an 2775375Smckusic * available inode is located. 2785375Smckusic */ 27953519Sheideman ffs_valloc (ap) 28053519Sheideman struct vop_valloc_args *ap; 2814359Smckusick { 28253519Sheideman USES_VOP_VFREE; 28353519Sheideman USES_VOP_VGET; 28451541Smckusick register struct inode *pip; 2854359Smckusick register struct fs *fs; 2864359Smckusick register struct inode *ip; 28751469Sbostic ino_t ino, ipref; 28837735Smckusick int cg, error; 2894359Smckusick 290*53583Sheideman *ap->a_vpp = NULL; 291*53583Sheideman pip = VTOI(ap->a_pvp); 2925965Smckusic fs = pip->i_fs; 2934792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 2944359Smckusick goto noinodes; 29551469Sbostic 296*53583Sheideman if ((ap->a_mode & IFMT) == IFDIR) 29751541Smckusick ipref = ffs_dirpref(fs); 29851469Sbostic else 29951469Sbostic ipref = pip->i_number; 3004948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 3014948Smckusic ipref = 0; 3025377Smckusic cg = itog(fs, ipref); 303*53583Sheideman ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, ap->a_mode, ffs_ialloccg); 3044359Smckusick if (ino == 0) 3054359Smckusick goto noinodes; 306*53583Sheideman error = FFS_VGET(ap->a_pvp->v_mount, ino, ap->a_vpp); 30737735Smckusick if (error) { 308*53583Sheideman VOP_VFREE(ap->a_pvp, ino, ap->a_mode); 30937735Smckusick return (error); 3104359Smckusick } 311*53583Sheideman ip = VTOI(*ap->a_vpp); 3126716Smckusick if (ip->i_mode) { 313*53583Sheideman printf("ap->a_mode = 0%o, inum = %d, fs = %s\n", 3146716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 31551541Smckusick panic("ffs_valloc: dup alloc"); 3166716Smckusick } 31712643Ssam if (ip->i_blocks) { /* XXX */ 31812643Ssam printf("free inode %s/%d had %d blocks\n", 31912643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 32012643Ssam ip->i_blocks = 0; 32112643Ssam } 32239516Smckusick ip->i_flags = 0; 32338255Smckusick /* 32438255Smckusick * Set up a new generation number for this inode. 32538255Smckusick */ 32638255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 32738255Smckusick nextgennumber = time.tv_sec; 32838255Smckusick ip->i_gen = nextgennumber; 32937735Smckusick return (0); 3304359Smckusick noinodes: 331*53583Sheideman ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes"); 3326294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 33337735Smckusick return (ENOSPC); 3344359Smckusick } 3354359Smckusick 3364651Smckusic /* 3375375Smckusic * Find a cylinder to place a directory. 3385375Smckusic * 3395375Smckusic * The policy implemented by this algorithm is to select from 3405375Smckusic * among those cylinder groups with above the average number of 3415375Smckusic * free inodes, the one with the smallest number of directories. 3424651Smckusic */ 34351469Sbostic static ino_t 34451469Sbostic ffs_dirpref(fs) 3455965Smckusic register struct fs *fs; 3464359Smckusick { 3474651Smckusic int cg, minndir, mincg, avgifree; 3484359Smckusick 3494792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 3504651Smckusic minndir = fs->fs_ipg; 3514359Smckusick mincg = 0; 3524651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 3535322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 3545322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 3554359Smckusick mincg = cg; 3565322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 3574359Smckusick } 3589163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 3594359Smckusick } 3604359Smckusick 3614651Smckusic /* 3629163Ssam * Select the desired position for the next block in a file. The file is 3639163Ssam * logically divided into sections. The first section is composed of the 3649163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 3659163Ssam * 3669163Ssam * If no blocks have been allocated in the first section, the policy is to 3679163Ssam * request a block in the same cylinder group as the inode that describes 3689163Ssam * the file. If no blocks have been allocated in any other section, the 3699163Ssam * policy is to place the section in a cylinder group with a greater than 3709163Ssam * average number of free blocks. An appropriate cylinder group is found 37117696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of 37217696Smckusick * blocks is needed, the sweep begins in the cylinder group following the 37317696Smckusick * cylinder group from which the previous allocation was made. The sweep 37417696Smckusick * continues until a cylinder group with greater than the average number 37517696Smckusick * of free blocks is found. If the allocation is for the first block in an 37617696Smckusick * indirect block, the information on the previous allocation is unavailable; 37717696Smckusick * here a best guess is made based upon the logical block number being 37817696Smckusick * allocated. 3799163Ssam * 3809163Ssam * If a section is already partially allocated, the policy is to 3819163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 3829163Ssam * contiguous blocks and the beginning of the next is physically separated 3839163Ssam * so that the disk head will be in transit between them for at least 3849163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 3859163Ssam * schedule another I/O transfer. 3864651Smckusic */ 3875212Smckusic daddr_t 38851469Sbostic ffs_blkpref(ip, lbn, indx, bap) 3899163Ssam struct inode *ip; 3909163Ssam daddr_t lbn; 3919163Ssam int indx; 3929163Ssam daddr_t *bap; 3939163Ssam { 3945965Smckusic register struct fs *fs; 39517696Smckusick register int cg; 39617696Smckusick int avgbfree, startcg; 3979163Ssam daddr_t nextblk; 3984651Smckusic 3999163Ssam fs = ip->i_fs; 4009163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 4019163Ssam if (lbn < NDADDR) { 4029163Ssam cg = itog(fs, ip->i_number); 4035322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 4044651Smckusic } 4059163Ssam /* 4069163Ssam * Find a cylinder with greater than average number of 4079163Ssam * unused data blocks. 4089163Ssam */ 40917696Smckusick if (indx == 0 || bap[indx - 1] == 0) 41017696Smckusick startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; 41117696Smckusick else 41217696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1; 41317696Smckusick startcg %= fs->fs_ncg; 4149163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 41517696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++) 4169163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4179163Ssam fs->fs_cgrotor = cg; 4189163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4199163Ssam } 42017696Smckusick for (cg = 0; cg <= startcg; cg++) 4219163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4229163Ssam fs->fs_cgrotor = cg; 4239163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4249163Ssam } 4259163Ssam return (NULL); 4269163Ssam } 4279163Ssam /* 4289163Ssam * One or more previous blocks have been laid out. If less 4299163Ssam * than fs_maxcontig previous blocks are contiguous, the 4309163Ssam * next block is requested contiguously, otherwise it is 4319163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 4329163Ssam */ 4339163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 4349163Ssam if (indx > fs->fs_maxcontig && 43511638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 4369163Ssam != nextblk) 4379163Ssam return (nextblk); 4389163Ssam if (fs->fs_rotdelay != 0) 4399163Ssam /* 4409163Ssam * Here we convert ms of delay to frags as: 4419163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 4429163Ssam * ((sect/frag) * (ms/sec)) 4439163Ssam * then round up to the next block. 4449163Ssam */ 4459163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 4469163Ssam (NSPF(fs) * 1000), fs->fs_frag); 4479163Ssam return (nextblk); 4484651Smckusic } 4494651Smckusic 4505375Smckusic /* 4515375Smckusic * Implement the cylinder overflow algorithm. 4525375Smckusic * 4535375Smckusic * The policy implemented by this algorithm is: 4545375Smckusic * 1) allocate the block in its requested cylinder group. 4555375Smckusic * 2) quadradically rehash on the cylinder group number. 4565375Smckusic * 3) brute force search for a free block. 4575375Smckusic */ 4585212Smckusic /*VARARGS5*/ 45951469Sbostic static u_long 46051469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator) 4615965Smckusic struct inode *ip; 4624359Smckusick int cg; 4634359Smckusick long pref; 4644359Smckusick int size; /* size for data blocks, mode for inodes */ 4655212Smckusic u_long (*allocator)(); 4664359Smckusick { 4675965Smckusic register struct fs *fs; 4684359Smckusick long result; 4694359Smckusick int i, icg = cg; 4704359Smckusick 4715965Smckusic fs = ip->i_fs; 4724359Smckusick /* 4734359Smckusick * 1: preferred cylinder group 4744359Smckusick */ 4755965Smckusic result = (*allocator)(ip, cg, pref, size); 4764359Smckusick if (result) 4774359Smckusick return (result); 4784359Smckusick /* 4794359Smckusick * 2: quadratic rehash 4804359Smckusick */ 4814359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 4824359Smckusick cg += i; 4834359Smckusick if (cg >= fs->fs_ncg) 4844359Smckusick cg -= fs->fs_ncg; 4855965Smckusic result = (*allocator)(ip, cg, 0, size); 4864359Smckusick if (result) 4874359Smckusick return (result); 4884359Smckusick } 4894359Smckusick /* 4904359Smckusick * 3: brute force search 49110847Ssam * Note that we start at i == 2, since 0 was checked initially, 49210847Ssam * and 1 is always checked in the quadratic rehash. 4934359Smckusick */ 49410848Smckusick cg = (icg + 2) % fs->fs_ncg; 49510847Ssam for (i = 2; i < fs->fs_ncg; i++) { 4965965Smckusic result = (*allocator)(ip, cg, 0, size); 4974359Smckusick if (result) 4984359Smckusick return (result); 4994359Smckusick cg++; 5004359Smckusick if (cg == fs->fs_ncg) 5014359Smckusick cg = 0; 5024359Smckusick } 5036294Smckusick return (NULL); 5044359Smckusick } 5054359Smckusick 5065375Smckusic /* 5075375Smckusic * Determine whether a fragment can be extended. 5085375Smckusic * 5095375Smckusic * Check to see if the necessary fragments are available, and 5105375Smckusic * if they are, allocate them. 5115375Smckusic */ 51251469Sbostic static daddr_t 51351469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize) 5145965Smckusic struct inode *ip; 5154426Smckusic int cg; 5164463Smckusic long bprev; 5174426Smckusic int osize, nsize; 5184426Smckusic { 5195965Smckusic register struct fs *fs; 5204463Smckusic register struct cg *cgp; 52137735Smckusick struct buf *bp; 5224463Smckusic long bno; 5234463Smckusic int frags, bbase; 52437735Smckusick int i, error; 5254426Smckusic 5265965Smckusic fs = ip->i_fs; 52717224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 5286531Smckusick return (NULL); 5295960Smckusic frags = numfrags(fs, nsize); 53017224Smckusick bbase = fragnum(fs, bprev); 53117224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 53230749Skarels /* cannot extend across a block boundary */ 5336294Smckusick return (NULL); 5344463Smckusic } 53537735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 53638776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 53737735Smckusick if (error) { 53837735Smckusick brelse(bp); 53937735Smckusick return (NULL); 54037735Smckusick } 5416531Smckusick cgp = bp->b_un.b_cg; 54237735Smckusick if (!cg_chkmagic(cgp)) { 5435960Smckusic brelse(bp); 5446294Smckusick return (NULL); 5455960Smckusic } 5468105Sroot cgp->cg_time = time.tv_sec; 5475377Smckusic bno = dtogd(fs, bprev); 5485960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 54934143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) { 5505361Smckusic brelse(bp); 5516294Smckusick return (NULL); 5525361Smckusic } 5535361Smckusic /* 5545361Smckusic * the current fragment can be extended 5555361Smckusic * deduct the count on fragment being extended into 5565361Smckusic * increase the count on the remaining fragment (if any) 5575361Smckusic * allocate the extended piece 5585361Smckusic */ 5595361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 56034143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) 5614463Smckusic break; 5625960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 5635361Smckusic if (i != frags) 5645361Smckusic cgp->cg_frsum[i - frags]++; 5655960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 56634143Smckusick clrbit(cg_blksfree(cgp), bno + i); 5675361Smckusic cgp->cg_cs.cs_nffree--; 5685361Smckusic fs->fs_cstotal.cs_nffree--; 5695361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 5704463Smckusic } 57150893Smckusick fs->fs_fmod = 1; 5725361Smckusic bdwrite(bp); 5735361Smckusic return (bprev); 5744426Smckusic } 5754426Smckusic 5765375Smckusic /* 5775375Smckusic * Determine whether a block can be allocated. 5785375Smckusic * 5795375Smckusic * Check to see if a block of the apprpriate size is available, 5805375Smckusic * and if it is, allocate it. 5815375Smckusic */ 58251779Smarc static daddr_t 58351469Sbostic ffs_alloccg(ip, cg, bpref, size) 5845965Smckusic struct inode *ip; 5854359Smckusick int cg; 5864359Smckusick daddr_t bpref; 5874359Smckusick int size; 5884359Smckusick { 5895965Smckusic register struct fs *fs; 5904463Smckusic register struct cg *cgp; 59137735Smckusick struct buf *bp; 5924463Smckusic register int i; 59337735Smckusick int error, bno, frags, allocsiz; 5944359Smckusick 5955965Smckusic fs = ip->i_fs; 5965322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 5976294Smckusick return (NULL); 59837735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 59938776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 60037735Smckusick if (error) { 60137735Smckusick brelse(bp); 60237735Smckusick return (NULL); 60337735Smckusick } 6046531Smckusick cgp = bp->b_un.b_cg; 60537735Smckusick if (!cg_chkmagic(cgp) || 60615950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 6075960Smckusic brelse(bp); 6086294Smckusick return (NULL); 6095960Smckusic } 6108105Sroot cgp->cg_time = time.tv_sec; 6115322Smckusic if (size == fs->fs_bsize) { 61251469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref); 6134463Smckusic bdwrite(bp); 6144463Smckusic return (bno); 6154463Smckusic } 6164463Smckusic /* 6174463Smckusic * check to see if any fragments are already available 6184463Smckusic * allocsiz is the size which will be allocated, hacking 6194463Smckusic * it down to a smaller size if necessary 6204463Smckusic */ 6215960Smckusic frags = numfrags(fs, size); 6225322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 6234463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 6244463Smckusic break; 6255322Smckusic if (allocsiz == fs->fs_frag) { 6264463Smckusic /* 6274463Smckusic * no fragments were available, so a block will be 6284463Smckusic * allocated, and hacked up 6294463Smckusic */ 6304792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 6314463Smckusic brelse(bp); 6326294Smckusick return (NULL); 6334463Smckusic } 63451469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref); 6355377Smckusic bpref = dtogd(fs, bno); 6365322Smckusic for (i = frags; i < fs->fs_frag; i++) 63734143Smckusick setbit(cg_blksfree(cgp), bpref + i); 6385322Smckusic i = fs->fs_frag - frags; 6394792Smckusic cgp->cg_cs.cs_nffree += i; 6404792Smckusic fs->fs_cstotal.cs_nffree += i; 6415322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 64250893Smckusick fs->fs_fmod = 1; 6434463Smckusic cgp->cg_frsum[i]++; 6444463Smckusic bdwrite(bp); 6454463Smckusic return (bno); 6464463Smckusic } 64751469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); 64815950Smckusick if (bno < 0) { 64915950Smckusick brelse(bp); 6506294Smckusick return (NULL); 65115950Smckusick } 6524463Smckusic for (i = 0; i < frags; i++) 65334143Smckusick clrbit(cg_blksfree(cgp), bno + i); 6544792Smckusic cgp->cg_cs.cs_nffree -= frags; 6554792Smckusic fs->fs_cstotal.cs_nffree -= frags; 6565322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 65750893Smckusick fs->fs_fmod = 1; 6584463Smckusic cgp->cg_frsum[allocsiz]--; 6594463Smckusic if (frags != allocsiz) 6604463Smckusic cgp->cg_frsum[allocsiz - frags]++; 6614463Smckusic bdwrite(bp); 6624463Smckusic return (cg * fs->fs_fpg + bno); 6634463Smckusic } 6644463Smckusic 6655375Smckusic /* 6665375Smckusic * Allocate a block in a cylinder group. 6675375Smckusic * 6685375Smckusic * This algorithm implements the following policy: 6695375Smckusic * 1) allocate the requested block. 6705375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 6715375Smckusic * 3) allocate the next available block on the block rotor for the 6725375Smckusic * specified cylinder group. 6735375Smckusic * Note that this routine only allocates fs_bsize blocks; these 6745375Smckusic * blocks may be fragmented by the routine that allocates them. 6755375Smckusic */ 67651469Sbostic static daddr_t 67751469Sbostic ffs_alloccgblk(fs, cgp, bpref) 6785965Smckusic register struct fs *fs; 6794463Smckusic register struct cg *cgp; 6804463Smckusic daddr_t bpref; 6814463Smckusic { 6824651Smckusic daddr_t bno; 6836294Smckusick int cylno, pos, delta; 6844651Smckusic short *cylbp; 6855361Smckusic register int i; 6864463Smckusic 6874651Smckusic if (bpref == 0) { 6884651Smckusic bpref = cgp->cg_rotor; 6895361Smckusic goto norot; 6905361Smckusic } 69117224Smckusick bpref = blknum(fs, bpref); 6925377Smckusic bpref = dtogd(fs, bpref); 6935361Smckusic /* 6945361Smckusic * if the requested block is available, use it 6955361Smckusic */ 69651469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 6975361Smckusic bno = bpref; 6985361Smckusic goto gotit; 6995361Smckusic } 7005361Smckusic /* 7015361Smckusic * check for a block available on the same cylinder 7025361Smckusic */ 7035361Smckusic cylno = cbtocylno(fs, bpref); 70434143Smckusick if (cg_blktot(cgp)[cylno] == 0) 7055375Smckusic goto norot; 7065375Smckusic if (fs->fs_cpc == 0) { 7075375Smckusic /* 7085375Smckusic * block layout info is not available, so just have 7095375Smckusic * to take any block in this cylinder. 7105375Smckusic */ 7115375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 7125375Smckusic goto norot; 7135375Smckusic } 7145375Smckusic /* 7155361Smckusic * check the summary information to see if a block is 7165361Smckusic * available in the requested cylinder starting at the 7179163Ssam * requested rotational position and proceeding around. 7185361Smckusic */ 71934143Smckusick cylbp = cg_blks(fs, cgp, cylno); 7209163Ssam pos = cbtorpos(fs, bpref); 72134143Smckusick for (i = pos; i < fs->fs_nrpos; i++) 7225361Smckusic if (cylbp[i] > 0) 7235361Smckusic break; 72434143Smckusick if (i == fs->fs_nrpos) 7255361Smckusic for (i = 0; i < pos; i++) 7265361Smckusic if (cylbp[i] > 0) 7275361Smckusic break; 7285361Smckusic if (cylbp[i] > 0) { 7294651Smckusic /* 7305361Smckusic * found a rotational position, now find the actual 7315361Smckusic * block. A panic if none is actually there. 7324651Smckusic */ 7335361Smckusic pos = cylno % fs->fs_cpc; 7345361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 73534143Smckusick if (fs_postbl(fs, pos)[i] == -1) { 7366716Smckusick printf("pos = %d, i = %d, fs = %s\n", 7376716Smckusick pos, i, fs->fs_fsmnt); 73851469Sbostic panic("ffs_alloccgblk: cyl groups corrupted"); 7396716Smckusick } 74034143Smckusick for (i = fs_postbl(fs, pos)[i];; ) { 74151469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) { 74211638Ssam bno = blkstofrags(fs, (bno + i)); 7435361Smckusic goto gotit; 7445361Smckusic } 74534143Smckusick delta = fs_rotbl(fs)[i]; 74634143Smckusick if (delta <= 0 || 74734143Smckusick delta + i > fragstoblks(fs, fs->fs_fpg)) 7484651Smckusic break; 7496294Smckusick i += delta; 7504651Smckusic } 7516716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 75251469Sbostic panic("ffs_alloccgblk: can't find blk in cyl"); 7534359Smckusick } 7545361Smckusic norot: 7555361Smckusic /* 7565361Smckusic * no blocks in the requested cylinder, so take next 7575361Smckusic * available one in this cylinder group. 7585361Smckusic */ 75951469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 7606567Smckusic if (bno < 0) 7616294Smckusick return (NULL); 7624651Smckusic cgp->cg_rotor = bno; 7634359Smckusick gotit: 76451469Sbostic ffs_clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno)); 7654792Smckusic cgp->cg_cs.cs_nbfree--; 7664792Smckusic fs->fs_cstotal.cs_nbfree--; 7675322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 7685375Smckusic cylno = cbtocylno(fs, bno); 76934143Smckusick cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 77034143Smckusick cg_blktot(cgp)[cylno]--; 77150893Smckusick fs->fs_fmod = 1; 7724651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 7734359Smckusick } 77434143Smckusick 7755375Smckusic /* 7765375Smckusic * Determine whether an inode can be allocated. 7775375Smckusic * 7785375Smckusic * Check to see if an inode is available, and if it is, 7795375Smckusic * allocate it using the following policy: 7805375Smckusic * 1) allocate the requested inode. 7815375Smckusic * 2) allocate the next available inode after the requested 7825375Smckusic * inode in the specified cylinder group. 7835375Smckusic */ 78451469Sbostic static ino_t 78551469Sbostic ffs_ialloccg(ip, cg, ipref, mode) 7865965Smckusic struct inode *ip; 7874359Smckusick int cg; 7884359Smckusick daddr_t ipref; 7894359Smckusick int mode; 7904359Smckusick { 7915965Smckusic register struct fs *fs; 7924463Smckusic register struct cg *cgp; 79316784Smckusick struct buf *bp; 79437735Smckusick int error, start, len, loc, map, i; 7954359Smckusick 7965965Smckusic fs = ip->i_fs; 7975322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 7986294Smckusick return (NULL); 79937735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 80038776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 80137735Smckusick if (error) { 80237735Smckusick brelse(bp); 80337735Smckusick return (NULL); 80437735Smckusick } 8056531Smckusick cgp = bp->b_un.b_cg; 80637735Smckusick if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 8075960Smckusic brelse(bp); 8086294Smckusick return (NULL); 8095960Smckusic } 8108105Sroot cgp->cg_time = time.tv_sec; 8114359Smckusick if (ipref) { 8124359Smckusick ipref %= fs->fs_ipg; 81334143Smckusick if (isclr(cg_inosused(cgp), ipref)) 8144359Smckusick goto gotit; 81516784Smckusick } 81616784Smckusick start = cgp->cg_irotor / NBBY; 81716784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 81834143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 81916784Smckusick if (loc == 0) { 82017697Smckusick len = start + 1; 82117697Smckusick start = 0; 82234143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 82317697Smckusick if (loc == 0) { 82417697Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 82517697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 82651469Sbostic panic("ffs_ialloccg: map corrupted"); 82717697Smckusick /* NOTREACHED */ 82817697Smckusick } 82916784Smckusick } 83016784Smckusick i = start + len - loc; 83134143Smckusick map = cg_inosused(cgp)[i]; 83216784Smckusick ipref = i * NBBY; 83316784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 83416784Smckusick if ((map & i) == 0) { 8354359Smckusick cgp->cg_irotor = ipref; 8364359Smckusick goto gotit; 8374359Smckusick } 8384359Smckusick } 83916784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 84051469Sbostic panic("ffs_ialloccg: block not in map"); 84116784Smckusick /* NOTREACHED */ 8424359Smckusick gotit: 84334143Smckusick setbit(cg_inosused(cgp), ipref); 8444792Smckusic cgp->cg_cs.cs_nifree--; 8454792Smckusic fs->fs_cstotal.cs_nifree--; 8465322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 84750893Smckusick fs->fs_fmod = 1; 8484359Smckusick if ((mode & IFMT) == IFDIR) { 8494792Smckusic cgp->cg_cs.cs_ndir++; 8504792Smckusic fs->fs_cstotal.cs_ndir++; 8515322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 8524359Smckusick } 8534359Smckusick bdwrite(bp); 8544359Smckusick return (cg * fs->fs_ipg + ipref); 8554359Smckusick } 8564359Smckusick 8575375Smckusic /* 8585375Smckusic * Free a block or fragment. 8595375Smckusic * 8605375Smckusic * The specified block or fragment is placed back in the 8615375Smckusic * free map. If a fragment is deallocated, a possible 8625375Smckusic * block reassembly is checked. 8635375Smckusic */ 86451469Sbostic ffs_blkfree(ip, bno, size) 8655965Smckusic register struct inode *ip; 8664359Smckusick daddr_t bno; 86753059Smckusick long size; 8684359Smckusick { 8694359Smckusick register struct fs *fs; 8704359Smckusick register struct cg *cgp; 87137735Smckusick struct buf *bp; 87237735Smckusick int error, cg, blk, frags, bbase; 8734463Smckusic register int i; 8744359Smckusick 8755965Smckusic fs = ip->i_fs; 8766716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 8776716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 8786716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 87931402Smckusick panic("blkfree: bad size"); 8806716Smckusick } 8815377Smckusic cg = dtog(fs, bno); 88242318Smckusick if ((unsigned)bno >= fs->fs_size) { 8836567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 88453244Smckusick ffs_fserr(fs, ip->i_uid, "bad block"); 8854359Smckusick return; 8866567Smckusic } 88737735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 88838776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 88937735Smckusick if (error) { 89037735Smckusick brelse(bp); 89137735Smckusick return; 89237735Smckusick } 8936531Smckusick cgp = bp->b_un.b_cg; 89437735Smckusick if (!cg_chkmagic(cgp)) { 8955960Smckusic brelse(bp); 8964359Smckusick return; 8975960Smckusic } 8988105Sroot cgp->cg_time = time.tv_sec; 8995377Smckusic bno = dtogd(fs, bno); 9005322Smckusic if (size == fs->fs_bsize) { 90151469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) { 9026716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9036716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 90431402Smckusick panic("blkfree: freeing free block"); 9056567Smckusic } 90651469Sbostic ffs_setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno)); 9074792Smckusic cgp->cg_cs.cs_nbfree++; 9084792Smckusic fs->fs_cstotal.cs_nbfree++; 9095322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9105375Smckusic i = cbtocylno(fs, bno); 91134143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; 91234143Smckusick cg_blktot(cgp)[i]++; 9134426Smckusic } else { 91417224Smckusick bbase = bno - fragnum(fs, bno); 9154463Smckusic /* 9164463Smckusic * decrement the counts associated with the old frags 9174463Smckusic */ 91834143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 91951469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 9204463Smckusic /* 9214463Smckusic * deallocate the fragment 9224463Smckusic */ 9235960Smckusic frags = numfrags(fs, size); 9244463Smckusic for (i = 0; i < frags; i++) { 92534143Smckusick if (isset(cg_blksfree(cgp), bno + i)) { 9266716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9276716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 92831402Smckusick panic("blkfree: freeing free frag"); 9296716Smckusick } 93034143Smckusick setbit(cg_blksfree(cgp), bno + i); 9314426Smckusic } 9326294Smckusick cgp->cg_cs.cs_nffree += i; 9336294Smckusick fs->fs_cstotal.cs_nffree += i; 9346294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 9354463Smckusic /* 9364463Smckusic * add back in counts associated with the new frags 9374463Smckusic */ 93834143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 93951469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 9404463Smckusic /* 9414463Smckusic * if a complete block has been reassembled, account for it 9424463Smckusic */ 94351469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), 94434476Smckusick (daddr_t)fragstoblks(fs, bbase))) { 9455322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 9465322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 9475322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 9484792Smckusic cgp->cg_cs.cs_nbfree++; 9494792Smckusic fs->fs_cstotal.cs_nbfree++; 9505322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9515375Smckusic i = cbtocylno(fs, bbase); 95234143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 95334143Smckusick cg_blktot(cgp)[i]++; 9544426Smckusic } 9554426Smckusic } 95650893Smckusick fs->fs_fmod = 1; 9574359Smckusick bdwrite(bp); 9584359Smckusick } 9594359Smckusick 9605375Smckusic /* 9615375Smckusic * Free an inode. 9625375Smckusic * 9635375Smckusic * The specified inode is placed back in the free map. 9645375Smckusic */ 96553577Sheideman int 96653519Sheideman ffs_vfree (ap) 96753519Sheideman struct vop_vfree_args *ap; 9684359Smckusick { 9694359Smckusick register struct fs *fs; 9704359Smckusick register struct cg *cgp; 97151541Smckusick register struct inode *pip; 97237735Smckusick struct buf *bp; 97337735Smckusick int error, cg; 9744359Smckusick 975*53583Sheideman pip = VTOI(ap->a_pvp); 97651469Sbostic fs = pip->i_fs; 977*53583Sheideman if ((u_int)ap->a_ino >= fs->fs_ipg * fs->fs_ncg) 978*53583Sheideman panic("ifree: range: dev = 0x%x, ap->a_ino = %d, fs = %s\n", 979*53583Sheideman pip->i_dev, ap->a_ino, fs->fs_fsmnt); 980*53583Sheideman cg = itog(fs, ap->a_ino); 98151469Sbostic error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 98238776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 98337735Smckusick if (error) { 98437735Smckusick brelse(bp); 98553577Sheideman return (0); 98637735Smckusick } 9876531Smckusick cgp = bp->b_un.b_cg; 98837735Smckusick if (!cg_chkmagic(cgp)) { 9895960Smckusic brelse(bp); 99053577Sheideman return (0); 9915960Smckusic } 9928105Sroot cgp->cg_time = time.tv_sec; 993*53583Sheideman ap->a_ino %= fs->fs_ipg; 994*53583Sheideman if (isclr(cg_inosused(cgp), ap->a_ino)) { 995*53583Sheideman printf("dev = 0x%x, ap->a_ino = %d, fs = %s\n", 996*53583Sheideman pip->i_dev, ap->a_ino, fs->fs_fsmnt); 99748057Smckusick if (fs->fs_ronly == 0) 99848057Smckusick panic("ifree: freeing free inode"); 9996716Smckusick } 1000*53583Sheideman clrbit(cg_inosused(cgp), ap->a_ino); 1001*53583Sheideman if (ap->a_ino < cgp->cg_irotor) 1002*53583Sheideman cgp->cg_irotor = ap->a_ino; 10034792Smckusic cgp->cg_cs.cs_nifree++; 10044792Smckusic fs->fs_cstotal.cs_nifree++; 10055322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 1006*53583Sheideman if ((ap->a_mode & IFMT) == IFDIR) { 10074792Smckusic cgp->cg_cs.cs_ndir--; 10084792Smckusic fs->fs_cstotal.cs_ndir--; 10095322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 10104359Smckusick } 101150893Smckusick fs->fs_fmod = 1; 10124359Smckusick bdwrite(bp); 101353577Sheideman return (0); 10144359Smckusick } 10154359Smckusick 10164463Smckusic /* 10175375Smckusic * Find a block of the specified size in the specified cylinder group. 10185375Smckusic * 10194651Smckusic * It is a panic if a request is made to find a block if none are 10204651Smckusic * available. 10214651Smckusic */ 102251469Sbostic static daddr_t 102351469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz) 10244651Smckusic register struct fs *fs; 10254651Smckusic register struct cg *cgp; 10264651Smckusic daddr_t bpref; 10274651Smckusic int allocsiz; 10284651Smckusic { 10294651Smckusic daddr_t bno; 10304651Smckusic int start, len, loc, i; 10314651Smckusic int blk, field, subfield, pos; 10324651Smckusic 10334651Smckusic /* 10344651Smckusic * find the fragment by searching through the free block 10354651Smckusic * map for an appropriate bit pattern 10364651Smckusic */ 10374651Smckusic if (bpref) 10385377Smckusic start = dtogd(fs, bpref) / NBBY; 10394651Smckusic else 10404651Smckusic start = cgp->cg_frotor / NBBY; 10415398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 104234476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start], 104334476Smckusick (u_char *)fragtbl[fs->fs_frag], 104434476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 10454651Smckusic if (loc == 0) { 10466531Smckusick len = start + 1; 10476531Smckusick start = 0; 104834476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0], 104934476Smckusick (u_char *)fragtbl[fs->fs_frag], 105034476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 105116784Smckusick if (loc == 0) { 105216784Smckusick printf("start = %d, len = %d, fs = %s\n", 105316784Smckusick start, len, fs->fs_fsmnt); 105451469Sbostic panic("ffs_alloccg: map corrupted"); 105517697Smckusick /* NOTREACHED */ 105616784Smckusick } 10574651Smckusic } 10584651Smckusic bno = (start + len - loc) * NBBY; 10594651Smckusic cgp->cg_frotor = bno; 10604651Smckusic /* 10614651Smckusic * found the byte in the map 10624651Smckusic * sift through the bits to find the selected frag 10634651Smckusic */ 10646294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 106534143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bno); 10664651Smckusic blk <<= 1; 10674651Smckusic field = around[allocsiz]; 10684651Smckusic subfield = inside[allocsiz]; 10695322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 10706294Smckusick if ((blk & field) == subfield) 10716294Smckusick return (bno + pos); 10724651Smckusic field <<= 1; 10734651Smckusic subfield <<= 1; 10744651Smckusic } 10754651Smckusic } 10766716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 107751469Sbostic panic("ffs_alloccg: block not in map"); 10786531Smckusick return (-1); 10794651Smckusic } 10804651Smckusic 10814651Smckusic /* 10825375Smckusic * Fserr prints the name of a file system with an error diagnostic. 10835375Smckusic * 10845375Smckusic * The form of the error message is: 10854359Smckusick * fs: error message 10864359Smckusick */ 108751469Sbostic static void 108851469Sbostic ffs_fserr(fs, uid, cp) 10894359Smckusick struct fs *fs; 109051469Sbostic u_int uid; 10914359Smckusick char *cp; 10924359Smckusick { 10934359Smckusick 109442318Smckusick log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp); 10954359Smckusick } 1096