123394Smckusick /* 263371Sbostic * Copyright (c) 1982, 1986, 1989, 1993 363371Sbostic * The Regents of the University of California. All rights reserved. 423394Smckusick * 544536Sbostic * %sccs.include.redist.c% 634432Sbostic * 7*68112Smckusick * @(#)ffs_alloc.c 8.12 (Berkeley) 01/02/95 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)); 3165999Smckusick static daddr_t ffs_clusteralloc __P((struct inode *, int, daddr_t, int)); 3251469Sbostic static ino_t ffs_dirpref __P((struct fs *)); 3351469Sbostic static daddr_t ffs_fragextend __P((struct inode *, int, long, int, int)); 3451469Sbostic static void ffs_fserr __P((struct fs *, u_int, char *)); 3551469Sbostic static u_long ffs_hashalloc 3651469Sbostic __P((struct inode *, int, long, int, u_long (*)())); 3767869Smckusick static u_long ffs_nodealloccg __P((struct inode *, int, daddr_t, int)); 3851469Sbostic static daddr_t ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int)); 3951469Sbostic 405375Smckusic /* 415375Smckusic * Allocate a block in the file system. 425375Smckusic * 435375Smckusic * The size of the requested block is given, which must be some 445375Smckusic * multiple of fs_fsize and <= fs_bsize. 455375Smckusic * A preference may be optionally specified. If a preference is given 465375Smckusic * the following hierarchy is used to allocate a block: 475375Smckusic * 1) allocate the requested block. 485375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 495375Smckusic * 3) allocate a block in the same cylinder group. 505375Smckusic * 4) quadradically rehash into other cylinder groups, until an 515375Smckusic * available block is located. 525375Smckusic * If no block preference is given the following heirarchy is used 535375Smckusic * to allocate a block: 545375Smckusic * 1) allocate a block in the cylinder group that contains the 555375Smckusic * inode for the file. 565375Smckusic * 2) quadradically rehash into other cylinder groups, until an 575375Smckusic * available block is located. 585375Smckusic */ 5953244Smckusick ffs_alloc(ip, lbn, bpref, size, cred, bnp) 604463Smckusic register struct inode *ip; 6139678Smckusick daddr_t lbn, bpref; 624359Smckusick int size; 6353244Smckusick struct ucred *cred; 6439678Smckusick daddr_t *bnp; 654359Smckusick { 6665468Sbostic register struct fs *fs; 674359Smckusick daddr_t bno; 6837735Smckusick int cg, error; 694359Smckusick 7039678Smckusick *bnp = 0; 715965Smckusic fs = ip->i_fs; 7253244Smckusick #ifdef DIAGNOSTIC 7364508Sbostic if ((u_int)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) 9264603Sbostic cg = ino_to_cg(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); 9964603Sbostic ip->i_flag |= IN_CHANGE | IN_UPDATE; 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; 13265468Sbostic struct buf *bp; 13345719Smckusick int cg, request, error; 13445719Smckusick daddr_t bprev, bno; 1354426Smckusic 13637735Smckusick *bpp = 0; 1375965Smckusic fs = ip->i_fs; 13853244Smckusick #ifdef DIAGNOSTIC 13964508Sbostic if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 14064508Sbostic (u_int)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); 17764603Sbostic ip->i_flag |= IN_CHANGE | IN_UPDATE; 17848948Smckusick allocbuf(bp, nsize); 17948948Smckusick bp->b_flags |= B_DONE; 18064508Sbostic bzero((char *)bp->b_data + osize, (u_int)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); 24264603Sbostic ip->i_flag |= IN_CHANGE | IN_UPDATE; 24348948Smckusick allocbuf(bp, nsize); 24448948Smckusick bp->b_flags |= B_DONE; 24564508Sbostic bzero((char *)bp->b_data + osize, (u_int)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 /* 26665999Smckusick * Reallocate a sequence of blocks into a contiguous sequence of blocks. 26765999Smckusick * 26865999Smckusick * The vnode and an array of buffer pointers for a range of sequential 26965999Smckusick * logical blocks to be made contiguous is given. The allocator attempts 27065999Smckusick * to find a range of sequential blocks starting as close as possible to 27165999Smckusick * an fs_rotdelay offset from the end of the allocation for the logical 27265999Smckusick * block immediately preceeding the current range. If successful, the 27365999Smckusick * physical block numbers in the buffer pointers and in the inode are 27465999Smckusick * changed to reflect the new allocation. If unsuccessful, the allocation 27565999Smckusick * is left unchanged. The success in doing the reallocation is returned. 27665999Smckusick * Note that the error return is not reflected back to the user. Rather 27765999Smckusick * the previous block allocation will be used. 27865999Smckusick */ 279*68112Smckusick #ifdef DEBUG 28066176Smckusick #include <sys/sysctl.h> 28166176Smckusick int doasyncfree = 1; 28266176Smckusick struct ctldebug debug14 = { "doasyncfree", &doasyncfree }; 28367871Smckusick int prtrealloc = 0; 28467871Smckusick struct ctldebug debug15 = { "prtrealloc", &prtrealloc }; 285*68112Smckusick #else 286*68112Smckusick #define doasyncfree 1 28767392Smkm #endif 28867392Smkm 28965999Smckusick int 29065999Smckusick ffs_reallocblks(ap) 29165999Smckusick struct vop_reallocblks_args /* { 29265999Smckusick struct vnode *a_vp; 29365999Smckusick struct cluster_save *a_buflist; 29465999Smckusick } */ *ap; 29565999Smckusick { 29665999Smckusick struct fs *fs; 29765999Smckusick struct inode *ip; 29865999Smckusick struct vnode *vp; 29965999Smckusick struct buf *sbp, *ebp; 30065999Smckusick daddr_t *bap, *sbap, *ebap; 30165999Smckusick struct cluster_save *buflist; 30265999Smckusick daddr_t start_lbn, end_lbn, soff, eoff, newblk, blkno; 30365999Smckusick struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp; 30465999Smckusick int i, len, start_lvl, end_lvl, pref, ssize; 30565999Smckusick 30665999Smckusick vp = ap->a_vp; 30765999Smckusick ip = VTOI(vp); 30865999Smckusick fs = ip->i_fs; 30965999Smckusick if (fs->fs_contigsumsize <= 0) 31065999Smckusick return (ENOSPC); 31165999Smckusick buflist = ap->a_buflist; 31265999Smckusick len = buflist->bs_nchildren; 31365999Smckusick start_lbn = buflist->bs_children[0]->b_lblkno; 31465999Smckusick end_lbn = start_lbn + len - 1; 31565999Smckusick #ifdef DIAGNOSTIC 31665999Smckusick for (i = 1; i < len; i++) 31765999Smckusick if (buflist->bs_children[i]->b_lblkno != start_lbn + i) 31865999Smckusick panic("ffs_reallocblks: non-cluster"); 31965999Smckusick #endif 32065999Smckusick /* 32165999Smckusick * If the latest allocation is in a new cylinder group, assume that 32265999Smckusick * the filesystem has decided to move and do not force it back to 32365999Smckusick * the previous cylinder group. 32465999Smckusick */ 32565999Smckusick if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) != 32665999Smckusick dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno))) 32765999Smckusick return (ENOSPC); 32865999Smckusick if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) || 32965999Smckusick ufs_getlbns(vp, end_lbn, end_ap, &end_lvl)) 33065999Smckusick return (ENOSPC); 33165999Smckusick /* 33265999Smckusick * Get the starting offset and block map for the first block. 33365999Smckusick */ 33465999Smckusick if (start_lvl == 0) { 33565999Smckusick sbap = &ip->i_db[0]; 33665999Smckusick soff = start_lbn; 33765999Smckusick } else { 33865999Smckusick idp = &start_ap[start_lvl - 1]; 33965999Smckusick if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) { 34065999Smckusick brelse(sbp); 34165999Smckusick return (ENOSPC); 34265999Smckusick } 34365999Smckusick sbap = (daddr_t *)sbp->b_data; 34465999Smckusick soff = idp->in_off; 34565999Smckusick } 34665999Smckusick /* 34765999Smckusick * Find the preferred location for the cluster. 34865999Smckusick */ 34965999Smckusick pref = ffs_blkpref(ip, start_lbn, soff, sbap); 35065999Smckusick /* 35165999Smckusick * If the block range spans two block maps, get the second map. 35265999Smckusick */ 35366086Smckusick if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) { 35465999Smckusick ssize = len; 35565999Smckusick } else { 35666086Smckusick #ifdef DIAGNOSTIC 35766086Smckusick if (start_ap[start_lvl-1].in_lbn == idp->in_lbn) 35866086Smckusick panic("ffs_reallocblk: start == end"); 35966086Smckusick #endif 36065999Smckusick ssize = len - (idp->in_off + 1); 36165999Smckusick if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp)) 36265999Smckusick goto fail; 36365999Smckusick ebap = (daddr_t *)ebp->b_data; 36465999Smckusick } 36565999Smckusick /* 36665999Smckusick * Search the block map looking for an allocation of the desired size. 36765999Smckusick */ 36865999Smckusick if ((newblk = (daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref, 36965999Smckusick len, (u_long (*)())ffs_clusteralloc)) == 0) 37065999Smckusick goto fail; 37165999Smckusick /* 37265999Smckusick * We have found a new contiguous block. 37365999Smckusick * 37465999Smckusick * First we have to replace the old block pointers with the new 37565999Smckusick * block pointers in the inode and indirect blocks associated 37665999Smckusick * with the file. 37765999Smckusick */ 37867871Smckusick #ifdef DEBUG 37967871Smckusick if (prtrealloc) 38067871Smckusick printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number, 381*68112Smckusick start_lbn, end_lbn); 382*68112Smckusick #endif 38365999Smckusick blkno = newblk; 38465999Smckusick for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) { 38565999Smckusick if (i == ssize) 38665999Smckusick bap = ebap; 38765999Smckusick #ifdef DIAGNOSTIC 38867869Smckusick if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap) 38965999Smckusick panic("ffs_reallocblks: alloc mismatch"); 39065999Smckusick #endif 39167871Smckusick #ifdef DEBUG 39267871Smckusick if (prtrealloc) 39367871Smckusick printf(" %d,", *bap); 39467871Smckusick #endif 39565999Smckusick *bap++ = blkno; 39665999Smckusick } 39765999Smckusick /* 39865999Smckusick * Next we must write out the modified inode and indirect blocks. 39966176Smckusick * For strict correctness, the writes should be synchronous since 40066176Smckusick * the old block values may have been written to disk. In practise 40166176Smckusick * they are almost never written, but if we are concerned about 40266176Smckusick * strict correctness, the `doasyncfree' flag should be set to zero. 40365999Smckusick * 40466176Smckusick * The test on `doasyncfree' should be changed to test a flag 40566176Smckusick * that shows whether the associated buffers and inodes have 40666176Smckusick * been written. The flag should be set when the cluster is 40766176Smckusick * started and cleared whenever the buffer or inode is flushed. 40866176Smckusick * We can then check below to see if it is set, and do the 40966176Smckusick * synchronous write only when it has been cleared. 41065999Smckusick */ 41165999Smckusick if (sbap != &ip->i_db[0]) { 41266176Smckusick if (doasyncfree) 41366176Smckusick bdwrite(sbp); 41466176Smckusick else 41566176Smckusick bwrite(sbp); 41665999Smckusick } else { 41765999Smckusick ip->i_flag |= IN_CHANGE | IN_UPDATE; 41866176Smckusick if (!doasyncfree) 41966176Smckusick VOP_UPDATE(vp, &time, &time, MNT_WAIT); 42065999Smckusick } 42165999Smckusick if (ssize < len) 42266176Smckusick if (doasyncfree) 42366176Smckusick bdwrite(ebp); 42466176Smckusick else 42566176Smckusick bwrite(ebp); 42665999Smckusick /* 42765999Smckusick * Last, free the old blocks and assign the new blocks to the buffers. 42865999Smckusick */ 42967871Smckusick #ifdef DEBUG 43067871Smckusick if (prtrealloc) 43167871Smckusick printf("\n\tnew:"); 43267871Smckusick #endif 43365999Smckusick for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) { 43465999Smckusick ffs_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno), 43565999Smckusick fs->fs_bsize); 43665999Smckusick buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno); 43767871Smckusick #ifdef DEBUG 43867871Smckusick if (prtrealloc) 43967871Smckusick printf(" %d,", blkno); 44067871Smckusick #endif 44165999Smckusick } 44267871Smckusick #ifdef DEBUG 44367871Smckusick if (prtrealloc) { 44467871Smckusick prtrealloc--; 44567871Smckusick printf("\n"); 44667871Smckusick } 44767871Smckusick #endif 44865999Smckusick return (0); 44965999Smckusick 45065999Smckusick fail: 45165999Smckusick if (ssize < len) 45265999Smckusick brelse(ebp); 45365999Smckusick if (sbap != &ip->i_db[0]) 45465999Smckusick brelse(sbp); 45565999Smckusick return (ENOSPC); 45665999Smckusick } 45765999Smckusick 45865999Smckusick /* 4595375Smckusic * Allocate an inode in the file system. 4605375Smckusic * 46151469Sbostic * If allocating a directory, use ffs_dirpref to select the inode. 46251469Sbostic * If allocating in a directory, the following hierarchy is followed: 46351469Sbostic * 1) allocate the preferred inode. 4645375Smckusic * 2) allocate an inode in the same cylinder group. 4655375Smckusic * 3) quadradically rehash into other cylinder groups, until an 4665375Smckusic * available inode is located. 4675375Smckusic * If no inode preference is given the following heirarchy is used 4685375Smckusic * to allocate an inode: 4695375Smckusic * 1) allocate an inode in cylinder group 0. 4705375Smckusic * 2) quadradically rehash into other cylinder groups, until an 4715375Smckusic * available inode is located. 4725375Smckusic */ 47354653Smckusick ffs_valloc(ap) 47454653Smckusick struct vop_valloc_args /* { 47554653Smckusick struct vnode *a_pvp; 47654653Smckusick int a_mode; 47754653Smckusick struct ucred *a_cred; 47854653Smckusick struct vnode **a_vpp; 47954653Smckusick } */ *ap; 4804359Smckusick { 48153865Sheideman register struct vnode *pvp = ap->a_pvp; 48251541Smckusick register struct inode *pip; 4834359Smckusick register struct fs *fs; 4844359Smckusick register struct inode *ip; 48554653Smckusick mode_t mode = ap->a_mode; 48651469Sbostic ino_t ino, ipref; 48737735Smckusick int cg, error; 4884359Smckusick 48953583Sheideman *ap->a_vpp = NULL; 49053865Sheideman pip = VTOI(pvp); 4915965Smckusic fs = pip->i_fs; 4924792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 4934359Smckusick goto noinodes; 49451469Sbostic 49554653Smckusick if ((mode & IFMT) == IFDIR) 49651541Smckusick ipref = ffs_dirpref(fs); 49751469Sbostic else 49851469Sbostic ipref = pip->i_number; 4994948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 5004948Smckusic ipref = 0; 50164603Sbostic cg = ino_to_cg(fs, ipref); 50264603Sbostic ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg); 5034359Smckusick if (ino == 0) 5044359Smckusick goto noinodes; 50554653Smckusick error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp); 50637735Smckusick if (error) { 50754653Smckusick VOP_VFREE(pvp, ino, mode); 50837735Smckusick return (error); 5094359Smckusick } 51053583Sheideman ip = VTOI(*ap->a_vpp); 5116716Smckusick if (ip->i_mode) { 51254653Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 5136716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 51451541Smckusick panic("ffs_valloc: dup alloc"); 5156716Smckusick } 51612643Ssam if (ip->i_blocks) { /* XXX */ 51712643Ssam printf("free inode %s/%d had %d blocks\n", 51812643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 51912643Ssam ip->i_blocks = 0; 52012643Ssam } 52139516Smckusick ip->i_flags = 0; 52238255Smckusick /* 52338255Smckusick * Set up a new generation number for this inode. 52438255Smckusick */ 52538255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 52638255Smckusick nextgennumber = time.tv_sec; 52738255Smckusick ip->i_gen = nextgennumber; 52837735Smckusick return (0); 5294359Smckusick noinodes: 53053583Sheideman ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes"); 5316294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 53237735Smckusick return (ENOSPC); 5334359Smckusick } 5344359Smckusick 5354651Smckusic /* 5365375Smckusic * Find a cylinder to place a directory. 5375375Smckusic * 5385375Smckusic * The policy implemented by this algorithm is to select from 5395375Smckusic * among those cylinder groups with above the average number of 5405375Smckusic * free inodes, the one with the smallest number of directories. 5414651Smckusic */ 54251469Sbostic static ino_t 54351469Sbostic ffs_dirpref(fs) 5445965Smckusic register struct fs *fs; 5454359Smckusick { 5464651Smckusic int cg, minndir, mincg, avgifree; 5474359Smckusick 5484792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 5494651Smckusic minndir = fs->fs_ipg; 5504359Smckusick mincg = 0; 5514651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 5525322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 5535322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 5544359Smckusick mincg = cg; 5555322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 5564359Smckusick } 5579163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 5584359Smckusick } 5594359Smckusick 5604651Smckusic /* 5619163Ssam * Select the desired position for the next block in a file. The file is 5629163Ssam * logically divided into sections. The first section is composed of the 5639163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 5649163Ssam * 5659163Ssam * If no blocks have been allocated in the first section, the policy is to 5669163Ssam * request a block in the same cylinder group as the inode that describes 5679163Ssam * the file. If no blocks have been allocated in any other section, the 5689163Ssam * policy is to place the section in a cylinder group with a greater than 5699163Ssam * average number of free blocks. An appropriate cylinder group is found 57017696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of 57117696Smckusick * blocks is needed, the sweep begins in the cylinder group following the 57217696Smckusick * cylinder group from which the previous allocation was made. The sweep 57317696Smckusick * continues until a cylinder group with greater than the average number 57417696Smckusick * of free blocks is found. If the allocation is for the first block in an 57517696Smckusick * indirect block, the information on the previous allocation is unavailable; 57617696Smckusick * here a best guess is made based upon the logical block number being 57717696Smckusick * allocated. 5789163Ssam * 5799163Ssam * If a section is already partially allocated, the policy is to 5809163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 5819163Ssam * contiguous blocks and the beginning of the next is physically separated 5829163Ssam * so that the disk head will be in transit between them for at least 5839163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 5849163Ssam * schedule another I/O transfer. 5854651Smckusic */ 5865212Smckusic daddr_t 58751469Sbostic ffs_blkpref(ip, lbn, indx, bap) 5889163Ssam struct inode *ip; 5899163Ssam daddr_t lbn; 5909163Ssam int indx; 5919163Ssam daddr_t *bap; 5929163Ssam { 5935965Smckusic register struct fs *fs; 59417696Smckusick register int cg; 59517696Smckusick int avgbfree, startcg; 5969163Ssam daddr_t nextblk; 5974651Smckusic 5989163Ssam fs = ip->i_fs; 5999163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 6009163Ssam if (lbn < NDADDR) { 60164603Sbostic cg = ino_to_cg(fs, ip->i_number); 6025322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 6034651Smckusic } 6049163Ssam /* 6059163Ssam * Find a cylinder with greater than average number of 6069163Ssam * unused data blocks. 6079163Ssam */ 60817696Smckusick if (indx == 0 || bap[indx - 1] == 0) 60964603Sbostic startcg = 61064603Sbostic ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; 61117696Smckusick else 61217696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1; 61317696Smckusick startcg %= fs->fs_ncg; 6149163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 61517696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++) 6169163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 6179163Ssam fs->fs_cgrotor = cg; 6189163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 6199163Ssam } 62017696Smckusick for (cg = 0; cg <= startcg; cg++) 6219163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 6229163Ssam fs->fs_cgrotor = cg; 6239163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 6249163Ssam } 6259163Ssam return (NULL); 6269163Ssam } 6279163Ssam /* 6289163Ssam * One or more previous blocks have been laid out. If less 6299163Ssam * than fs_maxcontig previous blocks are contiguous, the 6309163Ssam * next block is requested contiguously, otherwise it is 6319163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 6329163Ssam */ 6339163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 63456665Smargo if (indx < fs->fs_maxcontig || bap[indx - fs->fs_maxcontig] + 63556665Smargo blkstofrags(fs, fs->fs_maxcontig) != nextblk) 6369163Ssam return (nextblk); 6379163Ssam if (fs->fs_rotdelay != 0) 6389163Ssam /* 6399163Ssam * Here we convert ms of delay to frags as: 6409163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 6419163Ssam * ((sect/frag) * (ms/sec)) 6429163Ssam * then round up to the next block. 6439163Ssam */ 6449163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 6459163Ssam (NSPF(fs) * 1000), fs->fs_frag); 6469163Ssam return (nextblk); 6474651Smckusic } 6484651Smckusic 6495375Smckusic /* 6505375Smckusic * Implement the cylinder overflow algorithm. 6515375Smckusic * 6525375Smckusic * The policy implemented by this algorithm is: 6535375Smckusic * 1) allocate the block in its requested cylinder group. 6545375Smckusic * 2) quadradically rehash on the cylinder group number. 6555375Smckusic * 3) brute force search for a free block. 6565375Smckusic */ 6575212Smckusic /*VARARGS5*/ 65851469Sbostic static u_long 65951469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator) 6605965Smckusic struct inode *ip; 6614359Smckusick int cg; 6624359Smckusick long pref; 6634359Smckusick int size; /* size for data blocks, mode for inodes */ 6645212Smckusic u_long (*allocator)(); 6654359Smckusick { 6665965Smckusic register struct fs *fs; 6674359Smckusick long result; 6684359Smckusick int i, icg = cg; 6694359Smckusick 6705965Smckusic fs = ip->i_fs; 6714359Smckusick /* 6724359Smckusick * 1: preferred cylinder group 6734359Smckusick */ 6745965Smckusic result = (*allocator)(ip, cg, pref, size); 6754359Smckusick if (result) 6764359Smckusick return (result); 6774359Smckusick /* 6784359Smckusick * 2: quadratic rehash 6794359Smckusick */ 6804359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 6814359Smckusick cg += i; 6824359Smckusick if (cg >= fs->fs_ncg) 6834359Smckusick cg -= fs->fs_ncg; 6845965Smckusic result = (*allocator)(ip, cg, 0, size); 6854359Smckusick if (result) 6864359Smckusick return (result); 6874359Smckusick } 6884359Smckusick /* 6894359Smckusick * 3: brute force search 69010847Ssam * Note that we start at i == 2, since 0 was checked initially, 69110847Ssam * and 1 is always checked in the quadratic rehash. 6924359Smckusick */ 69310848Smckusick cg = (icg + 2) % fs->fs_ncg; 69410847Ssam for (i = 2; i < fs->fs_ncg; i++) { 6955965Smckusic result = (*allocator)(ip, cg, 0, size); 6964359Smckusick if (result) 6974359Smckusick return (result); 6984359Smckusick cg++; 6994359Smckusick if (cg == fs->fs_ncg) 7004359Smckusick cg = 0; 7014359Smckusick } 7026294Smckusick return (NULL); 7034359Smckusick } 7044359Smckusick 7055375Smckusic /* 7065375Smckusic * Determine whether a fragment can be extended. 7075375Smckusic * 7085375Smckusic * Check to see if the necessary fragments are available, and 7095375Smckusic * if they are, allocate them. 7105375Smckusic */ 71151469Sbostic static daddr_t 71251469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize) 7135965Smckusic struct inode *ip; 7144426Smckusic int cg; 7154463Smckusic long bprev; 7164426Smckusic int osize, nsize; 7174426Smckusic { 7185965Smckusic register struct fs *fs; 7194463Smckusic register struct cg *cgp; 72037735Smckusick struct buf *bp; 7214463Smckusic long bno; 7224463Smckusic int frags, bbase; 72337735Smckusick int i, error; 7244426Smckusic 7255965Smckusic fs = ip->i_fs; 72617224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 7276531Smckusick return (NULL); 7285960Smckusic frags = numfrags(fs, nsize); 72917224Smckusick bbase = fragnum(fs, bprev); 73017224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 73130749Skarels /* cannot extend across a block boundary */ 7326294Smckusick return (NULL); 7334463Smckusic } 73437735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 73538776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 73637735Smckusick if (error) { 73737735Smckusick brelse(bp); 73837735Smckusick return (NULL); 73937735Smckusick } 74064508Sbostic cgp = (struct cg *)bp->b_data; 74137735Smckusick if (!cg_chkmagic(cgp)) { 7425960Smckusic brelse(bp); 7436294Smckusick return (NULL); 7445960Smckusic } 7458105Sroot cgp->cg_time = time.tv_sec; 7465377Smckusic bno = dtogd(fs, bprev); 7475960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 74834143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) { 7495361Smckusic brelse(bp); 7506294Smckusick return (NULL); 7515361Smckusic } 7525361Smckusic /* 7535361Smckusic * the current fragment can be extended 7545361Smckusic * deduct the count on fragment being extended into 7555361Smckusic * increase the count on the remaining fragment (if any) 7565361Smckusic * allocate the extended piece 7575361Smckusic */ 7585361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 75934143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) 7604463Smckusic break; 7615960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 7625361Smckusic if (i != frags) 7635361Smckusic cgp->cg_frsum[i - frags]++; 7645960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 76534143Smckusick clrbit(cg_blksfree(cgp), bno + i); 7665361Smckusic cgp->cg_cs.cs_nffree--; 7675361Smckusic fs->fs_cstotal.cs_nffree--; 7685361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 7694463Smckusic } 77050893Smckusick fs->fs_fmod = 1; 7715361Smckusic bdwrite(bp); 7725361Smckusic return (bprev); 7734426Smckusic } 7744426Smckusic 7755375Smckusic /* 7765375Smckusic * Determine whether a block can be allocated. 7775375Smckusic * 77864603Sbostic * Check to see if a block of the appropriate size is available, 7795375Smckusic * and if it is, allocate it. 7805375Smckusic */ 78151779Smarc static daddr_t 78251469Sbostic ffs_alloccg(ip, cg, bpref, size) 7835965Smckusic struct inode *ip; 7844359Smckusick int cg; 7854359Smckusick daddr_t bpref; 7864359Smckusick int size; 7874359Smckusick { 7885965Smckusic register struct fs *fs; 7894463Smckusic register struct cg *cgp; 79037735Smckusick struct buf *bp; 7914463Smckusic register int i; 79237735Smckusick int error, bno, frags, allocsiz; 7934359Smckusick 7945965Smckusic fs = ip->i_fs; 7955322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 7966294Smckusick return (NULL); 79737735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 79838776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 79937735Smckusick if (error) { 80037735Smckusick brelse(bp); 80137735Smckusick return (NULL); 80237735Smckusick } 80364508Sbostic cgp = (struct cg *)bp->b_data; 80437735Smckusick if (!cg_chkmagic(cgp) || 80515950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 8065960Smckusic brelse(bp); 8076294Smckusick return (NULL); 8085960Smckusic } 8098105Sroot cgp->cg_time = time.tv_sec; 8105322Smckusic if (size == fs->fs_bsize) { 81151469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref); 8124463Smckusic bdwrite(bp); 8134463Smckusic return (bno); 8144463Smckusic } 8154463Smckusic /* 8164463Smckusic * check to see if any fragments are already available 8174463Smckusic * allocsiz is the size which will be allocated, hacking 8184463Smckusic * it down to a smaller size if necessary 8194463Smckusic */ 8205960Smckusic frags = numfrags(fs, size); 8215322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 8224463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 8234463Smckusic break; 8245322Smckusic if (allocsiz == fs->fs_frag) { 8254463Smckusic /* 8264463Smckusic * no fragments were available, so a block will be 8274463Smckusic * allocated, and hacked up 8284463Smckusic */ 8294792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 8304463Smckusic brelse(bp); 8316294Smckusick return (NULL); 8324463Smckusic } 83351469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref); 8345377Smckusic bpref = dtogd(fs, bno); 8355322Smckusic for (i = frags; i < fs->fs_frag; i++) 83634143Smckusick setbit(cg_blksfree(cgp), bpref + i); 8375322Smckusic i = fs->fs_frag - frags; 8384792Smckusic cgp->cg_cs.cs_nffree += i; 8394792Smckusic fs->fs_cstotal.cs_nffree += i; 8405322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 84150893Smckusick fs->fs_fmod = 1; 8424463Smckusic cgp->cg_frsum[i]++; 8434463Smckusic bdwrite(bp); 8444463Smckusic return (bno); 8454463Smckusic } 84651469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); 84715950Smckusick if (bno < 0) { 84815950Smckusick brelse(bp); 8496294Smckusick return (NULL); 85015950Smckusick } 8514463Smckusic for (i = 0; i < frags; i++) 85234143Smckusick clrbit(cg_blksfree(cgp), bno + i); 8534792Smckusic cgp->cg_cs.cs_nffree -= frags; 8544792Smckusic fs->fs_cstotal.cs_nffree -= frags; 8555322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 85650893Smckusick fs->fs_fmod = 1; 8574463Smckusic cgp->cg_frsum[allocsiz]--; 8584463Smckusic if (frags != allocsiz) 8594463Smckusic cgp->cg_frsum[allocsiz - frags]++; 8604463Smckusic bdwrite(bp); 8614463Smckusic return (cg * fs->fs_fpg + bno); 8624463Smckusic } 8634463Smckusic 8645375Smckusic /* 8655375Smckusic * Allocate a block in a cylinder group. 8665375Smckusic * 8675375Smckusic * This algorithm implements the following policy: 8685375Smckusic * 1) allocate the requested block. 8695375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 8705375Smckusic * 3) allocate the next available block on the block rotor for the 8715375Smckusic * specified cylinder group. 8725375Smckusic * Note that this routine only allocates fs_bsize blocks; these 8735375Smckusic * blocks may be fragmented by the routine that allocates them. 8745375Smckusic */ 87551469Sbostic static daddr_t 87651469Sbostic ffs_alloccgblk(fs, cgp, bpref) 8775965Smckusic register struct fs *fs; 8784463Smckusic register struct cg *cgp; 8794463Smckusic daddr_t bpref; 8804463Smckusic { 88165999Smckusick daddr_t bno, blkno; 8826294Smckusick int cylno, pos, delta; 8834651Smckusic short *cylbp; 8845361Smckusic register int i; 8854463Smckusic 88665999Smckusick if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) { 8874651Smckusic bpref = cgp->cg_rotor; 8885361Smckusic goto norot; 8895361Smckusic } 89017224Smckusick bpref = blknum(fs, bpref); 8915377Smckusic bpref = dtogd(fs, bpref); 8925361Smckusic /* 8935361Smckusic * if the requested block is available, use it 8945361Smckusic */ 89551469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 8965361Smckusic bno = bpref; 8975361Smckusic goto gotit; 8985361Smckusic } 8995361Smckusic /* 9005361Smckusic * check for a block available on the same cylinder 9015361Smckusic */ 9025361Smckusic cylno = cbtocylno(fs, bpref); 90334143Smckusick if (cg_blktot(cgp)[cylno] == 0) 9045375Smckusic goto norot; 9055375Smckusic if (fs->fs_cpc == 0) { 9065375Smckusic /* 90757000Smckusick * Block layout information is not available. 90857000Smckusick * Leaving bpref unchanged means we take the 90957000Smckusick * next available free block following the one 91057000Smckusick * we just allocated. Hopefully this will at 91157000Smckusick * least hit a track cache on drives of unknown 91257000Smckusick * geometry (e.g. SCSI). 9135375Smckusic */ 9145375Smckusic goto norot; 9155375Smckusic } 9165375Smckusic /* 9175361Smckusic * check the summary information to see if a block is 9185361Smckusic * available in the requested cylinder starting at the 9199163Ssam * requested rotational position and proceeding around. 9205361Smckusic */ 92134143Smckusick cylbp = cg_blks(fs, cgp, cylno); 9229163Ssam pos = cbtorpos(fs, bpref); 92334143Smckusick for (i = pos; i < fs->fs_nrpos; i++) 9245361Smckusic if (cylbp[i] > 0) 9255361Smckusic break; 92634143Smckusick if (i == fs->fs_nrpos) 9275361Smckusic for (i = 0; i < pos; i++) 9285361Smckusic if (cylbp[i] > 0) 9295361Smckusic break; 9305361Smckusic if (cylbp[i] > 0) { 9314651Smckusic /* 9325361Smckusic * found a rotational position, now find the actual 9335361Smckusic * block. A panic if none is actually there. 9344651Smckusic */ 9355361Smckusic pos = cylno % fs->fs_cpc; 9365361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 93734143Smckusick if (fs_postbl(fs, pos)[i] == -1) { 9386716Smckusick printf("pos = %d, i = %d, fs = %s\n", 9396716Smckusick pos, i, fs->fs_fsmnt); 94051469Sbostic panic("ffs_alloccgblk: cyl groups corrupted"); 9416716Smckusick } 94234143Smckusick for (i = fs_postbl(fs, pos)[i];; ) { 94351469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) { 94411638Ssam bno = blkstofrags(fs, (bno + i)); 9455361Smckusic goto gotit; 9465361Smckusic } 94734143Smckusick delta = fs_rotbl(fs)[i]; 94834143Smckusick if (delta <= 0 || 94934143Smckusick delta + i > fragstoblks(fs, fs->fs_fpg)) 9504651Smckusic break; 9516294Smckusick i += delta; 9524651Smckusic } 9536716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 95451469Sbostic panic("ffs_alloccgblk: can't find blk in cyl"); 9554359Smckusick } 9565361Smckusic norot: 9575361Smckusic /* 9585361Smckusic * no blocks in the requested cylinder, so take next 9595361Smckusic * available one in this cylinder group. 9605361Smckusic */ 96151469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 9626567Smckusic if (bno < 0) 9636294Smckusick return (NULL); 9644651Smckusic cgp->cg_rotor = bno; 9654359Smckusick gotit: 96665999Smckusick blkno = fragstoblks(fs, bno); 96765999Smckusick ffs_clrblock(fs, cg_blksfree(cgp), (long)blkno); 96865999Smckusick ffs_clusteracct(fs, cgp, blkno, -1); 9694792Smckusic cgp->cg_cs.cs_nbfree--; 9704792Smckusic fs->fs_cstotal.cs_nbfree--; 9715322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 9725375Smckusic cylno = cbtocylno(fs, bno); 97334143Smckusick cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 97434143Smckusick cg_blktot(cgp)[cylno]--; 97550893Smckusick fs->fs_fmod = 1; 9764651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 9774359Smckusick } 97834143Smckusick 9795375Smckusic /* 98065999Smckusick * Determine whether a cluster can be allocated. 98165999Smckusick * 98265999Smckusick * We do not currently check for optimal rotational layout if there 98365999Smckusick * are multiple choices in the same cylinder group. Instead we just 98465999Smckusick * take the first one that we find following bpref. 98565999Smckusick */ 98665999Smckusick static daddr_t 98765999Smckusick ffs_clusteralloc(ip, cg, bpref, len) 98865999Smckusick struct inode *ip; 98965999Smckusick int cg; 99065999Smckusick daddr_t bpref; 99165999Smckusick int len; 99265999Smckusick { 99365999Smckusick register struct fs *fs; 99465999Smckusick register struct cg *cgp; 99565999Smckusick struct buf *bp; 99665999Smckusick int i, run, bno, bit, map; 99765999Smckusick u_char *mapp; 99867869Smckusick int32_t *lp; 99965999Smckusick 100065999Smckusick fs = ip->i_fs; 100167869Smckusick if (fs->fs_maxcluster[cg] < len) 100265999Smckusick return (NULL); 100365999Smckusick if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, 100465999Smckusick NOCRED, &bp)) 100565999Smckusick goto fail; 100665999Smckusick cgp = (struct cg *)bp->b_data; 100765999Smckusick if (!cg_chkmagic(cgp)) 100865999Smckusick goto fail; 100965999Smckusick /* 101065999Smckusick * Check to see if a cluster of the needed size (or bigger) is 101165999Smckusick * available in this cylinder group. 101265999Smckusick */ 101367869Smckusick lp = &cg_clustersum(cgp)[len]; 101465999Smckusick for (i = len; i <= fs->fs_contigsumsize; i++) 101567869Smckusick if (*lp++ > 0) 101665999Smckusick break; 101767869Smckusick if (i > fs->fs_contigsumsize) { 101867869Smckusick /* 101967869Smckusick * This is the first time looking for a cluster in this 102067869Smckusick * cylinder group. Update the cluster summary information 102167869Smckusick * to reflect the true maximum sized cluster so that 102267869Smckusick * future cluster allocation requests can avoid reading 102367869Smckusick * the cylinder group map only to find no clusters. 102467869Smckusick */ 102567869Smckusick lp = &cg_clustersum(cgp)[len - 1]; 102667869Smckusick for (i = len - 1; i > 0; i--) 102767869Smckusick if (*lp-- > 0) 102867869Smckusick break; 102967869Smckusick fs->fs_maxcluster[cg] = i; 103065999Smckusick goto fail; 103167869Smckusick } 103265999Smckusick /* 103365999Smckusick * Search the cluster map to find a big enough cluster. 103465999Smckusick * We take the first one that we find, even if it is larger 103565999Smckusick * than we need as we prefer to get one close to the previous 103665999Smckusick * block allocation. We do not search before the current 103765999Smckusick * preference point as we do not want to allocate a block 103865999Smckusick * that is allocated before the previous one (as we will 103965999Smckusick * then have to wait for another pass of the elevator 104065999Smckusick * algorithm before it will be read). We prefer to fail and 104165999Smckusick * be recalled to try an allocation in the next cylinder group. 104265999Smckusick */ 104365999Smckusick if (dtog(fs, bpref) != cg) 104465999Smckusick bpref = 0; 104565999Smckusick else 104665999Smckusick bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref))); 104765999Smckusick mapp = &cg_clustersfree(cgp)[bpref / NBBY]; 104865999Smckusick map = *mapp++; 104965999Smckusick bit = 1 << (bpref % NBBY); 105065999Smckusick for (run = 0, i = bpref; i < cgp->cg_nclusterblks; i++) { 105165999Smckusick if ((map & bit) == 0) { 105265999Smckusick run = 0; 105365999Smckusick } else { 105465999Smckusick run++; 105565999Smckusick if (run == len) 105665999Smckusick break; 105765999Smckusick } 105865999Smckusick if ((i & (NBBY - 1)) != (NBBY - 1)) { 105965999Smckusick bit <<= 1; 106065999Smckusick } else { 106165999Smckusick map = *mapp++; 106265999Smckusick bit = 1; 106365999Smckusick } 106465999Smckusick } 106565999Smckusick if (i == cgp->cg_nclusterblks) 106665999Smckusick goto fail; 106765999Smckusick /* 106865999Smckusick * Allocate the cluster that we have found. 106965999Smckusick */ 107065999Smckusick bno = cg * fs->fs_fpg + blkstofrags(fs, i - run + 1); 107165999Smckusick len = blkstofrags(fs, len); 107265999Smckusick for (i = 0; i < len; i += fs->fs_frag) 107365999Smckusick if (ffs_alloccgblk(fs, cgp, bno + i) != bno + i) 107465999Smckusick panic("ffs_clusteralloc: lost block"); 107565999Smckusick brelse(bp); 107665999Smckusick return (bno); 107765999Smckusick 107865999Smckusick fail: 107965999Smckusick brelse(bp); 108065999Smckusick return (0); 108165999Smckusick } 108265999Smckusick 108365999Smckusick /* 10845375Smckusic * Determine whether an inode can be allocated. 10855375Smckusic * 10865375Smckusic * Check to see if an inode is available, and if it is, 10875375Smckusic * allocate it using the following policy: 10885375Smckusic * 1) allocate the requested inode. 10895375Smckusic * 2) allocate the next available inode after the requested 10905375Smckusic * inode in the specified cylinder group. 10915375Smckusic */ 109267869Smckusick static u_long 109364603Sbostic ffs_nodealloccg(ip, cg, ipref, mode) 10945965Smckusic struct inode *ip; 10954359Smckusick int cg; 10964359Smckusick daddr_t ipref; 10974359Smckusick int mode; 10984359Smckusick { 10995965Smckusic register struct fs *fs; 11004463Smckusic register struct cg *cgp; 110116784Smckusick struct buf *bp; 110237735Smckusick int error, start, len, loc, map, i; 11034359Smckusick 11045965Smckusic fs = ip->i_fs; 11055322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 11066294Smckusick return (NULL); 110737735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 110838776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 110937735Smckusick if (error) { 111037735Smckusick brelse(bp); 111137735Smckusick return (NULL); 111237735Smckusick } 111364508Sbostic cgp = (struct cg *)bp->b_data; 111437735Smckusick if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 11155960Smckusic brelse(bp); 11166294Smckusick return (NULL); 11175960Smckusic } 11188105Sroot cgp->cg_time = time.tv_sec; 11194359Smckusick if (ipref) { 11204359Smckusick ipref %= fs->fs_ipg; 112134143Smckusick if (isclr(cg_inosused(cgp), ipref)) 11224359Smckusick goto gotit; 112316784Smckusick } 112416784Smckusick start = cgp->cg_irotor / NBBY; 112516784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 112634143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 112716784Smckusick if (loc == 0) { 112817697Smckusick len = start + 1; 112917697Smckusick start = 0; 113034143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 113117697Smckusick if (loc == 0) { 113265718Sbostic printf("cg = %d, irotor = %d, fs = %s\n", 113317697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 113464603Sbostic panic("ffs_nodealloccg: map corrupted"); 113517697Smckusick /* NOTREACHED */ 113617697Smckusick } 113716784Smckusick } 113816784Smckusick i = start + len - loc; 113934143Smckusick map = cg_inosused(cgp)[i]; 114016784Smckusick ipref = i * NBBY; 114116784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 114216784Smckusick if ((map & i) == 0) { 11434359Smckusick cgp->cg_irotor = ipref; 11444359Smckusick goto gotit; 11454359Smckusick } 11464359Smckusick } 114716784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 114864603Sbostic panic("ffs_nodealloccg: block not in map"); 114916784Smckusick /* NOTREACHED */ 11504359Smckusick gotit: 115134143Smckusick setbit(cg_inosused(cgp), ipref); 11524792Smckusic cgp->cg_cs.cs_nifree--; 11534792Smckusic fs->fs_cstotal.cs_nifree--; 11545322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 115550893Smckusick fs->fs_fmod = 1; 11564359Smckusick if ((mode & IFMT) == IFDIR) { 11574792Smckusic cgp->cg_cs.cs_ndir++; 11584792Smckusic fs->fs_cstotal.cs_ndir++; 11595322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 11604359Smckusick } 11614359Smckusick bdwrite(bp); 11624359Smckusick return (cg * fs->fs_ipg + ipref); 11634359Smckusick } 11644359Smckusick 11655375Smckusic /* 11665375Smckusic * Free a block or fragment. 11675375Smckusic * 11685375Smckusic * The specified block or fragment is placed back in the 11695375Smckusic * free map. If a fragment is deallocated, a possible 11705375Smckusic * block reassembly is checked. 11715375Smckusic */ 117251469Sbostic ffs_blkfree(ip, bno, size) 11735965Smckusic register struct inode *ip; 11744359Smckusick daddr_t bno; 117553059Smckusick long size; 11764359Smckusick { 11774359Smckusick register struct fs *fs; 11784359Smckusick register struct cg *cgp; 117937735Smckusick struct buf *bp; 118065999Smckusick daddr_t blkno; 118165999Smckusick int i, error, cg, blk, frags, bbase; 11824359Smckusick 11835965Smckusic fs = ip->i_fs; 118464508Sbostic if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { 11856716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 11866716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 118731402Smckusick panic("blkfree: bad size"); 11886716Smckusick } 11895377Smckusic cg = dtog(fs, bno); 119064508Sbostic if ((u_int)bno >= fs->fs_size) { 11916567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 119253244Smckusick ffs_fserr(fs, ip->i_uid, "bad block"); 11934359Smckusick return; 11946567Smckusic } 119537735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 119638776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 119737735Smckusick if (error) { 119837735Smckusick brelse(bp); 119937735Smckusick return; 120037735Smckusick } 120164508Sbostic cgp = (struct cg *)bp->b_data; 120237735Smckusick if (!cg_chkmagic(cgp)) { 12035960Smckusic brelse(bp); 12044359Smckusick return; 12055960Smckusic } 12068105Sroot cgp->cg_time = time.tv_sec; 12075377Smckusic bno = dtogd(fs, bno); 12085322Smckusic if (size == fs->fs_bsize) { 120965999Smckusick blkno = fragstoblks(fs, bno); 121065999Smckusick if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) { 12116716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 12126716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 121331402Smckusick panic("blkfree: freeing free block"); 12146567Smckusic } 121565999Smckusick ffs_setblock(fs, cg_blksfree(cgp), blkno); 121665999Smckusick ffs_clusteracct(fs, cgp, blkno, 1); 12174792Smckusic cgp->cg_cs.cs_nbfree++; 12184792Smckusic fs->fs_cstotal.cs_nbfree++; 12195322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 12205375Smckusic i = cbtocylno(fs, bno); 122134143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; 122234143Smckusick cg_blktot(cgp)[i]++; 12234426Smckusic } else { 122417224Smckusick bbase = bno - fragnum(fs, bno); 12254463Smckusic /* 12264463Smckusic * decrement the counts associated with the old frags 12274463Smckusic */ 122834143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 122951469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 12304463Smckusic /* 12314463Smckusic * deallocate the fragment 12324463Smckusic */ 12335960Smckusic frags = numfrags(fs, size); 12344463Smckusic for (i = 0; i < frags; i++) { 123534143Smckusick if (isset(cg_blksfree(cgp), bno + i)) { 12366716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 12376716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 123831402Smckusick panic("blkfree: freeing free frag"); 12396716Smckusick } 124034143Smckusick setbit(cg_blksfree(cgp), bno + i); 12414426Smckusic } 12426294Smckusick cgp->cg_cs.cs_nffree += i; 12436294Smckusick fs->fs_cstotal.cs_nffree += i; 12446294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 12454463Smckusic /* 12464463Smckusic * add back in counts associated with the new frags 12474463Smckusic */ 124834143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 124951469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 12504463Smckusic /* 12514463Smckusic * if a complete block has been reassembled, account for it 12524463Smckusic */ 125365999Smckusick blkno = fragstoblks(fs, bbase); 125465999Smckusick if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) { 12555322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 12565322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 12575322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 125865999Smckusick ffs_clusteracct(fs, cgp, blkno, 1); 12594792Smckusic cgp->cg_cs.cs_nbfree++; 12604792Smckusic fs->fs_cstotal.cs_nbfree++; 12615322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 12625375Smckusic i = cbtocylno(fs, bbase); 126334143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 126434143Smckusick cg_blktot(cgp)[i]++; 12654426Smckusic } 12664426Smckusic } 126750893Smckusick fs->fs_fmod = 1; 12684359Smckusick bdwrite(bp); 12694359Smckusick } 12704359Smckusick 12715375Smckusic /* 12725375Smckusic * Free an inode. 12735375Smckusic * 12745375Smckusic * The specified inode is placed back in the free map. 12755375Smckusic */ 127653577Sheideman int 127754653Smckusick ffs_vfree(ap) 127854653Smckusick struct vop_vfree_args /* { 127954653Smckusick struct vnode *a_pvp; 128054653Smckusick ino_t a_ino; 128154653Smckusick int a_mode; 128254653Smckusick } */ *ap; 12834359Smckusick { 12844359Smckusick register struct fs *fs; 12854359Smckusick register struct cg *cgp; 128651541Smckusick register struct inode *pip; 128754653Smckusick ino_t ino = ap->a_ino; 128837735Smckusick struct buf *bp; 128937735Smckusick int error, cg; 12904359Smckusick 129153583Sheideman pip = VTOI(ap->a_pvp); 129251469Sbostic fs = pip->i_fs; 129354653Smckusick if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg) 129454653Smckusick panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n", 129554653Smckusick pip->i_dev, ino, fs->fs_fsmnt); 129664603Sbostic cg = ino_to_cg(fs, ino); 129751469Sbostic error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 129838776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 129937735Smckusick if (error) { 130037735Smckusick brelse(bp); 130153577Sheideman return (0); 130237735Smckusick } 130364508Sbostic cgp = (struct cg *)bp->b_data; 130437735Smckusick if (!cg_chkmagic(cgp)) { 13055960Smckusic brelse(bp); 130653577Sheideman return (0); 13075960Smckusic } 13088105Sroot cgp->cg_time = time.tv_sec; 130954653Smckusick ino %= fs->fs_ipg; 131054653Smckusick if (isclr(cg_inosused(cgp), ino)) { 131154653Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 131254653Smckusick pip->i_dev, ino, fs->fs_fsmnt); 131348057Smckusick if (fs->fs_ronly == 0) 131448057Smckusick panic("ifree: freeing free inode"); 13156716Smckusick } 131654653Smckusick clrbit(cg_inosused(cgp), ino); 131754653Smckusick if (ino < cgp->cg_irotor) 131854653Smckusick cgp->cg_irotor = ino; 13194792Smckusic cgp->cg_cs.cs_nifree++; 13204792Smckusic fs->fs_cstotal.cs_nifree++; 13215322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 132253583Sheideman if ((ap->a_mode & IFMT) == IFDIR) { 13234792Smckusic cgp->cg_cs.cs_ndir--; 13244792Smckusic fs->fs_cstotal.cs_ndir--; 13255322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 13264359Smckusick } 132750893Smckusick fs->fs_fmod = 1; 13284359Smckusick bdwrite(bp); 132953577Sheideman return (0); 13304359Smckusick } 13314359Smckusick 13324463Smckusic /* 13335375Smckusic * Find a block of the specified size in the specified cylinder group. 13345375Smckusic * 13354651Smckusic * It is a panic if a request is made to find a block if none are 13364651Smckusic * available. 13374651Smckusic */ 133851469Sbostic static daddr_t 133951469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz) 13404651Smckusic register struct fs *fs; 13414651Smckusic register struct cg *cgp; 13424651Smckusic daddr_t bpref; 13434651Smckusic int allocsiz; 13444651Smckusic { 13454651Smckusic daddr_t bno; 13464651Smckusic int start, len, loc, i; 13474651Smckusic int blk, field, subfield, pos; 13484651Smckusic 13494651Smckusic /* 13504651Smckusic * find the fragment by searching through the free block 13514651Smckusic * map for an appropriate bit pattern 13524651Smckusic */ 13534651Smckusic if (bpref) 13545377Smckusic start = dtogd(fs, bpref) / NBBY; 13554651Smckusic else 13564651Smckusic start = cgp->cg_frotor / NBBY; 13575398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 135864508Sbostic loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[start], 135934476Smckusick (u_char *)fragtbl[fs->fs_frag], 136034476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 13614651Smckusic if (loc == 0) { 13626531Smckusick len = start + 1; 13636531Smckusick start = 0; 136464508Sbostic loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[0], 136534476Smckusick (u_char *)fragtbl[fs->fs_frag], 136634476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 136716784Smckusick if (loc == 0) { 136816784Smckusick printf("start = %d, len = %d, fs = %s\n", 136916784Smckusick start, len, fs->fs_fsmnt); 137051469Sbostic panic("ffs_alloccg: map corrupted"); 137117697Smckusick /* NOTREACHED */ 137216784Smckusick } 13734651Smckusic } 13744651Smckusic bno = (start + len - loc) * NBBY; 13754651Smckusic cgp->cg_frotor = bno; 13764651Smckusic /* 13774651Smckusic * found the byte in the map 13784651Smckusic * sift through the bits to find the selected frag 13794651Smckusic */ 13806294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 138134143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bno); 13824651Smckusic blk <<= 1; 13834651Smckusic field = around[allocsiz]; 13844651Smckusic subfield = inside[allocsiz]; 13855322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 13866294Smckusick if ((blk & field) == subfield) 13876294Smckusick return (bno + pos); 13884651Smckusic field <<= 1; 13894651Smckusic subfield <<= 1; 13904651Smckusic } 13914651Smckusic } 13926716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 139351469Sbostic panic("ffs_alloccg: block not in map"); 13946531Smckusick return (-1); 13954651Smckusic } 13964651Smckusic 13974651Smckusic /* 139865999Smckusick * Update the cluster map because of an allocation or free. 139965999Smckusick * 140065999Smckusick * Cnt == 1 means free; cnt == -1 means allocating. 140165999Smckusick */ 140265999Smckusick ffs_clusteracct(fs, cgp, blkno, cnt) 140365999Smckusick struct fs *fs; 140465999Smckusick struct cg *cgp; 140565999Smckusick daddr_t blkno; 140665999Smckusick int cnt; 140765999Smckusick { 140865999Smckusick long *sump; 140967869Smckusick int32_t *lp; 141065999Smckusick u_char *freemapp, *mapp; 141165999Smckusick int i, start, end, forw, back, map, bit; 141265999Smckusick 141365999Smckusick if (fs->fs_contigsumsize <= 0) 141465999Smckusick return; 141565999Smckusick freemapp = cg_clustersfree(cgp); 141665999Smckusick sump = cg_clustersum(cgp); 141765999Smckusick /* 141865999Smckusick * Allocate or clear the actual block. 141965999Smckusick */ 142065999Smckusick if (cnt > 0) 142165999Smckusick setbit(freemapp, blkno); 142265999Smckusick else 142365999Smckusick clrbit(freemapp, blkno); 142465999Smckusick /* 142565999Smckusick * Find the size of the cluster going forward. 142665999Smckusick */ 142765999Smckusick start = blkno + 1; 142865999Smckusick end = start + fs->fs_contigsumsize; 142965999Smckusick if (end >= cgp->cg_nclusterblks) 143065999Smckusick end = cgp->cg_nclusterblks; 143165999Smckusick mapp = &freemapp[start / NBBY]; 143265999Smckusick map = *mapp++; 143365999Smckusick bit = 1 << (start % NBBY); 143465999Smckusick for (i = start; i < end; i++) { 143565999Smckusick if ((map & bit) == 0) 143665999Smckusick break; 143765999Smckusick if ((i & (NBBY - 1)) != (NBBY - 1)) { 143865999Smckusick bit <<= 1; 143965999Smckusick } else { 144065999Smckusick map = *mapp++; 144165999Smckusick bit = 1; 144265999Smckusick } 144365999Smckusick } 144465999Smckusick forw = i - start; 144565999Smckusick /* 144665999Smckusick * Find the size of the cluster going backward. 144765999Smckusick */ 144865999Smckusick start = blkno - 1; 144965999Smckusick end = start - fs->fs_contigsumsize; 145065999Smckusick if (end < 0) 145165999Smckusick end = -1; 145265999Smckusick mapp = &freemapp[start / NBBY]; 145365999Smckusick map = *mapp--; 145465999Smckusick bit = 1 << (start % NBBY); 145565999Smckusick for (i = start; i > end; i--) { 145665999Smckusick if ((map & bit) == 0) 145765999Smckusick break; 145865999Smckusick if ((i & (NBBY - 1)) != 0) { 145965999Smckusick bit >>= 1; 146065999Smckusick } else { 146165999Smckusick map = *mapp--; 146265999Smckusick bit = 1 << (NBBY - 1); 146365999Smckusick } 146465999Smckusick } 146565999Smckusick back = start - i; 146665999Smckusick /* 146765999Smckusick * Account for old cluster and the possibly new forward and 146865999Smckusick * back clusters. 146965999Smckusick */ 147065999Smckusick i = back + forw + 1; 147165999Smckusick if (i > fs->fs_contigsumsize) 147265999Smckusick i = fs->fs_contigsumsize; 147365999Smckusick sump[i] += cnt; 147465999Smckusick if (back > 0) 147565999Smckusick sump[back] -= cnt; 147665999Smckusick if (forw > 0) 147765999Smckusick sump[forw] -= cnt; 147867869Smckusick /* 147967869Smckusick * Update cluster summary information. 148067869Smckusick */ 148167869Smckusick lp = &sump[fs->fs_contigsumsize]; 148267869Smckusick for (i = fs->fs_contigsumsize; i > 0; i--) 148367869Smckusick if (*lp-- > 0) 148467869Smckusick break; 148567869Smckusick fs->fs_maxcluster[cgp->cg_cgx] = i; 148665999Smckusick } 148765999Smckusick 148865999Smckusick /* 14895375Smckusic * Fserr prints the name of a file system with an error diagnostic. 14905375Smckusic * 14915375Smckusic * The form of the error message is: 14924359Smckusick * fs: error message 14934359Smckusick */ 149451469Sbostic static void 149551469Sbostic ffs_fserr(fs, uid, cp) 14964359Smckusick struct fs *fs; 149751469Sbostic u_int uid; 14984359Smckusick char *cp; 14994359Smckusick { 15004359Smckusick 150142318Smckusick log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp); 15024359Smckusick } 1503