123394Smckusick /* 237735Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 334432Sbostic * All rights reserved. 423394Smckusick * 534432Sbostic * Redistribution and use in source and binary forms are permitted 634857Sbostic * provided that the above copyright notice and this paragraph are 734857Sbostic * duplicated in all such forms and that any documentation, 834857Sbostic * advertising materials, and other materials related to such 934857Sbostic * distribution and use acknowledge that the software was developed 1034857Sbostic * by the University of California, Berkeley. The name of the 1134857Sbostic * University may not be used to endorse or promote products derived 1234857Sbostic * from this software without specific prior written permission. 1334857Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434857Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534857Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1634432Sbostic * 17*38255Smckusick * @(#)lfs_alloc.c 7.10 (Berkeley) 06/07/89 1823394Smckusick */ 194359Smckusick 2017097Sbloom #include "param.h" 2117097Sbloom #include "systm.h" 2217097Sbloom #include "mount.h" 2317097Sbloom #include "buf.h" 2417097Sbloom #include "user.h" 2537735Smckusick #include "vnode.h" 2617097Sbloom #include "kernel.h" 2718307Sralph #include "syslog.h" 2826253Skarels #include "cmap.h" 2937735Smckusick #include "../ufs/quota.h" 3037735Smckusick #include "../ufs/inode.h" 3137735Smckusick #include "../ufs/fs.h" 324359Smckusick 335212Smckusic extern u_long hashalloc(); 349163Ssam extern ino_t ialloccg(); 359163Ssam extern daddr_t alloccg(); 364651Smckusic extern daddr_t alloccgblk(); 374651Smckusic extern daddr_t fragextend(); 384651Smckusic extern daddr_t blkpref(); 394651Smckusic extern daddr_t mapsearch(); 404607Smckusic extern int inside[], around[]; 415322Smckusic extern unsigned char *fragtbl[]; 424359Smckusick 435375Smckusic /* 445375Smckusic * Allocate a block in the file system. 455375Smckusic * 465375Smckusic * The size of the requested block is given, which must be some 475375Smckusic * multiple of fs_fsize and <= fs_bsize. 485375Smckusic * A preference may be optionally specified. If a preference is given 495375Smckusic * the following hierarchy is used to allocate a block: 505375Smckusic * 1) allocate the requested block. 515375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 525375Smckusic * 3) allocate a block in the same cylinder group. 535375Smckusic * 4) quadradically rehash into other cylinder groups, until an 545375Smckusic * available block is located. 555375Smckusic * If no block preference is given the following heirarchy is used 565375Smckusic * to allocate a block: 575375Smckusic * 1) allocate a block in the cylinder group that contains the 585375Smckusic * inode for the file. 595375Smckusic * 2) quadradically rehash into other cylinder groups, until an 605375Smckusic * available block is located. 615375Smckusic */ 6237735Smckusick alloc(ip, bpref, size, bpp, flags) 634463Smckusic register struct inode *ip; 644359Smckusick daddr_t bpref; 654359Smckusick int size; 6637735Smckusick struct buf **bpp; 6737735Smckusick int flags; 684359Smckusick { 694359Smckusick daddr_t bno; 704359Smckusick register struct fs *fs; 714463Smckusic register struct buf *bp; 7237735Smckusick int cg, error; 734359Smckusick 7437735Smckusick *bpp = 0; 755965Smckusic fs = ip->i_fs; 766716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 776716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 786716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 794463Smckusic panic("alloc: bad size"); 806716Smckusick } 815322Smckusic if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 824359Smckusick goto nospace; 8311638Ssam if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 844792Smckusic goto nospace; 857650Ssam #ifdef QUOTA 8637735Smckusick if (error = chkdq(ip, (long)btodb(size), 0)) 8737735Smckusick return (error); 887483Skre #endif 894948Smckusic if (bpref >= fs->fs_size) 904948Smckusic bpref = 0; 914359Smckusick if (bpref == 0) 925377Smckusic cg = itog(fs, ip->i_number); 934359Smckusick else 945377Smckusic cg = dtog(fs, bpref); 959163Ssam bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, 969163Ssam (u_long (*)())alloccg); 976567Smckusic if (bno <= 0) 984359Smckusick goto nospace; 9912643Ssam ip->i_blocks += btodb(size); 10012643Ssam ip->i_flag |= IUPD|ICHG; 10137735Smckusick bp = getblk(ip->i_devvp, fsbtodb(fs, bno), size); 10237735Smckusick if (flags & B_CLRBUF) 10337735Smckusick clrbuf(bp); 10437735Smckusick *bpp = bp; 10537735Smckusick return (0); 1064359Smckusick nospace: 1074359Smckusick fserr(fs, "file system full"); 1084359Smckusick uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 10937735Smckusick return (ENOSPC); 1104359Smckusick } 1114359Smckusick 1125375Smckusic /* 1135375Smckusic * Reallocate a fragment to a bigger size 1145375Smckusic * 1155375Smckusic * The number and size of the old block is given, and a preference 1165375Smckusic * and new size is also specified. The allocator attempts to extend 1175375Smckusic * the original block. Failing that, the regular block allocator is 1185375Smckusic * invoked to get an appropriate block. 1195375Smckusic */ 12037735Smckusick realloccg(ip, bprev, bpref, osize, nsize, bpp) 1215965Smckusic register struct inode *ip; 1224651Smckusic daddr_t bprev, bpref; 1234426Smckusic int osize, nsize; 12437735Smckusick struct buf **bpp; 1254426Smckusic { 1264426Smckusic register struct fs *fs; 12737735Smckusick struct buf *bp, *obp; 12824698Smckusick int cg, request; 12925471Smckusick daddr_t bno, bn; 13037735Smckusick int i, error, count; 1314426Smckusic 13237735Smckusick *bpp = 0; 1335965Smckusic fs = ip->i_fs; 1345960Smckusic if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 1356716Smckusick (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 1366716Smckusick printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 1376716Smckusick ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 1384463Smckusic panic("realloccg: bad size"); 1396716Smckusick } 14011638Ssam if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 1414792Smckusic goto nospace; 1426716Smckusick if (bprev == 0) { 1436716Smckusick printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 1446716Smckusick ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 1454463Smckusic panic("realloccg: bad bprev"); 1466716Smckusick } 1477650Ssam #ifdef QUOTA 14837735Smckusick if (error = chkdq(ip, (long)btodb(nsize - osize), 0)) 14937735Smckusick return (error); 1507483Skre #endif 1516294Smckusick cg = dtog(fs, bprev); 1525965Smckusic bno = fragextend(ip, cg, (long)bprev, osize, nsize); 1534463Smckusic if (bno != 0) { 1547187Sroot do { 15537735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, bno), 15637735Smckusick osize, &bp); 15737735Smckusick if (error) { 1587187Sroot brelse(bp); 15937735Smckusick return (error); 1607187Sroot } 1617187Sroot } while (brealloc(bp, nsize) == 0); 1627187Sroot bp->b_flags |= B_DONE; 1639163Ssam bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 16412643Ssam ip->i_blocks += btodb(nsize - osize); 16512643Ssam ip->i_flag |= IUPD|ICHG; 16637735Smckusick *bpp = bp; 16737735Smckusick return (0); 1684463Smckusic } 1694948Smckusic if (bpref >= fs->fs_size) 1704948Smckusic bpref = 0; 17126253Skarels switch ((int)fs->fs_optim) { 17225256Smckusick case FS_OPTSPACE: 17325256Smckusick /* 17425256Smckusick * Allocate an exact sized fragment. Although this makes 17525256Smckusick * best use of space, we will waste time relocating it if 17625256Smckusick * the file continues to grow. If the fragmentation is 17725256Smckusick * less than half of the minimum free reserve, we choose 17825256Smckusick * to begin optimizing for time. 17925256Smckusick */ 18024698Smckusick request = nsize; 18125256Smckusick if (fs->fs_minfree < 5 || 18225256Smckusick fs->fs_cstotal.cs_nffree > 18325256Smckusick fs->fs_dsize * fs->fs_minfree / (2 * 100)) 18425256Smckusick break; 18525256Smckusick log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 18625256Smckusick fs->fs_fsmnt); 18725256Smckusick fs->fs_optim = FS_OPTTIME; 18825256Smckusick break; 18925256Smckusick case FS_OPTTIME: 19025256Smckusick /* 19125256Smckusick * At this point we have discovered a file that is trying 19225256Smckusick * to grow a small fragment to a larger fragment. To save 19325256Smckusick * time, we allocate a full sized block, then free the 19425256Smckusick * unused portion. If the file continues to grow, the 19525256Smckusick * `fragextend' call above will be able to grow it in place 19625256Smckusick * without further copying. If aberrant programs cause 19725256Smckusick * disk fragmentation to grow within 2% of the free reserve, 19825256Smckusick * we choose to begin optimizing for space. 19925256Smckusick */ 20024698Smckusick request = fs->fs_bsize; 20125256Smckusick if (fs->fs_cstotal.cs_nffree < 20225256Smckusick fs->fs_dsize * (fs->fs_minfree - 2) / 100) 20325256Smckusick break; 20425256Smckusick log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 20525256Smckusick fs->fs_fsmnt); 20625256Smckusick fs->fs_optim = FS_OPTSPACE; 20725256Smckusick break; 20825256Smckusick default: 20925256Smckusick printf("dev = 0x%x, optim = %d, fs = %s\n", 21025256Smckusick ip->i_dev, fs->fs_optim, fs->fs_fsmnt); 21125256Smckusick panic("realloccg: bad optim"); 21225256Smckusick /* NOTREACHED */ 21325256Smckusick } 21424698Smckusick bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request, 2159163Ssam (u_long (*)())alloccg); 2166567Smckusic if (bno > 0) { 21737735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, bprev), osize, &obp); 21837735Smckusick if (error) { 2195960Smckusic brelse(obp); 22037735Smckusick return (error); 2215960Smckusic } 22225471Smckusick bn = fsbtodb(fs, bno); 22337735Smckusick bp = getblk(ip->i_devvp, bn, nsize); 22417658Smckusick bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize); 22530749Skarels count = howmany(osize, CLBYTES); 22630749Skarels for (i = 0; i < count; i++) 22737735Smckusick munhash(ip->i_devvp, bn + i * CLBYTES / DEV_BSIZE); 22817658Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 22917658Smckusick if (obp->b_flags & B_DELWRI) { 23017658Smckusick obp->b_flags &= ~B_DELWRI; 23117658Smckusick u.u_ru.ru_oublock--; /* delete charge */ 23217658Smckusick } 2334463Smckusic brelse(obp); 23431402Smckusick blkfree(ip, bprev, (off_t)osize); 23524698Smckusick if (nsize < request) 23631402Smckusick blkfree(ip, bno + numfrags(fs, nsize), 23724698Smckusick (off_t)(request - nsize)); 23812643Ssam ip->i_blocks += btodb(nsize - osize); 23912643Ssam ip->i_flag |= IUPD|ICHG; 24037735Smckusick *bpp = bp; 24137735Smckusick return (0); 2424463Smckusic } 2434792Smckusic nospace: 2444463Smckusic /* 2454463Smckusic * no space available 2464463Smckusic */ 2474426Smckusic fserr(fs, "file system full"); 2484426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 24937735Smckusick return (ENOSPC); 2504426Smckusic } 2514426Smckusic 2525375Smckusic /* 2535375Smckusic * Allocate an inode in the file system. 2545375Smckusic * 2555375Smckusic * A preference may be optionally specified. If a preference is given 2565375Smckusic * the following hierarchy is used to allocate an inode: 2575375Smckusic * 1) allocate the requested inode. 2585375Smckusic * 2) allocate an inode in the same cylinder group. 2595375Smckusic * 3) quadradically rehash into other cylinder groups, until an 2605375Smckusic * available inode is located. 2615375Smckusic * If no inode preference is given the following heirarchy is used 2625375Smckusic * to allocate an inode: 2635375Smckusic * 1) allocate an inode in cylinder group 0. 2645375Smckusic * 2) quadradically rehash into other cylinder groups, until an 2655375Smckusic * available inode is located. 2665375Smckusic */ 26737735Smckusick ialloc(pip, ipref, mode, ipp) 2685965Smckusic register struct inode *pip; 2694359Smckusick ino_t ipref; 2704359Smckusick int mode; 27137735Smckusick struct inode **ipp; 2724359Smckusick { 2735212Smckusic ino_t ino; 2744359Smckusick register struct fs *fs; 2754359Smckusick register struct inode *ip; 27637735Smckusick int cg, error; 2774359Smckusick 27837735Smckusick *ipp = 0; 2795965Smckusic fs = pip->i_fs; 2804792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 2814359Smckusick goto noinodes; 2827650Ssam #ifdef QUOTA 28337735Smckusick if (error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0)) 28437735Smckusick return (error); 2857483Skre #endif 2864948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 2874948Smckusic ipref = 0; 2885377Smckusic cg = itog(fs, ipref); 2895965Smckusic ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); 2904359Smckusick if (ino == 0) 2914359Smckusick goto noinodes; 29237735Smckusick error = iget(pip, ino, ipp); 29337735Smckusick if (error) { 29415120Skarels ifree(pip, ino, 0); 29537735Smckusick return (error); 2964359Smckusick } 297*38255Smckusick ip = *ipp; 2986716Smckusick if (ip->i_mode) { 2996716Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 3006716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 3014359Smckusick panic("ialloc: dup alloc"); 3026716Smckusick } 30312643Ssam if (ip->i_blocks) { /* XXX */ 30412643Ssam printf("free inode %s/%d had %d blocks\n", 30512643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 30612643Ssam ip->i_blocks = 0; 30712643Ssam } 308*38255Smckusick /* 309*38255Smckusick * Set up a new generation number for this inode. 310*38255Smckusick */ 311*38255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 312*38255Smckusick nextgennumber = time.tv_sec; 313*38255Smckusick ip->i_gen = nextgennumber; 31437735Smckusick return (0); 3154359Smckusick noinodes: 3164359Smckusick fserr(fs, "out of inodes"); 3176294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 31837735Smckusick return (ENOSPC); 3194359Smckusick } 3204359Smckusick 3214651Smckusic /* 3225375Smckusic * Find a cylinder to place a directory. 3235375Smckusic * 3245375Smckusic * The policy implemented by this algorithm is to select from 3255375Smckusic * among those cylinder groups with above the average number of 3265375Smckusic * free inodes, the one with the smallest number of directories. 3274651Smckusic */ 3289163Ssam ino_t 3295965Smckusic dirpref(fs) 3305965Smckusic register struct fs *fs; 3314359Smckusick { 3324651Smckusic int cg, minndir, mincg, avgifree; 3334359Smckusick 3344792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 3354651Smckusic minndir = fs->fs_ipg; 3364359Smckusick mincg = 0; 3374651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 3385322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 3395322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 3404359Smckusick mincg = cg; 3415322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 3424359Smckusick } 3439163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 3444359Smckusick } 3454359Smckusick 3464651Smckusic /* 3479163Ssam * Select the desired position for the next block in a file. The file is 3489163Ssam * logically divided into sections. The first section is composed of the 3499163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 3509163Ssam * 3519163Ssam * If no blocks have been allocated in the first section, the policy is to 3529163Ssam * request a block in the same cylinder group as the inode that describes 3539163Ssam * the file. If no blocks have been allocated in any other section, the 3549163Ssam * policy is to place the section in a cylinder group with a greater than 3559163Ssam * average number of free blocks. An appropriate cylinder group is found 35617696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of 35717696Smckusick * blocks is needed, the sweep begins in the cylinder group following the 35817696Smckusick * cylinder group from which the previous allocation was made. The sweep 35917696Smckusick * continues until a cylinder group with greater than the average number 36017696Smckusick * of free blocks is found. If the allocation is for the first block in an 36117696Smckusick * indirect block, the information on the previous allocation is unavailable; 36217696Smckusick * here a best guess is made based upon the logical block number being 36317696Smckusick * allocated. 3649163Ssam * 3659163Ssam * If a section is already partially allocated, the policy is to 3669163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 3679163Ssam * contiguous blocks and the beginning of the next is physically separated 3689163Ssam * so that the disk head will be in transit between them for at least 3699163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 3709163Ssam * schedule another I/O transfer. 3714651Smckusic */ 3725212Smckusic daddr_t 3739163Ssam blkpref(ip, lbn, indx, bap) 3749163Ssam struct inode *ip; 3759163Ssam daddr_t lbn; 3769163Ssam int indx; 3779163Ssam daddr_t *bap; 3789163Ssam { 3795965Smckusic register struct fs *fs; 38017696Smckusick register int cg; 38117696Smckusick int avgbfree, startcg; 3829163Ssam daddr_t nextblk; 3834651Smckusic 3849163Ssam fs = ip->i_fs; 3859163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 3869163Ssam if (lbn < NDADDR) { 3879163Ssam cg = itog(fs, ip->i_number); 3885322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 3894651Smckusic } 3909163Ssam /* 3919163Ssam * Find a cylinder with greater than average number of 3929163Ssam * unused data blocks. 3939163Ssam */ 39417696Smckusick if (indx == 0 || bap[indx - 1] == 0) 39517696Smckusick startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; 39617696Smckusick else 39717696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1; 39817696Smckusick startcg %= fs->fs_ncg; 3999163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 40017696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++) 4019163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4029163Ssam fs->fs_cgrotor = cg; 4039163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4049163Ssam } 40517696Smckusick for (cg = 0; cg <= startcg; cg++) 4069163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4079163Ssam fs->fs_cgrotor = cg; 4089163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4099163Ssam } 4109163Ssam return (NULL); 4119163Ssam } 4129163Ssam /* 4139163Ssam * One or more previous blocks have been laid out. If less 4149163Ssam * than fs_maxcontig previous blocks are contiguous, the 4159163Ssam * next block is requested contiguously, otherwise it is 4169163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 4179163Ssam */ 4189163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 4199163Ssam if (indx > fs->fs_maxcontig && 42011638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 4219163Ssam != nextblk) 4229163Ssam return (nextblk); 4239163Ssam if (fs->fs_rotdelay != 0) 4249163Ssam /* 4259163Ssam * Here we convert ms of delay to frags as: 4269163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 4279163Ssam * ((sect/frag) * (ms/sec)) 4289163Ssam * then round up to the next block. 4299163Ssam */ 4309163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 4319163Ssam (NSPF(fs) * 1000), fs->fs_frag); 4329163Ssam return (nextblk); 4334651Smckusic } 4344651Smckusic 4355375Smckusic /* 4365375Smckusic * Implement the cylinder overflow algorithm. 4375375Smckusic * 4385375Smckusic * The policy implemented by this algorithm is: 4395375Smckusic * 1) allocate the block in its requested cylinder group. 4405375Smckusic * 2) quadradically rehash on the cylinder group number. 4415375Smckusic * 3) brute force search for a free block. 4425375Smckusic */ 4435212Smckusic /*VARARGS5*/ 4445212Smckusic u_long 4455965Smckusic hashalloc(ip, cg, pref, size, allocator) 4465965Smckusic struct inode *ip; 4474359Smckusick int cg; 4484359Smckusick long pref; 4494359Smckusick int size; /* size for data blocks, mode for inodes */ 4505212Smckusic u_long (*allocator)(); 4514359Smckusick { 4525965Smckusic register struct fs *fs; 4534359Smckusick long result; 4544359Smckusick int i, icg = cg; 4554359Smckusick 4565965Smckusic fs = ip->i_fs; 4574359Smckusick /* 4584359Smckusick * 1: preferred cylinder group 4594359Smckusick */ 4605965Smckusic result = (*allocator)(ip, cg, pref, size); 4614359Smckusick if (result) 4624359Smckusick return (result); 4634359Smckusick /* 4644359Smckusick * 2: quadratic rehash 4654359Smckusick */ 4664359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 4674359Smckusick cg += i; 4684359Smckusick if (cg >= fs->fs_ncg) 4694359Smckusick cg -= fs->fs_ncg; 4705965Smckusic result = (*allocator)(ip, cg, 0, size); 4714359Smckusick if (result) 4724359Smckusick return (result); 4734359Smckusick } 4744359Smckusick /* 4754359Smckusick * 3: brute force search 47610847Ssam * Note that we start at i == 2, since 0 was checked initially, 47710847Ssam * and 1 is always checked in the quadratic rehash. 4784359Smckusick */ 47910848Smckusick cg = (icg + 2) % fs->fs_ncg; 48010847Ssam for (i = 2; i < fs->fs_ncg; i++) { 4815965Smckusic result = (*allocator)(ip, cg, 0, size); 4824359Smckusick if (result) 4834359Smckusick return (result); 4844359Smckusick cg++; 4854359Smckusick if (cg == fs->fs_ncg) 4864359Smckusick cg = 0; 4874359Smckusick } 4886294Smckusick return (NULL); 4894359Smckusick } 4904359Smckusick 4915375Smckusic /* 4925375Smckusic * Determine whether a fragment can be extended. 4935375Smckusic * 4945375Smckusic * Check to see if the necessary fragments are available, and 4955375Smckusic * if they are, allocate them. 4965375Smckusic */ 4974359Smckusick daddr_t 4985965Smckusic fragextend(ip, cg, bprev, osize, nsize) 4995965Smckusic struct inode *ip; 5004426Smckusic int cg; 5014463Smckusic long bprev; 5024426Smckusic int osize, nsize; 5034426Smckusic { 5045965Smckusic register struct fs *fs; 5054463Smckusic register struct cg *cgp; 50637735Smckusick struct buf *bp; 5074463Smckusic long bno; 5084463Smckusic int frags, bbase; 50937735Smckusick int i, error; 5104426Smckusic 5115965Smckusic fs = ip->i_fs; 51217224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 5136531Smckusick return (NULL); 5145960Smckusic frags = numfrags(fs, nsize); 51517224Smckusick bbase = fragnum(fs, bprev); 51617224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 51730749Skarels /* cannot extend across a block boundary */ 5186294Smckusick return (NULL); 5194463Smckusic } 52037735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 52137735Smckusick (int)fs->fs_cgsize, &bp); 52237735Smckusick if (error) { 52337735Smckusick brelse(bp); 52437735Smckusick return (NULL); 52537735Smckusick } 5266531Smckusick cgp = bp->b_un.b_cg; 52737735Smckusick if (!cg_chkmagic(cgp)) { 5285960Smckusic brelse(bp); 5296294Smckusick return (NULL); 5305960Smckusic } 5318105Sroot cgp->cg_time = time.tv_sec; 5325377Smckusic bno = dtogd(fs, bprev); 5335960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 53434143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) { 5355361Smckusic brelse(bp); 5366294Smckusick return (NULL); 5375361Smckusic } 5385361Smckusic /* 5395361Smckusic * the current fragment can be extended 5405361Smckusic * deduct the count on fragment being extended into 5415361Smckusic * increase the count on the remaining fragment (if any) 5425361Smckusic * allocate the extended piece 5435361Smckusic */ 5445361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 54534143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) 5464463Smckusic break; 5475960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 5485361Smckusic if (i != frags) 5495361Smckusic cgp->cg_frsum[i - frags]++; 5505960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 55134143Smckusick clrbit(cg_blksfree(cgp), bno + i); 5525361Smckusic cgp->cg_cs.cs_nffree--; 5535361Smckusic fs->fs_cstotal.cs_nffree--; 5545361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 5554463Smckusic } 5565361Smckusic fs->fs_fmod++; 5575361Smckusic bdwrite(bp); 5585361Smckusic return (bprev); 5594426Smckusic } 5604426Smckusic 5615375Smckusic /* 5625375Smckusic * Determine whether a block can be allocated. 5635375Smckusic * 5645375Smckusic * Check to see if a block of the apprpriate size is available, 5655375Smckusic * and if it is, allocate it. 5665375Smckusic */ 5679163Ssam daddr_t 5685965Smckusic alloccg(ip, cg, bpref, size) 5695965Smckusic struct inode *ip; 5704359Smckusick int cg; 5714359Smckusick daddr_t bpref; 5724359Smckusick int size; 5734359Smckusick { 5745965Smckusic register struct fs *fs; 5754463Smckusic register struct cg *cgp; 57637735Smckusick struct buf *bp; 5774463Smckusic register int i; 57837735Smckusick int error, bno, frags, allocsiz; 5794359Smckusick 5805965Smckusic fs = ip->i_fs; 5815322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 5826294Smckusick return (NULL); 58337735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 58437735Smckusick (int)fs->fs_cgsize, &bp); 58537735Smckusick if (error) { 58637735Smckusick brelse(bp); 58737735Smckusick return (NULL); 58837735Smckusick } 5896531Smckusick cgp = bp->b_un.b_cg; 59037735Smckusick if (!cg_chkmagic(cgp) || 59115950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 5925960Smckusic brelse(bp); 5936294Smckusick return (NULL); 5945960Smckusic } 5958105Sroot cgp->cg_time = time.tv_sec; 5965322Smckusic if (size == fs->fs_bsize) { 5975212Smckusic bno = alloccgblk(fs, cgp, bpref); 5984463Smckusic bdwrite(bp); 5994463Smckusic return (bno); 6004463Smckusic } 6014463Smckusic /* 6024463Smckusic * check to see if any fragments are already available 6034463Smckusic * allocsiz is the size which will be allocated, hacking 6044463Smckusic * it down to a smaller size if necessary 6054463Smckusic */ 6065960Smckusic frags = numfrags(fs, size); 6075322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 6084463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 6094463Smckusic break; 6105322Smckusic if (allocsiz == fs->fs_frag) { 6114463Smckusic /* 6124463Smckusic * no fragments were available, so a block will be 6134463Smckusic * allocated, and hacked up 6144463Smckusic */ 6154792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 6164463Smckusic brelse(bp); 6176294Smckusick return (NULL); 6184463Smckusic } 6195212Smckusic bno = alloccgblk(fs, cgp, bpref); 6205377Smckusic bpref = dtogd(fs, bno); 6215322Smckusic for (i = frags; i < fs->fs_frag; i++) 62234143Smckusick setbit(cg_blksfree(cgp), bpref + i); 6235322Smckusic i = fs->fs_frag - frags; 6244792Smckusic cgp->cg_cs.cs_nffree += i; 6254792Smckusic fs->fs_cstotal.cs_nffree += i; 6265322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 6279762Ssam fs->fs_fmod++; 6284463Smckusic cgp->cg_frsum[i]++; 6294463Smckusic bdwrite(bp); 6304463Smckusic return (bno); 6314463Smckusic } 6324651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 63315950Smckusick if (bno < 0) { 63415950Smckusick brelse(bp); 6356294Smckusick return (NULL); 63615950Smckusick } 6374463Smckusic for (i = 0; i < frags; i++) 63834143Smckusick clrbit(cg_blksfree(cgp), bno + i); 6394792Smckusic cgp->cg_cs.cs_nffree -= frags; 6404792Smckusic fs->fs_cstotal.cs_nffree -= frags; 6415322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 6429762Ssam fs->fs_fmod++; 6434463Smckusic cgp->cg_frsum[allocsiz]--; 6444463Smckusic if (frags != allocsiz) 6454463Smckusic cgp->cg_frsum[allocsiz - frags]++; 6464463Smckusic bdwrite(bp); 6474463Smckusic return (cg * fs->fs_fpg + bno); 6484463Smckusic } 6494463Smckusic 6505375Smckusic /* 6515375Smckusic * Allocate a block in a cylinder group. 6525375Smckusic * 6535375Smckusic * This algorithm implements the following policy: 6545375Smckusic * 1) allocate the requested block. 6555375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 6565375Smckusic * 3) allocate the next available block on the block rotor for the 6575375Smckusic * specified cylinder group. 6585375Smckusic * Note that this routine only allocates fs_bsize blocks; these 6595375Smckusic * blocks may be fragmented by the routine that allocates them. 6605375Smckusic */ 6614463Smckusic daddr_t 6625212Smckusic alloccgblk(fs, cgp, bpref) 6635965Smckusic register struct fs *fs; 6644463Smckusic register struct cg *cgp; 6654463Smckusic daddr_t bpref; 6664463Smckusic { 6674651Smckusic daddr_t bno; 6686294Smckusick int cylno, pos, delta; 6694651Smckusic short *cylbp; 6705361Smckusic register int i; 6714463Smckusic 6724651Smckusic if (bpref == 0) { 6734651Smckusic bpref = cgp->cg_rotor; 6745361Smckusic goto norot; 6755361Smckusic } 67617224Smckusick bpref = blknum(fs, bpref); 6775377Smckusic bpref = dtogd(fs, bpref); 6785361Smckusic /* 6795361Smckusic * if the requested block is available, use it 6805361Smckusic */ 68134143Smckusick if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 6825361Smckusic bno = bpref; 6835361Smckusic goto gotit; 6845361Smckusic } 6855361Smckusic /* 6865361Smckusic * check for a block available on the same cylinder 6875361Smckusic */ 6885361Smckusic cylno = cbtocylno(fs, bpref); 68934143Smckusick if (cg_blktot(cgp)[cylno] == 0) 6905375Smckusic goto norot; 6915375Smckusic if (fs->fs_cpc == 0) { 6925375Smckusic /* 6935375Smckusic * block layout info is not available, so just have 6945375Smckusic * to take any block in this cylinder. 6955375Smckusic */ 6965375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 6975375Smckusic goto norot; 6985375Smckusic } 6995375Smckusic /* 7005361Smckusic * check the summary information to see if a block is 7015361Smckusic * available in the requested cylinder starting at the 7029163Ssam * requested rotational position and proceeding around. 7035361Smckusic */ 70434143Smckusick cylbp = cg_blks(fs, cgp, cylno); 7059163Ssam pos = cbtorpos(fs, bpref); 70634143Smckusick for (i = pos; i < fs->fs_nrpos; i++) 7075361Smckusic if (cylbp[i] > 0) 7085361Smckusic break; 70934143Smckusick if (i == fs->fs_nrpos) 7105361Smckusic for (i = 0; i < pos; i++) 7115361Smckusic if (cylbp[i] > 0) 7125361Smckusic break; 7135361Smckusic if (cylbp[i] > 0) { 7144651Smckusic /* 7155361Smckusic * found a rotational position, now find the actual 7165361Smckusic * block. A panic if none is actually there. 7174651Smckusic */ 7185361Smckusic pos = cylno % fs->fs_cpc; 7195361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 72034143Smckusick if (fs_postbl(fs, pos)[i] == -1) { 7216716Smckusick printf("pos = %d, i = %d, fs = %s\n", 7226716Smckusick pos, i, fs->fs_fsmnt); 7235361Smckusic panic("alloccgblk: cyl groups corrupted"); 7246716Smckusick } 72534143Smckusick for (i = fs_postbl(fs, pos)[i];; ) { 72634143Smckusick if (isblock(fs, cg_blksfree(cgp), bno + i)) { 72711638Ssam bno = blkstofrags(fs, (bno + i)); 7285361Smckusic goto gotit; 7295361Smckusic } 73034143Smckusick delta = fs_rotbl(fs)[i]; 73134143Smckusick if (delta <= 0 || 73234143Smckusick delta + i > fragstoblks(fs, fs->fs_fpg)) 7334651Smckusic break; 7346294Smckusick i += delta; 7354651Smckusic } 7366716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 7375361Smckusic panic("alloccgblk: can't find blk in cyl"); 7384359Smckusick } 7395361Smckusic norot: 7405361Smckusic /* 7415361Smckusic * no blocks in the requested cylinder, so take next 7425361Smckusic * available one in this cylinder group. 7435361Smckusic */ 7448628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 7456567Smckusic if (bno < 0) 7466294Smckusick return (NULL); 7474651Smckusic cgp->cg_rotor = bno; 7484359Smckusick gotit: 74934143Smckusick clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno)); 7504792Smckusic cgp->cg_cs.cs_nbfree--; 7514792Smckusic fs->fs_cstotal.cs_nbfree--; 7525322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 7535375Smckusic cylno = cbtocylno(fs, bno); 75434143Smckusick cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 75534143Smckusick cg_blktot(cgp)[cylno]--; 7564359Smckusick fs->fs_fmod++; 7574651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 7584359Smckusick } 75934143Smckusick 7605375Smckusic /* 7615375Smckusic * Determine whether an inode can be allocated. 7625375Smckusic * 7635375Smckusic * Check to see if an inode is available, and if it is, 7645375Smckusic * allocate it using the following policy: 7655375Smckusic * 1) allocate the requested inode. 7665375Smckusic * 2) allocate the next available inode after the requested 7675375Smckusic * inode in the specified cylinder group. 7685375Smckusic */ 7699163Ssam ino_t 7705965Smckusic ialloccg(ip, cg, ipref, mode) 7715965Smckusic struct inode *ip; 7724359Smckusick int cg; 7734359Smckusick daddr_t ipref; 7744359Smckusick int mode; 7754359Smckusick { 7765965Smckusic register struct fs *fs; 7774463Smckusic register struct cg *cgp; 77816784Smckusick struct buf *bp; 77937735Smckusick int error, start, len, loc, map, i; 7804359Smckusick 7815965Smckusic fs = ip->i_fs; 7825322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 7836294Smckusick return (NULL); 78437735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 78537735Smckusick (int)fs->fs_cgsize, &bp); 78637735Smckusick if (error) { 78737735Smckusick brelse(bp); 78837735Smckusick return (NULL); 78937735Smckusick } 7906531Smckusick cgp = bp->b_un.b_cg; 79137735Smckusick if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 7925960Smckusic brelse(bp); 7936294Smckusick return (NULL); 7945960Smckusic } 7958105Sroot cgp->cg_time = time.tv_sec; 7964359Smckusick if (ipref) { 7974359Smckusick ipref %= fs->fs_ipg; 79834143Smckusick if (isclr(cg_inosused(cgp), ipref)) 7994359Smckusick goto gotit; 80016784Smckusick } 80116784Smckusick start = cgp->cg_irotor / NBBY; 80216784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 80334143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 80416784Smckusick if (loc == 0) { 80517697Smckusick len = start + 1; 80617697Smckusick start = 0; 80734143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 80817697Smckusick if (loc == 0) { 80917697Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 81017697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 81117697Smckusick panic("ialloccg: map corrupted"); 81217697Smckusick /* NOTREACHED */ 81317697Smckusick } 81416784Smckusick } 81516784Smckusick i = start + len - loc; 81634143Smckusick map = cg_inosused(cgp)[i]; 81716784Smckusick ipref = i * NBBY; 81816784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 81916784Smckusick if ((map & i) == 0) { 8204359Smckusick cgp->cg_irotor = ipref; 8214359Smckusick goto gotit; 8224359Smckusick } 8234359Smckusick } 82416784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 82516784Smckusick panic("ialloccg: block not in map"); 82616784Smckusick /* NOTREACHED */ 8274359Smckusick gotit: 82834143Smckusick setbit(cg_inosused(cgp), ipref); 8294792Smckusic cgp->cg_cs.cs_nifree--; 8304792Smckusic fs->fs_cstotal.cs_nifree--; 8315322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 8324359Smckusick fs->fs_fmod++; 8334359Smckusick if ((mode & IFMT) == IFDIR) { 8344792Smckusic cgp->cg_cs.cs_ndir++; 8354792Smckusic fs->fs_cstotal.cs_ndir++; 8365322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 8374359Smckusick } 8384359Smckusick bdwrite(bp); 8394359Smckusick return (cg * fs->fs_ipg + ipref); 8404359Smckusick } 8414359Smckusick 8425375Smckusic /* 8435375Smckusic * Free a block or fragment. 8445375Smckusic * 8455375Smckusic * The specified block or fragment is placed back in the 8465375Smckusic * free map. If a fragment is deallocated, a possible 8475375Smckusic * block reassembly is checked. 8485375Smckusic */ 84931402Smckusick blkfree(ip, bno, size) 8505965Smckusic register struct inode *ip; 8514359Smckusick daddr_t bno; 8525212Smckusic off_t size; 8534359Smckusick { 8544359Smckusick register struct fs *fs; 8554359Smckusick register struct cg *cgp; 85637735Smckusick struct buf *bp; 85737735Smckusick int error, cg, blk, frags, bbase; 8584463Smckusic register int i; 8594359Smckusick 8605965Smckusic fs = ip->i_fs; 8616716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 8626716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 8636716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 86431402Smckusick panic("blkfree: bad size"); 8656716Smckusick } 8665377Smckusic cg = dtog(fs, bno); 8676567Smckusic if (badblock(fs, bno)) { 8686567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 8694359Smckusick return; 8706567Smckusic } 87137735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 87237735Smckusick (int)fs->fs_cgsize, &bp); 87337735Smckusick if (error) { 87437735Smckusick brelse(bp); 87537735Smckusick return; 87637735Smckusick } 8776531Smckusick cgp = bp->b_un.b_cg; 87837735Smckusick if (!cg_chkmagic(cgp)) { 8795960Smckusic brelse(bp); 8804359Smckusick return; 8815960Smckusic } 8828105Sroot cgp->cg_time = time.tv_sec; 8835377Smckusic bno = dtogd(fs, bno); 8845322Smckusic if (size == fs->fs_bsize) { 88534143Smckusick if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) { 8866716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 8876716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 88831402Smckusick panic("blkfree: freeing free block"); 8896567Smckusic } 89034143Smckusick setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno)); 8914792Smckusic cgp->cg_cs.cs_nbfree++; 8924792Smckusic fs->fs_cstotal.cs_nbfree++; 8935322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 8945375Smckusic i = cbtocylno(fs, bno); 89534143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; 89634143Smckusick cg_blktot(cgp)[i]++; 8974426Smckusic } else { 89817224Smckusick bbase = bno - fragnum(fs, bno); 8994463Smckusic /* 9004463Smckusic * decrement the counts associated with the old frags 9014463Smckusic */ 90234143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 9035322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 9044463Smckusic /* 9054463Smckusic * deallocate the fragment 9064463Smckusic */ 9075960Smckusic frags = numfrags(fs, size); 9084463Smckusic for (i = 0; i < frags; i++) { 90934143Smckusick if (isset(cg_blksfree(cgp), bno + i)) { 9106716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9116716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 91231402Smckusick panic("blkfree: freeing free frag"); 9136716Smckusick } 91434143Smckusick setbit(cg_blksfree(cgp), bno + i); 9154426Smckusic } 9166294Smckusick cgp->cg_cs.cs_nffree += i; 9176294Smckusick fs->fs_cstotal.cs_nffree += i; 9186294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 9194463Smckusic /* 9204463Smckusic * add back in counts associated with the new frags 9214463Smckusic */ 92234143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 9235322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 9244463Smckusic /* 9254463Smckusic * if a complete block has been reassembled, account for it 9264463Smckusic */ 92734476Smckusick if (isblock(fs, cg_blksfree(cgp), 92834476Smckusick (daddr_t)fragstoblks(fs, bbase))) { 9295322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 9305322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 9315322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 9324792Smckusic cgp->cg_cs.cs_nbfree++; 9334792Smckusic fs->fs_cstotal.cs_nbfree++; 9345322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9355375Smckusic i = cbtocylno(fs, bbase); 93634143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 93734143Smckusick cg_blktot(cgp)[i]++; 9384426Smckusic } 9394426Smckusic } 9404359Smckusick fs->fs_fmod++; 9414359Smckusick bdwrite(bp); 9424359Smckusick } 9434359Smckusick 9445375Smckusic /* 9455375Smckusic * Free an inode. 9465375Smckusic * 9475375Smckusic * The specified inode is placed back in the free map. 9485375Smckusic */ 9495965Smckusic ifree(ip, ino, mode) 9505965Smckusic struct inode *ip; 9514359Smckusick ino_t ino; 9524359Smckusick int mode; 9534359Smckusick { 9544359Smckusick register struct fs *fs; 9554359Smckusick register struct cg *cgp; 95637735Smckusick struct buf *bp; 95737735Smckusick int error, cg; 9584359Smckusick 9595965Smckusic fs = ip->i_fs; 9606716Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 9616716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9626716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9634359Smckusick panic("ifree: range"); 9646716Smckusick } 9655377Smckusic cg = itog(fs, ino); 96637735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 96737735Smckusick (int)fs->fs_cgsize, &bp); 96837735Smckusick if (error) { 96937735Smckusick brelse(bp); 97037735Smckusick return; 97137735Smckusick } 9726531Smckusick cgp = bp->b_un.b_cg; 97337735Smckusick if (!cg_chkmagic(cgp)) { 9745960Smckusic brelse(bp); 9754359Smckusick return; 9765960Smckusic } 9778105Sroot cgp->cg_time = time.tv_sec; 9784359Smckusick ino %= fs->fs_ipg; 97934143Smckusick if (isclr(cg_inosused(cgp), ino)) { 9806716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9816716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9824359Smckusick panic("ifree: freeing free inode"); 9836716Smckusick } 98434143Smckusick clrbit(cg_inosused(cgp), ino); 98516784Smckusick if (ino < cgp->cg_irotor) 98616784Smckusick cgp->cg_irotor = ino; 9874792Smckusic cgp->cg_cs.cs_nifree++; 9884792Smckusic fs->fs_cstotal.cs_nifree++; 9895322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 9904359Smckusick if ((mode & IFMT) == IFDIR) { 9914792Smckusic cgp->cg_cs.cs_ndir--; 9924792Smckusic fs->fs_cstotal.cs_ndir--; 9935322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 9944359Smckusick } 9954359Smckusick fs->fs_fmod++; 9964359Smckusick bdwrite(bp); 9974359Smckusick } 9984359Smckusick 9994463Smckusic /* 10005375Smckusic * Find a block of the specified size in the specified cylinder group. 10015375Smckusic * 10024651Smckusic * It is a panic if a request is made to find a block if none are 10034651Smckusic * available. 10044651Smckusic */ 10054651Smckusic daddr_t 10064651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 10074651Smckusic register struct fs *fs; 10084651Smckusic register struct cg *cgp; 10094651Smckusic daddr_t bpref; 10104651Smckusic int allocsiz; 10114651Smckusic { 10124651Smckusic daddr_t bno; 10134651Smckusic int start, len, loc, i; 10144651Smckusic int blk, field, subfield, pos; 10154651Smckusic 10164651Smckusic /* 10174651Smckusic * find the fragment by searching through the free block 10184651Smckusic * map for an appropriate bit pattern 10194651Smckusic */ 10204651Smckusic if (bpref) 10215377Smckusic start = dtogd(fs, bpref) / NBBY; 10224651Smckusic else 10234651Smckusic start = cgp->cg_frotor / NBBY; 10245398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 102534476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start], 102634476Smckusick (u_char *)fragtbl[fs->fs_frag], 102734476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 10284651Smckusic if (loc == 0) { 10296531Smckusick len = start + 1; 10306531Smckusick start = 0; 103134476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0], 103234476Smckusick (u_char *)fragtbl[fs->fs_frag], 103334476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 103416784Smckusick if (loc == 0) { 103516784Smckusick printf("start = %d, len = %d, fs = %s\n", 103616784Smckusick start, len, fs->fs_fsmnt); 103716784Smckusick panic("alloccg: map corrupted"); 103817697Smckusick /* NOTREACHED */ 103916784Smckusick } 10404651Smckusic } 10414651Smckusic bno = (start + len - loc) * NBBY; 10424651Smckusic cgp->cg_frotor = bno; 10434651Smckusic /* 10444651Smckusic * found the byte in the map 10454651Smckusic * sift through the bits to find the selected frag 10464651Smckusic */ 10476294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 104834143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bno); 10494651Smckusic blk <<= 1; 10504651Smckusic field = around[allocsiz]; 10514651Smckusic subfield = inside[allocsiz]; 10525322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 10536294Smckusick if ((blk & field) == subfield) 10546294Smckusick return (bno + pos); 10554651Smckusic field <<= 1; 10564651Smckusic subfield <<= 1; 10574651Smckusic } 10584651Smckusic } 10596716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 10604651Smckusic panic("alloccg: block not in map"); 10616531Smckusick return (-1); 10624651Smckusic } 10634651Smckusic 10644651Smckusic /* 10655375Smckusic * Fserr prints the name of a file system with an error diagnostic. 10665375Smckusic * 10675375Smckusic * The form of the error message is: 10684359Smckusick * fs: error message 10694359Smckusick */ 10704359Smckusick fserr(fs, cp) 10714359Smckusick struct fs *fs; 10724359Smckusick char *cp; 10734359Smckusick { 10744359Smckusick 107524839Seric log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp); 10764359Smckusick } 1077