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*53577Sheideman * @(#)ffs_alloc.c 7.35 (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; 28153519Sheideman #define pvp (ap->a_pvp) 28253519Sheideman #define mode (ap->a_mode) 28353519Sheideman #define cred (ap->a_cred) 28453519Sheideman #define vpp (ap->a_vpp) 2854359Smckusick { 28653519Sheideman USES_VOP_VFREE; 28753519Sheideman USES_VOP_VGET; 28851541Smckusick register struct inode *pip; 2894359Smckusick register struct fs *fs; 2904359Smckusick register struct inode *ip; 29151469Sbostic ino_t ino, ipref; 29237735Smckusick int cg, error; 2934359Smckusick 29451541Smckusick *vpp = NULL; 29551541Smckusick pip = VTOI(pvp); 2965965Smckusic fs = pip->i_fs; 2974792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 2984359Smckusick goto noinodes; 29951469Sbostic 30051469Sbostic if ((mode & IFMT) == IFDIR) 30151541Smckusick ipref = ffs_dirpref(fs); 30251469Sbostic else 30351469Sbostic ipref = pip->i_number; 3044948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 3054948Smckusic ipref = 0; 3065377Smckusic cg = itog(fs, ipref); 30751469Sbostic ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_ialloccg); 3084359Smckusick if (ino == 0) 3094359Smckusick goto noinodes; 31053519Sheideman error = FFS_VGET(pvp->v_mount, ino, vpp); 31137735Smckusick if (error) { 31253519Sheideman VOP_VFREE(pvp, ino, mode); 31337735Smckusick return (error); 3144359Smckusick } 31551541Smckusick ip = VTOI(*vpp); 3166716Smckusick if (ip->i_mode) { 3176716Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 3186716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 31951541Smckusick panic("ffs_valloc: dup alloc"); 3206716Smckusick } 32112643Ssam if (ip->i_blocks) { /* XXX */ 32212643Ssam printf("free inode %s/%d had %d blocks\n", 32312643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 32412643Ssam ip->i_blocks = 0; 32512643Ssam } 32639516Smckusick ip->i_flags = 0; 32738255Smckusick /* 32838255Smckusick * Set up a new generation number for this inode. 32938255Smckusick */ 33038255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 33138255Smckusick nextgennumber = time.tv_sec; 33238255Smckusick ip->i_gen = nextgennumber; 33337735Smckusick return (0); 3344359Smckusick noinodes: 33551469Sbostic ffs_fserr(fs, cred->cr_uid, "out of inodes"); 3366294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 33737735Smckusick return (ENOSPC); 3384359Smckusick } 33953519Sheideman #undef pvp 34053519Sheideman #undef mode 34153519Sheideman #undef cred 34253519Sheideman #undef vpp 3434359Smckusick 3444651Smckusic /* 3455375Smckusic * Find a cylinder to place a directory. 3465375Smckusic * 3475375Smckusic * The policy implemented by this algorithm is to select from 3485375Smckusic * among those cylinder groups with above the average number of 3495375Smckusic * free inodes, the one with the smallest number of directories. 3504651Smckusic */ 35151469Sbostic static ino_t 35251469Sbostic ffs_dirpref(fs) 3535965Smckusic register struct fs *fs; 3544359Smckusick { 3554651Smckusic int cg, minndir, mincg, avgifree; 3564359Smckusick 3574792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 3584651Smckusic minndir = fs->fs_ipg; 3594359Smckusick mincg = 0; 3604651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 3615322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 3625322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 3634359Smckusick mincg = cg; 3645322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 3654359Smckusick } 3669163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 3674359Smckusick } 3684359Smckusick 3694651Smckusic /* 3709163Ssam * Select the desired position for the next block in a file. The file is 3719163Ssam * logically divided into sections. The first section is composed of the 3729163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 3739163Ssam * 3749163Ssam * If no blocks have been allocated in the first section, the policy is to 3759163Ssam * request a block in the same cylinder group as the inode that describes 3769163Ssam * the file. If no blocks have been allocated in any other section, the 3779163Ssam * policy is to place the section in a cylinder group with a greater than 3789163Ssam * average number of free blocks. An appropriate cylinder group is found 37917696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of 38017696Smckusick * blocks is needed, the sweep begins in the cylinder group following the 38117696Smckusick * cylinder group from which the previous allocation was made. The sweep 38217696Smckusick * continues until a cylinder group with greater than the average number 38317696Smckusick * of free blocks is found. If the allocation is for the first block in an 38417696Smckusick * indirect block, the information on the previous allocation is unavailable; 38517696Smckusick * here a best guess is made based upon the logical block number being 38617696Smckusick * allocated. 3879163Ssam * 3889163Ssam * If a section is already partially allocated, the policy is to 3899163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 3909163Ssam * contiguous blocks and the beginning of the next is physically separated 3919163Ssam * so that the disk head will be in transit between them for at least 3929163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 3939163Ssam * schedule another I/O transfer. 3944651Smckusic */ 3955212Smckusic daddr_t 39651469Sbostic ffs_blkpref(ip, lbn, indx, bap) 3979163Ssam struct inode *ip; 3989163Ssam daddr_t lbn; 3999163Ssam int indx; 4009163Ssam daddr_t *bap; 4019163Ssam { 4025965Smckusic register struct fs *fs; 40317696Smckusick register int cg; 40417696Smckusick int avgbfree, startcg; 4059163Ssam daddr_t nextblk; 4064651Smckusic 4079163Ssam fs = ip->i_fs; 4089163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 4099163Ssam if (lbn < NDADDR) { 4109163Ssam cg = itog(fs, ip->i_number); 4115322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 4124651Smckusic } 4139163Ssam /* 4149163Ssam * Find a cylinder with greater than average number of 4159163Ssam * unused data blocks. 4169163Ssam */ 41717696Smckusick if (indx == 0 || bap[indx - 1] == 0) 41817696Smckusick startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; 41917696Smckusick else 42017696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1; 42117696Smckusick startcg %= fs->fs_ncg; 4229163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 42317696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++) 4249163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4259163Ssam fs->fs_cgrotor = cg; 4269163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4279163Ssam } 42817696Smckusick for (cg = 0; cg <= startcg; cg++) 4299163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4309163Ssam fs->fs_cgrotor = cg; 4319163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4329163Ssam } 4339163Ssam return (NULL); 4349163Ssam } 4359163Ssam /* 4369163Ssam * One or more previous blocks have been laid out. If less 4379163Ssam * than fs_maxcontig previous blocks are contiguous, the 4389163Ssam * next block is requested contiguously, otherwise it is 4399163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 4409163Ssam */ 4419163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 4429163Ssam if (indx > fs->fs_maxcontig && 44311638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 4449163Ssam != nextblk) 4459163Ssam return (nextblk); 4469163Ssam if (fs->fs_rotdelay != 0) 4479163Ssam /* 4489163Ssam * Here we convert ms of delay to frags as: 4499163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 4509163Ssam * ((sect/frag) * (ms/sec)) 4519163Ssam * then round up to the next block. 4529163Ssam */ 4539163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 4549163Ssam (NSPF(fs) * 1000), fs->fs_frag); 4559163Ssam return (nextblk); 4564651Smckusic } 4574651Smckusic 4585375Smckusic /* 4595375Smckusic * Implement the cylinder overflow algorithm. 4605375Smckusic * 4615375Smckusic * The policy implemented by this algorithm is: 4625375Smckusic * 1) allocate the block in its requested cylinder group. 4635375Smckusic * 2) quadradically rehash on the cylinder group number. 4645375Smckusic * 3) brute force search for a free block. 4655375Smckusic */ 4665212Smckusic /*VARARGS5*/ 46751469Sbostic static u_long 46851469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator) 4695965Smckusic struct inode *ip; 4704359Smckusick int cg; 4714359Smckusick long pref; 4724359Smckusick int size; /* size for data blocks, mode for inodes */ 4735212Smckusic u_long (*allocator)(); 4744359Smckusick { 4755965Smckusic register struct fs *fs; 4764359Smckusick long result; 4774359Smckusick int i, icg = cg; 4784359Smckusick 4795965Smckusic fs = ip->i_fs; 4804359Smckusick /* 4814359Smckusick * 1: preferred cylinder group 4824359Smckusick */ 4835965Smckusic result = (*allocator)(ip, cg, pref, size); 4844359Smckusick if (result) 4854359Smckusick return (result); 4864359Smckusick /* 4874359Smckusick * 2: quadratic rehash 4884359Smckusick */ 4894359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 4904359Smckusick cg += i; 4914359Smckusick if (cg >= fs->fs_ncg) 4924359Smckusick cg -= fs->fs_ncg; 4935965Smckusic result = (*allocator)(ip, cg, 0, size); 4944359Smckusick if (result) 4954359Smckusick return (result); 4964359Smckusick } 4974359Smckusick /* 4984359Smckusick * 3: brute force search 49910847Ssam * Note that we start at i == 2, since 0 was checked initially, 50010847Ssam * and 1 is always checked in the quadratic rehash. 5014359Smckusick */ 50210848Smckusick cg = (icg + 2) % fs->fs_ncg; 50310847Ssam for (i = 2; i < fs->fs_ncg; i++) { 5045965Smckusic result = (*allocator)(ip, cg, 0, size); 5054359Smckusick if (result) 5064359Smckusick return (result); 5074359Smckusick cg++; 5084359Smckusick if (cg == fs->fs_ncg) 5094359Smckusick cg = 0; 5104359Smckusick } 5116294Smckusick return (NULL); 5124359Smckusick } 5134359Smckusick 5145375Smckusic /* 5155375Smckusic * Determine whether a fragment can be extended. 5165375Smckusic * 5175375Smckusic * Check to see if the necessary fragments are available, and 5185375Smckusic * if they are, allocate them. 5195375Smckusic */ 52051469Sbostic static daddr_t 52151469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize) 5225965Smckusic struct inode *ip; 5234426Smckusic int cg; 5244463Smckusic long bprev; 5254426Smckusic int osize, nsize; 5264426Smckusic { 5275965Smckusic register struct fs *fs; 5284463Smckusic register struct cg *cgp; 52937735Smckusick struct buf *bp; 5304463Smckusic long bno; 5314463Smckusic int frags, bbase; 53237735Smckusick int i, error; 5334426Smckusic 5345965Smckusic fs = ip->i_fs; 53517224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 5366531Smckusick return (NULL); 5375960Smckusic frags = numfrags(fs, nsize); 53817224Smckusick bbase = fragnum(fs, bprev); 53917224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 54030749Skarels /* cannot extend across a block boundary */ 5416294Smckusick return (NULL); 5424463Smckusic } 54337735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 54438776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 54537735Smckusick if (error) { 54637735Smckusick brelse(bp); 54737735Smckusick return (NULL); 54837735Smckusick } 5496531Smckusick cgp = bp->b_un.b_cg; 55037735Smckusick if (!cg_chkmagic(cgp)) { 5515960Smckusic brelse(bp); 5526294Smckusick return (NULL); 5535960Smckusic } 5548105Sroot cgp->cg_time = time.tv_sec; 5555377Smckusic bno = dtogd(fs, bprev); 5565960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 55734143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) { 5585361Smckusic brelse(bp); 5596294Smckusick return (NULL); 5605361Smckusic } 5615361Smckusic /* 5625361Smckusic * the current fragment can be extended 5635361Smckusic * deduct the count on fragment being extended into 5645361Smckusic * increase the count on the remaining fragment (if any) 5655361Smckusic * allocate the extended piece 5665361Smckusic */ 5675361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 56834143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) 5694463Smckusic break; 5705960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 5715361Smckusic if (i != frags) 5725361Smckusic cgp->cg_frsum[i - frags]++; 5735960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 57434143Smckusick clrbit(cg_blksfree(cgp), bno + i); 5755361Smckusic cgp->cg_cs.cs_nffree--; 5765361Smckusic fs->fs_cstotal.cs_nffree--; 5775361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 5784463Smckusic } 57950893Smckusick fs->fs_fmod = 1; 5805361Smckusic bdwrite(bp); 5815361Smckusic return (bprev); 5824426Smckusic } 5834426Smckusic 5845375Smckusic /* 5855375Smckusic * Determine whether a block can be allocated. 5865375Smckusic * 5875375Smckusic * Check to see if a block of the apprpriate size is available, 5885375Smckusic * and if it is, allocate it. 5895375Smckusic */ 59051779Smarc static daddr_t 59151469Sbostic ffs_alloccg(ip, cg, bpref, size) 5925965Smckusic struct inode *ip; 5934359Smckusick int cg; 5944359Smckusick daddr_t bpref; 5954359Smckusick int size; 5964359Smckusick { 5975965Smckusic register struct fs *fs; 5984463Smckusic register struct cg *cgp; 59937735Smckusick struct buf *bp; 6004463Smckusic register int i; 60137735Smckusick int error, bno, frags, allocsiz; 6024359Smckusick 6035965Smckusic fs = ip->i_fs; 6045322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 6056294Smckusick return (NULL); 60637735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 60738776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 60837735Smckusick if (error) { 60937735Smckusick brelse(bp); 61037735Smckusick return (NULL); 61137735Smckusick } 6126531Smckusick cgp = bp->b_un.b_cg; 61337735Smckusick if (!cg_chkmagic(cgp) || 61415950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 6155960Smckusic brelse(bp); 6166294Smckusick return (NULL); 6175960Smckusic } 6188105Sroot cgp->cg_time = time.tv_sec; 6195322Smckusic if (size == fs->fs_bsize) { 62051469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref); 6214463Smckusic bdwrite(bp); 6224463Smckusic return (bno); 6234463Smckusic } 6244463Smckusic /* 6254463Smckusic * check to see if any fragments are already available 6264463Smckusic * allocsiz is the size which will be allocated, hacking 6274463Smckusic * it down to a smaller size if necessary 6284463Smckusic */ 6295960Smckusic frags = numfrags(fs, size); 6305322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 6314463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 6324463Smckusic break; 6335322Smckusic if (allocsiz == fs->fs_frag) { 6344463Smckusic /* 6354463Smckusic * no fragments were available, so a block will be 6364463Smckusic * allocated, and hacked up 6374463Smckusic */ 6384792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 6394463Smckusic brelse(bp); 6406294Smckusick return (NULL); 6414463Smckusic } 64251469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref); 6435377Smckusic bpref = dtogd(fs, bno); 6445322Smckusic for (i = frags; i < fs->fs_frag; i++) 64534143Smckusick setbit(cg_blksfree(cgp), bpref + i); 6465322Smckusic i = fs->fs_frag - frags; 6474792Smckusic cgp->cg_cs.cs_nffree += i; 6484792Smckusic fs->fs_cstotal.cs_nffree += i; 6495322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 65050893Smckusick fs->fs_fmod = 1; 6514463Smckusic cgp->cg_frsum[i]++; 6524463Smckusic bdwrite(bp); 6534463Smckusic return (bno); 6544463Smckusic } 65551469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); 65615950Smckusick if (bno < 0) { 65715950Smckusick brelse(bp); 6586294Smckusick return (NULL); 65915950Smckusick } 6604463Smckusic for (i = 0; i < frags; i++) 66134143Smckusick clrbit(cg_blksfree(cgp), bno + i); 6624792Smckusic cgp->cg_cs.cs_nffree -= frags; 6634792Smckusic fs->fs_cstotal.cs_nffree -= frags; 6645322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 66550893Smckusick fs->fs_fmod = 1; 6664463Smckusic cgp->cg_frsum[allocsiz]--; 6674463Smckusic if (frags != allocsiz) 6684463Smckusic cgp->cg_frsum[allocsiz - frags]++; 6694463Smckusic bdwrite(bp); 6704463Smckusic return (cg * fs->fs_fpg + bno); 6714463Smckusic } 6724463Smckusic 6735375Smckusic /* 6745375Smckusic * Allocate a block in a cylinder group. 6755375Smckusic * 6765375Smckusic * This algorithm implements the following policy: 6775375Smckusic * 1) allocate the requested block. 6785375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 6795375Smckusic * 3) allocate the next available block on the block rotor for the 6805375Smckusic * specified cylinder group. 6815375Smckusic * Note that this routine only allocates fs_bsize blocks; these 6825375Smckusic * blocks may be fragmented by the routine that allocates them. 6835375Smckusic */ 68451469Sbostic static daddr_t 68551469Sbostic ffs_alloccgblk(fs, cgp, bpref) 6865965Smckusic register struct fs *fs; 6874463Smckusic register struct cg *cgp; 6884463Smckusic daddr_t bpref; 6894463Smckusic { 6904651Smckusic daddr_t bno; 6916294Smckusick int cylno, pos, delta; 6924651Smckusic short *cylbp; 6935361Smckusic register int i; 6944463Smckusic 6954651Smckusic if (bpref == 0) { 6964651Smckusic bpref = cgp->cg_rotor; 6975361Smckusic goto norot; 6985361Smckusic } 69917224Smckusick bpref = blknum(fs, bpref); 7005377Smckusic bpref = dtogd(fs, bpref); 7015361Smckusic /* 7025361Smckusic * if the requested block is available, use it 7035361Smckusic */ 70451469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 7055361Smckusic bno = bpref; 7065361Smckusic goto gotit; 7075361Smckusic } 7085361Smckusic /* 7095361Smckusic * check for a block available on the same cylinder 7105361Smckusic */ 7115361Smckusic cylno = cbtocylno(fs, bpref); 71234143Smckusick if (cg_blktot(cgp)[cylno] == 0) 7135375Smckusic goto norot; 7145375Smckusic if (fs->fs_cpc == 0) { 7155375Smckusic /* 7165375Smckusic * block layout info is not available, so just have 7175375Smckusic * to take any block in this cylinder. 7185375Smckusic */ 7195375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 7205375Smckusic goto norot; 7215375Smckusic } 7225375Smckusic /* 7235361Smckusic * check the summary information to see if a block is 7245361Smckusic * available in the requested cylinder starting at the 7259163Ssam * requested rotational position and proceeding around. 7265361Smckusic */ 72734143Smckusick cylbp = cg_blks(fs, cgp, cylno); 7289163Ssam pos = cbtorpos(fs, bpref); 72934143Smckusick for (i = pos; i < fs->fs_nrpos; i++) 7305361Smckusic if (cylbp[i] > 0) 7315361Smckusic break; 73234143Smckusick if (i == fs->fs_nrpos) 7335361Smckusic for (i = 0; i < pos; i++) 7345361Smckusic if (cylbp[i] > 0) 7355361Smckusic break; 7365361Smckusic if (cylbp[i] > 0) { 7374651Smckusic /* 7385361Smckusic * found a rotational position, now find the actual 7395361Smckusic * block. A panic if none is actually there. 7404651Smckusic */ 7415361Smckusic pos = cylno % fs->fs_cpc; 7425361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 74334143Smckusick if (fs_postbl(fs, pos)[i] == -1) { 7446716Smckusick printf("pos = %d, i = %d, fs = %s\n", 7456716Smckusick pos, i, fs->fs_fsmnt); 74651469Sbostic panic("ffs_alloccgblk: cyl groups corrupted"); 7476716Smckusick } 74834143Smckusick for (i = fs_postbl(fs, pos)[i];; ) { 74951469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) { 75011638Ssam bno = blkstofrags(fs, (bno + i)); 7515361Smckusic goto gotit; 7525361Smckusic } 75334143Smckusick delta = fs_rotbl(fs)[i]; 75434143Smckusick if (delta <= 0 || 75534143Smckusick delta + i > fragstoblks(fs, fs->fs_fpg)) 7564651Smckusic break; 7576294Smckusick i += delta; 7584651Smckusic } 7596716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 76051469Sbostic panic("ffs_alloccgblk: can't find blk in cyl"); 7614359Smckusick } 7625361Smckusic norot: 7635361Smckusic /* 7645361Smckusic * no blocks in the requested cylinder, so take next 7655361Smckusic * available one in this cylinder group. 7665361Smckusic */ 76751469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 7686567Smckusic if (bno < 0) 7696294Smckusick return (NULL); 7704651Smckusic cgp->cg_rotor = bno; 7714359Smckusick gotit: 77251469Sbostic ffs_clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno)); 7734792Smckusic cgp->cg_cs.cs_nbfree--; 7744792Smckusic fs->fs_cstotal.cs_nbfree--; 7755322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 7765375Smckusic cylno = cbtocylno(fs, bno); 77734143Smckusick cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 77834143Smckusick cg_blktot(cgp)[cylno]--; 77950893Smckusick fs->fs_fmod = 1; 7804651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 7814359Smckusick } 78234143Smckusick 7835375Smckusic /* 7845375Smckusic * Determine whether an inode can be allocated. 7855375Smckusic * 7865375Smckusic * Check to see if an inode is available, and if it is, 7875375Smckusic * allocate it using the following policy: 7885375Smckusic * 1) allocate the requested inode. 7895375Smckusic * 2) allocate the next available inode after the requested 7905375Smckusic * inode in the specified cylinder group. 7915375Smckusic */ 79251469Sbostic static ino_t 79351469Sbostic ffs_ialloccg(ip, cg, ipref, mode) 7945965Smckusic struct inode *ip; 7954359Smckusick int cg; 7964359Smckusick daddr_t ipref; 7974359Smckusick int mode; 7984359Smckusick { 7995965Smckusic register struct fs *fs; 8004463Smckusic register struct cg *cgp; 80116784Smckusick struct buf *bp; 80237735Smckusick int error, start, len, loc, map, i; 8034359Smckusick 8045965Smckusic fs = ip->i_fs; 8055322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 8066294Smckusick return (NULL); 80737735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 80838776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 80937735Smckusick if (error) { 81037735Smckusick brelse(bp); 81137735Smckusick return (NULL); 81237735Smckusick } 8136531Smckusick cgp = bp->b_un.b_cg; 81437735Smckusick if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 8155960Smckusic brelse(bp); 8166294Smckusick return (NULL); 8175960Smckusic } 8188105Sroot cgp->cg_time = time.tv_sec; 8194359Smckusick if (ipref) { 8204359Smckusick ipref %= fs->fs_ipg; 82134143Smckusick if (isclr(cg_inosused(cgp), ipref)) 8224359Smckusick goto gotit; 82316784Smckusick } 82416784Smckusick start = cgp->cg_irotor / NBBY; 82516784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 82634143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 82716784Smckusick if (loc == 0) { 82817697Smckusick len = start + 1; 82917697Smckusick start = 0; 83034143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 83117697Smckusick if (loc == 0) { 83217697Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 83317697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 83451469Sbostic panic("ffs_ialloccg: map corrupted"); 83517697Smckusick /* NOTREACHED */ 83617697Smckusick } 83716784Smckusick } 83816784Smckusick i = start + len - loc; 83934143Smckusick map = cg_inosused(cgp)[i]; 84016784Smckusick ipref = i * NBBY; 84116784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 84216784Smckusick if ((map & i) == 0) { 8434359Smckusick cgp->cg_irotor = ipref; 8444359Smckusick goto gotit; 8454359Smckusick } 8464359Smckusick } 84716784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 84851469Sbostic panic("ffs_ialloccg: block not in map"); 84916784Smckusick /* NOTREACHED */ 8504359Smckusick gotit: 85134143Smckusick setbit(cg_inosused(cgp), ipref); 8524792Smckusic cgp->cg_cs.cs_nifree--; 8534792Smckusic fs->fs_cstotal.cs_nifree--; 8545322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 85550893Smckusick fs->fs_fmod = 1; 8564359Smckusick if ((mode & IFMT) == IFDIR) { 8574792Smckusic cgp->cg_cs.cs_ndir++; 8584792Smckusic fs->fs_cstotal.cs_ndir++; 8595322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 8604359Smckusick } 8614359Smckusick bdwrite(bp); 8624359Smckusick return (cg * fs->fs_ipg + ipref); 8634359Smckusick } 8644359Smckusick 8655375Smckusic /* 8665375Smckusic * Free a block or fragment. 8675375Smckusic * 8685375Smckusic * The specified block or fragment is placed back in the 8695375Smckusic * free map. If a fragment is deallocated, a possible 8705375Smckusic * block reassembly is checked. 8715375Smckusic */ 87251469Sbostic ffs_blkfree(ip, bno, size) 8735965Smckusic register struct inode *ip; 8744359Smckusick daddr_t bno; 87553059Smckusick long size; 8764359Smckusick { 8774359Smckusick register struct fs *fs; 8784359Smckusick register struct cg *cgp; 87937735Smckusick struct buf *bp; 88037735Smckusick int error, cg, blk, frags, bbase; 8814463Smckusic register int i; 8824359Smckusick 8835965Smckusic fs = ip->i_fs; 8846716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 8856716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 8866716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 88731402Smckusick panic("blkfree: bad size"); 8886716Smckusick } 8895377Smckusic cg = dtog(fs, bno); 89042318Smckusick if ((unsigned)bno >= fs->fs_size) { 8916567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 89253244Smckusick ffs_fserr(fs, ip->i_uid, "bad block"); 8934359Smckusick return; 8946567Smckusic } 89537735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 89638776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 89737735Smckusick if (error) { 89837735Smckusick brelse(bp); 89937735Smckusick return; 90037735Smckusick } 9016531Smckusick cgp = bp->b_un.b_cg; 90237735Smckusick if (!cg_chkmagic(cgp)) { 9035960Smckusic brelse(bp); 9044359Smckusick return; 9055960Smckusic } 9068105Sroot cgp->cg_time = time.tv_sec; 9075377Smckusic bno = dtogd(fs, bno); 9085322Smckusic if (size == fs->fs_bsize) { 90951469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) { 9106716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9116716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 91231402Smckusick panic("blkfree: freeing free block"); 9136567Smckusic } 91451469Sbostic ffs_setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno)); 9154792Smckusic cgp->cg_cs.cs_nbfree++; 9164792Smckusic fs->fs_cstotal.cs_nbfree++; 9175322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9185375Smckusic i = cbtocylno(fs, bno); 91934143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; 92034143Smckusick cg_blktot(cgp)[i]++; 9214426Smckusic } else { 92217224Smckusick bbase = bno - fragnum(fs, bno); 9234463Smckusic /* 9244463Smckusic * decrement the counts associated with the old frags 9254463Smckusic */ 92634143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 92751469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 9284463Smckusic /* 9294463Smckusic * deallocate the fragment 9304463Smckusic */ 9315960Smckusic frags = numfrags(fs, size); 9324463Smckusic for (i = 0; i < frags; i++) { 93334143Smckusick if (isset(cg_blksfree(cgp), bno + i)) { 9346716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9356716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 93631402Smckusick panic("blkfree: freeing free frag"); 9376716Smckusick } 93834143Smckusick setbit(cg_blksfree(cgp), bno + i); 9394426Smckusic } 9406294Smckusick cgp->cg_cs.cs_nffree += i; 9416294Smckusick fs->fs_cstotal.cs_nffree += i; 9426294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 9434463Smckusic /* 9444463Smckusic * add back in counts associated with the new frags 9454463Smckusic */ 94634143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 94751469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 9484463Smckusic /* 9494463Smckusic * if a complete block has been reassembled, account for it 9504463Smckusic */ 95151469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), 95234476Smckusick (daddr_t)fragstoblks(fs, bbase))) { 9535322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 9545322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 9555322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 9564792Smckusic cgp->cg_cs.cs_nbfree++; 9574792Smckusic fs->fs_cstotal.cs_nbfree++; 9585322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9595375Smckusic i = cbtocylno(fs, bbase); 96034143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 96134143Smckusick cg_blktot(cgp)[i]++; 9624426Smckusic } 9634426Smckusic } 96450893Smckusick fs->fs_fmod = 1; 9654359Smckusick bdwrite(bp); 9664359Smckusick } 9674359Smckusick 9685375Smckusic /* 9695375Smckusic * Free an inode. 9705375Smckusic * 9715375Smckusic * The specified inode is placed back in the free map. 9725375Smckusic */ 973*53577Sheideman int 97453519Sheideman ffs_vfree (ap) 97553519Sheideman struct vop_vfree_args *ap; 97653519Sheideman #define pvp (ap->a_pvp) 97753519Sheideman #define ino (ap->a_ino) 97853519Sheideman #define mode (ap->a_mode) 9794359Smckusick { 9804359Smckusick register struct fs *fs; 9814359Smckusick register struct cg *cgp; 98251541Smckusick register struct inode *pip; 98337735Smckusick struct buf *bp; 98437735Smckusick int error, cg; 9854359Smckusick 98651541Smckusick pip = VTOI(pvp); 98751469Sbostic fs = pip->i_fs; 98851469Sbostic if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg) 98951469Sbostic panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n", 99051469Sbostic pip->i_dev, ino, fs->fs_fsmnt); 9915377Smckusic cg = itog(fs, ino); 99251469Sbostic error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 99338776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 99437735Smckusick if (error) { 99537735Smckusick brelse(bp); 996*53577Sheideman return (0); 99737735Smckusick } 9986531Smckusick cgp = bp->b_un.b_cg; 99937735Smckusick if (!cg_chkmagic(cgp)) { 10005960Smckusic brelse(bp); 1001*53577Sheideman return (0); 10025960Smckusic } 10038105Sroot cgp->cg_time = time.tv_sec; 10044359Smckusick ino %= fs->fs_ipg; 100534143Smckusick if (isclr(cg_inosused(cgp), ino)) { 10066716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 100751469Sbostic pip->i_dev, ino, fs->fs_fsmnt); 100848057Smckusick if (fs->fs_ronly == 0) 100948057Smckusick panic("ifree: freeing free inode"); 10106716Smckusick } 101134143Smckusick clrbit(cg_inosused(cgp), ino); 101216784Smckusick if (ino < cgp->cg_irotor) 101316784Smckusick cgp->cg_irotor = ino; 10144792Smckusic cgp->cg_cs.cs_nifree++; 10154792Smckusic fs->fs_cstotal.cs_nifree++; 10165322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 10174359Smckusick if ((mode & IFMT) == IFDIR) { 10184792Smckusic cgp->cg_cs.cs_ndir--; 10194792Smckusic fs->fs_cstotal.cs_ndir--; 10205322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 10214359Smckusick } 102250893Smckusick fs->fs_fmod = 1; 10234359Smckusick bdwrite(bp); 1024*53577Sheideman return (0); 10254359Smckusick } 102653519Sheideman #undef pvp 102753519Sheideman #undef ino 102853519Sheideman #undef mode 10294359Smckusick 10304463Smckusic /* 10315375Smckusic * Find a block of the specified size in the specified cylinder group. 10325375Smckusic * 10334651Smckusic * It is a panic if a request is made to find a block if none are 10344651Smckusic * available. 10354651Smckusic */ 103651469Sbostic static daddr_t 103751469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz) 10384651Smckusic register struct fs *fs; 10394651Smckusic register struct cg *cgp; 10404651Smckusic daddr_t bpref; 10414651Smckusic int allocsiz; 10424651Smckusic { 10434651Smckusic daddr_t bno; 10444651Smckusic int start, len, loc, i; 10454651Smckusic int blk, field, subfield, pos; 10464651Smckusic 10474651Smckusic /* 10484651Smckusic * find the fragment by searching through the free block 10494651Smckusic * map for an appropriate bit pattern 10504651Smckusic */ 10514651Smckusic if (bpref) 10525377Smckusic start = dtogd(fs, bpref) / NBBY; 10534651Smckusic else 10544651Smckusic start = cgp->cg_frotor / NBBY; 10555398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 105634476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start], 105734476Smckusick (u_char *)fragtbl[fs->fs_frag], 105834476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 10594651Smckusic if (loc == 0) { 10606531Smckusick len = start + 1; 10616531Smckusick start = 0; 106234476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0], 106334476Smckusick (u_char *)fragtbl[fs->fs_frag], 106434476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 106516784Smckusick if (loc == 0) { 106616784Smckusick printf("start = %d, len = %d, fs = %s\n", 106716784Smckusick start, len, fs->fs_fsmnt); 106851469Sbostic panic("ffs_alloccg: map corrupted"); 106917697Smckusick /* NOTREACHED */ 107016784Smckusick } 10714651Smckusic } 10724651Smckusic bno = (start + len - loc) * NBBY; 10734651Smckusic cgp->cg_frotor = bno; 10744651Smckusic /* 10754651Smckusic * found the byte in the map 10764651Smckusic * sift through the bits to find the selected frag 10774651Smckusic */ 10786294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 107934143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bno); 10804651Smckusic blk <<= 1; 10814651Smckusic field = around[allocsiz]; 10824651Smckusic subfield = inside[allocsiz]; 10835322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 10846294Smckusick if ((blk & field) == subfield) 10856294Smckusick return (bno + pos); 10864651Smckusic field <<= 1; 10874651Smckusic subfield <<= 1; 10884651Smckusic } 10894651Smckusic } 10906716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 109151469Sbostic panic("ffs_alloccg: block not in map"); 10926531Smckusick return (-1); 10934651Smckusic } 10944651Smckusic 10954651Smckusic /* 10965375Smckusic * Fserr prints the name of a file system with an error diagnostic. 10975375Smckusic * 10985375Smckusic * The form of the error message is: 10994359Smckusick * fs: error message 11004359Smckusick */ 110151469Sbostic static void 110251469Sbostic ffs_fserr(fs, uid, cp) 11034359Smckusick struct fs *fs; 110451469Sbostic u_int uid; 11054359Smckusick char *cp; 11064359Smckusick { 11074359Smckusick 110842318Smckusick log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp); 11094359Smckusick } 1110