123394Smckusick /* 2*37735Smckusick * 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*37735Smckusick * @(#)lfs_alloc.c 7.9 (Berkeley) 05/09/89 1823394Smckusick */ 194359Smckusick 2017097Sbloom #include "param.h" 2117097Sbloom #include "systm.h" 2217097Sbloom #include "mount.h" 2317097Sbloom #include "buf.h" 2417097Sbloom #include "user.h" 25*37735Smckusick #include "vnode.h" 2617097Sbloom #include "kernel.h" 2718307Sralph #include "syslog.h" 2826253Skarels #include "cmap.h" 29*37735Smckusick #include "../ufs/quota.h" 30*37735Smckusick #include "../ufs/inode.h" 31*37735Smckusick #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 */ 62*37735Smckusick alloc(ip, bpref, size, bpp, flags) 634463Smckusic register struct inode *ip; 644359Smckusick daddr_t bpref; 654359Smckusick int size; 66*37735Smckusick struct buf **bpp; 67*37735Smckusick int flags; 684359Smckusick { 694359Smckusick daddr_t bno; 704359Smckusick register struct fs *fs; 714463Smckusic register struct buf *bp; 72*37735Smckusick int cg, error; 734359Smckusick 74*37735Smckusick *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 86*37735Smckusick if (error = chkdq(ip, (long)btodb(size), 0)) 87*37735Smckusick 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; 101*37735Smckusick bp = getblk(ip->i_devvp, fsbtodb(fs, bno), size); 102*37735Smckusick if (flags & B_CLRBUF) 103*37735Smckusick clrbuf(bp); 104*37735Smckusick *bpp = bp; 105*37735Smckusick return (0); 1064359Smckusick nospace: 1074359Smckusick fserr(fs, "file system full"); 1084359Smckusick uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 109*37735Smckusick 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 */ 120*37735Smckusick realloccg(ip, bprev, bpref, osize, nsize, bpp) 1215965Smckusic register struct inode *ip; 1224651Smckusic daddr_t bprev, bpref; 1234426Smckusic int osize, nsize; 124*37735Smckusick struct buf **bpp; 1254426Smckusic { 1264426Smckusic register struct fs *fs; 127*37735Smckusick struct buf *bp, *obp; 12824698Smckusick int cg, request; 12925471Smckusick daddr_t bno, bn; 130*37735Smckusick int i, error, count; 1314426Smckusic 132*37735Smckusick *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 148*37735Smckusick if (error = chkdq(ip, (long)btodb(nsize - osize), 0)) 149*37735Smckusick return (error); 1507483Skre #endif 1516294Smckusick cg = dtog(fs, bprev); 1525965Smckusic bno = fragextend(ip, cg, (long)bprev, osize, nsize); 1534463Smckusic if (bno != 0) { 1547187Sroot do { 155*37735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, bno), 156*37735Smckusick osize, &bp); 157*37735Smckusick if (error) { 1587187Sroot brelse(bp); 159*37735Smckusick 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; 166*37735Smckusick *bpp = bp; 167*37735Smckusick 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) { 217*37735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, bprev), osize, &obp); 218*37735Smckusick if (error) { 2195960Smckusic brelse(obp); 220*37735Smckusick return (error); 2215960Smckusic } 22225471Smckusick bn = fsbtodb(fs, bno); 223*37735Smckusick 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++) 227*37735Smckusick 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; 240*37735Smckusick *bpp = bp; 241*37735Smckusick 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); 249*37735Smckusick 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 */ 267*37735Smckusick ialloc(pip, ipref, mode, ipp) 2685965Smckusic register struct inode *pip; 2694359Smckusick ino_t ipref; 2704359Smckusick int mode; 271*37735Smckusick struct inode **ipp; 2724359Smckusick { 2735212Smckusic ino_t ino; 2744359Smckusick register struct fs *fs; 2754359Smckusick register struct inode *ip; 276*37735Smckusick int cg, error; 2774359Smckusick 278*37735Smckusick *ipp = 0; 2795965Smckusic fs = pip->i_fs; 2804792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 2814359Smckusick goto noinodes; 2827650Ssam #ifdef QUOTA 283*37735Smckusick if (error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0)) 284*37735Smckusick 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; 292*37735Smckusick error = iget(pip, ino, ipp); 293*37735Smckusick ip = *ipp; 294*37735Smckusick if (error) { 29515120Skarels ifree(pip, ino, 0); 296*37735Smckusick return (error); 2974359Smckusick } 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*37735Smckusick return (0); 3094359Smckusick noinodes: 3104359Smckusick fserr(fs, "out of inodes"); 3116294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 312*37735Smckusick return (ENOSPC); 3134359Smckusick } 3144359Smckusick 3154651Smckusic /* 3165375Smckusic * Find a cylinder to place a directory. 3175375Smckusic * 3185375Smckusic * The policy implemented by this algorithm is to select from 3195375Smckusic * among those cylinder groups with above the average number of 3205375Smckusic * free inodes, the one with the smallest number of directories. 3214651Smckusic */ 3229163Ssam ino_t 3235965Smckusic dirpref(fs) 3245965Smckusic register struct fs *fs; 3254359Smckusick { 3264651Smckusic int cg, minndir, mincg, avgifree; 3274359Smckusick 3284792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 3294651Smckusic minndir = fs->fs_ipg; 3304359Smckusick mincg = 0; 3314651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 3325322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 3335322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 3344359Smckusick mincg = cg; 3355322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 3364359Smckusick } 3379163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 3384359Smckusick } 3394359Smckusick 3404651Smckusic /* 3419163Ssam * Select the desired position for the next block in a file. The file is 3429163Ssam * logically divided into sections. The first section is composed of the 3439163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 3449163Ssam * 3459163Ssam * If no blocks have been allocated in the first section, the policy is to 3469163Ssam * request a block in the same cylinder group as the inode that describes 3479163Ssam * the file. If no blocks have been allocated in any other section, the 3489163Ssam * policy is to place the section in a cylinder group with a greater than 3499163Ssam * average number of free blocks. An appropriate cylinder group is found 35017696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of 35117696Smckusick * blocks is needed, the sweep begins in the cylinder group following the 35217696Smckusick * cylinder group from which the previous allocation was made. The sweep 35317696Smckusick * continues until a cylinder group with greater than the average number 35417696Smckusick * of free blocks is found. If the allocation is for the first block in an 35517696Smckusick * indirect block, the information on the previous allocation is unavailable; 35617696Smckusick * here a best guess is made based upon the logical block number being 35717696Smckusick * allocated. 3589163Ssam * 3599163Ssam * If a section is already partially allocated, the policy is to 3609163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 3619163Ssam * contiguous blocks and the beginning of the next is physically separated 3629163Ssam * so that the disk head will be in transit between them for at least 3639163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 3649163Ssam * schedule another I/O transfer. 3654651Smckusic */ 3665212Smckusic daddr_t 3679163Ssam blkpref(ip, lbn, indx, bap) 3689163Ssam struct inode *ip; 3699163Ssam daddr_t lbn; 3709163Ssam int indx; 3719163Ssam daddr_t *bap; 3729163Ssam { 3735965Smckusic register struct fs *fs; 37417696Smckusick register int cg; 37517696Smckusick int avgbfree, startcg; 3769163Ssam daddr_t nextblk; 3774651Smckusic 3789163Ssam fs = ip->i_fs; 3799163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 3809163Ssam if (lbn < NDADDR) { 3819163Ssam cg = itog(fs, ip->i_number); 3825322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 3834651Smckusic } 3849163Ssam /* 3859163Ssam * Find a cylinder with greater than average number of 3869163Ssam * unused data blocks. 3879163Ssam */ 38817696Smckusick if (indx == 0 || bap[indx - 1] == 0) 38917696Smckusick startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; 39017696Smckusick else 39117696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1; 39217696Smckusick startcg %= fs->fs_ncg; 3939163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 39417696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++) 3959163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3969163Ssam fs->fs_cgrotor = cg; 3979163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3989163Ssam } 39917696Smckusick for (cg = 0; cg <= startcg; cg++) 4009163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4019163Ssam fs->fs_cgrotor = cg; 4029163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4039163Ssam } 4049163Ssam return (NULL); 4059163Ssam } 4069163Ssam /* 4079163Ssam * One or more previous blocks have been laid out. If less 4089163Ssam * than fs_maxcontig previous blocks are contiguous, the 4099163Ssam * next block is requested contiguously, otherwise it is 4109163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 4119163Ssam */ 4129163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 4139163Ssam if (indx > fs->fs_maxcontig && 41411638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 4159163Ssam != nextblk) 4169163Ssam return (nextblk); 4179163Ssam if (fs->fs_rotdelay != 0) 4189163Ssam /* 4199163Ssam * Here we convert ms of delay to frags as: 4209163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 4219163Ssam * ((sect/frag) * (ms/sec)) 4229163Ssam * then round up to the next block. 4239163Ssam */ 4249163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 4259163Ssam (NSPF(fs) * 1000), fs->fs_frag); 4269163Ssam return (nextblk); 4274651Smckusic } 4284651Smckusic 4295375Smckusic /* 4305375Smckusic * Implement the cylinder overflow algorithm. 4315375Smckusic * 4325375Smckusic * The policy implemented by this algorithm is: 4335375Smckusic * 1) allocate the block in its requested cylinder group. 4345375Smckusic * 2) quadradically rehash on the cylinder group number. 4355375Smckusic * 3) brute force search for a free block. 4365375Smckusic */ 4375212Smckusic /*VARARGS5*/ 4385212Smckusic u_long 4395965Smckusic hashalloc(ip, cg, pref, size, allocator) 4405965Smckusic struct inode *ip; 4414359Smckusick int cg; 4424359Smckusick long pref; 4434359Smckusick int size; /* size for data blocks, mode for inodes */ 4445212Smckusic u_long (*allocator)(); 4454359Smckusick { 4465965Smckusic register struct fs *fs; 4474359Smckusick long result; 4484359Smckusick int i, icg = cg; 4494359Smckusick 4505965Smckusic fs = ip->i_fs; 4514359Smckusick /* 4524359Smckusick * 1: preferred cylinder group 4534359Smckusick */ 4545965Smckusic result = (*allocator)(ip, cg, pref, size); 4554359Smckusick if (result) 4564359Smckusick return (result); 4574359Smckusick /* 4584359Smckusick * 2: quadratic rehash 4594359Smckusick */ 4604359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 4614359Smckusick cg += i; 4624359Smckusick if (cg >= fs->fs_ncg) 4634359Smckusick cg -= fs->fs_ncg; 4645965Smckusic result = (*allocator)(ip, cg, 0, size); 4654359Smckusick if (result) 4664359Smckusick return (result); 4674359Smckusick } 4684359Smckusick /* 4694359Smckusick * 3: brute force search 47010847Ssam * Note that we start at i == 2, since 0 was checked initially, 47110847Ssam * and 1 is always checked in the quadratic rehash. 4724359Smckusick */ 47310848Smckusick cg = (icg + 2) % fs->fs_ncg; 47410847Ssam for (i = 2; i < fs->fs_ncg; i++) { 4755965Smckusic result = (*allocator)(ip, cg, 0, size); 4764359Smckusick if (result) 4774359Smckusick return (result); 4784359Smckusick cg++; 4794359Smckusick if (cg == fs->fs_ncg) 4804359Smckusick cg = 0; 4814359Smckusick } 4826294Smckusick return (NULL); 4834359Smckusick } 4844359Smckusick 4855375Smckusic /* 4865375Smckusic * Determine whether a fragment can be extended. 4875375Smckusic * 4885375Smckusic * Check to see if the necessary fragments are available, and 4895375Smckusic * if they are, allocate them. 4905375Smckusic */ 4914359Smckusick daddr_t 4925965Smckusic fragextend(ip, cg, bprev, osize, nsize) 4935965Smckusic struct inode *ip; 4944426Smckusic int cg; 4954463Smckusic long bprev; 4964426Smckusic int osize, nsize; 4974426Smckusic { 4985965Smckusic register struct fs *fs; 4994463Smckusic register struct cg *cgp; 500*37735Smckusick struct buf *bp; 5014463Smckusic long bno; 5024463Smckusic int frags, bbase; 503*37735Smckusick int i, error; 5044426Smckusic 5055965Smckusic fs = ip->i_fs; 50617224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 5076531Smckusick return (NULL); 5085960Smckusic frags = numfrags(fs, nsize); 50917224Smckusick bbase = fragnum(fs, bprev); 51017224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 51130749Skarels /* cannot extend across a block boundary */ 5126294Smckusick return (NULL); 5134463Smckusic } 514*37735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 515*37735Smckusick (int)fs->fs_cgsize, &bp); 516*37735Smckusick if (error) { 517*37735Smckusick brelse(bp); 518*37735Smckusick return (NULL); 519*37735Smckusick } 5206531Smckusick cgp = bp->b_un.b_cg; 521*37735Smckusick if (!cg_chkmagic(cgp)) { 5225960Smckusic brelse(bp); 5236294Smckusick return (NULL); 5245960Smckusic } 5258105Sroot cgp->cg_time = time.tv_sec; 5265377Smckusic bno = dtogd(fs, bprev); 5275960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 52834143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) { 5295361Smckusic brelse(bp); 5306294Smckusick return (NULL); 5315361Smckusic } 5325361Smckusic /* 5335361Smckusic * the current fragment can be extended 5345361Smckusic * deduct the count on fragment being extended into 5355361Smckusic * increase the count on the remaining fragment (if any) 5365361Smckusic * allocate the extended piece 5375361Smckusic */ 5385361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 53934143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) 5404463Smckusic break; 5415960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 5425361Smckusic if (i != frags) 5435361Smckusic cgp->cg_frsum[i - frags]++; 5445960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 54534143Smckusick clrbit(cg_blksfree(cgp), bno + i); 5465361Smckusic cgp->cg_cs.cs_nffree--; 5475361Smckusic fs->fs_cstotal.cs_nffree--; 5485361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 5494463Smckusic } 5505361Smckusic fs->fs_fmod++; 5515361Smckusic bdwrite(bp); 5525361Smckusic return (bprev); 5534426Smckusic } 5544426Smckusic 5555375Smckusic /* 5565375Smckusic * Determine whether a block can be allocated. 5575375Smckusic * 5585375Smckusic * Check to see if a block of the apprpriate size is available, 5595375Smckusic * and if it is, allocate it. 5605375Smckusic */ 5619163Ssam daddr_t 5625965Smckusic alloccg(ip, cg, bpref, size) 5635965Smckusic struct inode *ip; 5644359Smckusick int cg; 5654359Smckusick daddr_t bpref; 5664359Smckusick int size; 5674359Smckusick { 5685965Smckusic register struct fs *fs; 5694463Smckusic register struct cg *cgp; 570*37735Smckusick struct buf *bp; 5714463Smckusic register int i; 572*37735Smckusick int error, bno, frags, allocsiz; 5734359Smckusick 5745965Smckusic fs = ip->i_fs; 5755322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 5766294Smckusick return (NULL); 577*37735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 578*37735Smckusick (int)fs->fs_cgsize, &bp); 579*37735Smckusick if (error) { 580*37735Smckusick brelse(bp); 581*37735Smckusick return (NULL); 582*37735Smckusick } 5836531Smckusick cgp = bp->b_un.b_cg; 584*37735Smckusick if (!cg_chkmagic(cgp) || 58515950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 5865960Smckusic brelse(bp); 5876294Smckusick return (NULL); 5885960Smckusic } 5898105Sroot cgp->cg_time = time.tv_sec; 5905322Smckusic if (size == fs->fs_bsize) { 5915212Smckusic bno = alloccgblk(fs, cgp, bpref); 5924463Smckusic bdwrite(bp); 5934463Smckusic return (bno); 5944463Smckusic } 5954463Smckusic /* 5964463Smckusic * check to see if any fragments are already available 5974463Smckusic * allocsiz is the size which will be allocated, hacking 5984463Smckusic * it down to a smaller size if necessary 5994463Smckusic */ 6005960Smckusic frags = numfrags(fs, size); 6015322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 6024463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 6034463Smckusic break; 6045322Smckusic if (allocsiz == fs->fs_frag) { 6054463Smckusic /* 6064463Smckusic * no fragments were available, so a block will be 6074463Smckusic * allocated, and hacked up 6084463Smckusic */ 6094792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 6104463Smckusic brelse(bp); 6116294Smckusick return (NULL); 6124463Smckusic } 6135212Smckusic bno = alloccgblk(fs, cgp, bpref); 6145377Smckusic bpref = dtogd(fs, bno); 6155322Smckusic for (i = frags; i < fs->fs_frag; i++) 61634143Smckusick setbit(cg_blksfree(cgp), bpref + i); 6175322Smckusic i = fs->fs_frag - frags; 6184792Smckusic cgp->cg_cs.cs_nffree += i; 6194792Smckusic fs->fs_cstotal.cs_nffree += i; 6205322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 6219762Ssam fs->fs_fmod++; 6224463Smckusic cgp->cg_frsum[i]++; 6234463Smckusic bdwrite(bp); 6244463Smckusic return (bno); 6254463Smckusic } 6264651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 62715950Smckusick if (bno < 0) { 62815950Smckusick brelse(bp); 6296294Smckusick return (NULL); 63015950Smckusick } 6314463Smckusic for (i = 0; i < frags; i++) 63234143Smckusick clrbit(cg_blksfree(cgp), bno + i); 6334792Smckusic cgp->cg_cs.cs_nffree -= frags; 6344792Smckusic fs->fs_cstotal.cs_nffree -= frags; 6355322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 6369762Ssam fs->fs_fmod++; 6374463Smckusic cgp->cg_frsum[allocsiz]--; 6384463Smckusic if (frags != allocsiz) 6394463Smckusic cgp->cg_frsum[allocsiz - frags]++; 6404463Smckusic bdwrite(bp); 6414463Smckusic return (cg * fs->fs_fpg + bno); 6424463Smckusic } 6434463Smckusic 6445375Smckusic /* 6455375Smckusic * Allocate a block in a cylinder group. 6465375Smckusic * 6475375Smckusic * This algorithm implements the following policy: 6485375Smckusic * 1) allocate the requested block. 6495375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 6505375Smckusic * 3) allocate the next available block on the block rotor for the 6515375Smckusic * specified cylinder group. 6525375Smckusic * Note that this routine only allocates fs_bsize blocks; these 6535375Smckusic * blocks may be fragmented by the routine that allocates them. 6545375Smckusic */ 6554463Smckusic daddr_t 6565212Smckusic alloccgblk(fs, cgp, bpref) 6575965Smckusic register struct fs *fs; 6584463Smckusic register struct cg *cgp; 6594463Smckusic daddr_t bpref; 6604463Smckusic { 6614651Smckusic daddr_t bno; 6626294Smckusick int cylno, pos, delta; 6634651Smckusic short *cylbp; 6645361Smckusic register int i; 6654463Smckusic 6664651Smckusic if (bpref == 0) { 6674651Smckusic bpref = cgp->cg_rotor; 6685361Smckusic goto norot; 6695361Smckusic } 67017224Smckusick bpref = blknum(fs, bpref); 6715377Smckusic bpref = dtogd(fs, bpref); 6725361Smckusic /* 6735361Smckusic * if the requested block is available, use it 6745361Smckusic */ 67534143Smckusick if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 6765361Smckusic bno = bpref; 6775361Smckusic goto gotit; 6785361Smckusic } 6795361Smckusic /* 6805361Smckusic * check for a block available on the same cylinder 6815361Smckusic */ 6825361Smckusic cylno = cbtocylno(fs, bpref); 68334143Smckusick if (cg_blktot(cgp)[cylno] == 0) 6845375Smckusic goto norot; 6855375Smckusic if (fs->fs_cpc == 0) { 6865375Smckusic /* 6875375Smckusic * block layout info is not available, so just have 6885375Smckusic * to take any block in this cylinder. 6895375Smckusic */ 6905375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 6915375Smckusic goto norot; 6925375Smckusic } 6935375Smckusic /* 6945361Smckusic * check the summary information to see if a block is 6955361Smckusic * available in the requested cylinder starting at the 6969163Ssam * requested rotational position and proceeding around. 6975361Smckusic */ 69834143Smckusick cylbp = cg_blks(fs, cgp, cylno); 6999163Ssam pos = cbtorpos(fs, bpref); 70034143Smckusick for (i = pos; i < fs->fs_nrpos; i++) 7015361Smckusic if (cylbp[i] > 0) 7025361Smckusic break; 70334143Smckusick if (i == fs->fs_nrpos) 7045361Smckusic for (i = 0; i < pos; i++) 7055361Smckusic if (cylbp[i] > 0) 7065361Smckusic break; 7075361Smckusic if (cylbp[i] > 0) { 7084651Smckusic /* 7095361Smckusic * found a rotational position, now find the actual 7105361Smckusic * block. A panic if none is actually there. 7114651Smckusic */ 7125361Smckusic pos = cylno % fs->fs_cpc; 7135361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 71434143Smckusick if (fs_postbl(fs, pos)[i] == -1) { 7156716Smckusick printf("pos = %d, i = %d, fs = %s\n", 7166716Smckusick pos, i, fs->fs_fsmnt); 7175361Smckusic panic("alloccgblk: cyl groups corrupted"); 7186716Smckusick } 71934143Smckusick for (i = fs_postbl(fs, pos)[i];; ) { 72034143Smckusick if (isblock(fs, cg_blksfree(cgp), bno + i)) { 72111638Ssam bno = blkstofrags(fs, (bno + i)); 7225361Smckusic goto gotit; 7235361Smckusic } 72434143Smckusick delta = fs_rotbl(fs)[i]; 72534143Smckusick if (delta <= 0 || 72634143Smckusick delta + i > fragstoblks(fs, fs->fs_fpg)) 7274651Smckusic break; 7286294Smckusick i += delta; 7294651Smckusic } 7306716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 7315361Smckusic panic("alloccgblk: can't find blk in cyl"); 7324359Smckusick } 7335361Smckusic norot: 7345361Smckusic /* 7355361Smckusic * no blocks in the requested cylinder, so take next 7365361Smckusic * available one in this cylinder group. 7375361Smckusic */ 7388628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 7396567Smckusic if (bno < 0) 7406294Smckusick return (NULL); 7414651Smckusic cgp->cg_rotor = bno; 7424359Smckusick gotit: 74334143Smckusick clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno)); 7444792Smckusic cgp->cg_cs.cs_nbfree--; 7454792Smckusic fs->fs_cstotal.cs_nbfree--; 7465322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 7475375Smckusic cylno = cbtocylno(fs, bno); 74834143Smckusick cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 74934143Smckusick cg_blktot(cgp)[cylno]--; 7504359Smckusick fs->fs_fmod++; 7514651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 7524359Smckusick } 75334143Smckusick 7545375Smckusic /* 7555375Smckusic * Determine whether an inode can be allocated. 7565375Smckusic * 7575375Smckusic * Check to see if an inode is available, and if it is, 7585375Smckusic * allocate it using the following policy: 7595375Smckusic * 1) allocate the requested inode. 7605375Smckusic * 2) allocate the next available inode after the requested 7615375Smckusic * inode in the specified cylinder group. 7625375Smckusic */ 7639163Ssam ino_t 7645965Smckusic ialloccg(ip, cg, ipref, mode) 7655965Smckusic struct inode *ip; 7664359Smckusick int cg; 7674359Smckusick daddr_t ipref; 7684359Smckusick int mode; 7694359Smckusick { 7705965Smckusic register struct fs *fs; 7714463Smckusic register struct cg *cgp; 77216784Smckusick struct buf *bp; 773*37735Smckusick int error, start, len, loc, map, i; 7744359Smckusick 7755965Smckusic fs = ip->i_fs; 7765322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 7776294Smckusick return (NULL); 778*37735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 779*37735Smckusick (int)fs->fs_cgsize, &bp); 780*37735Smckusick if (error) { 781*37735Smckusick brelse(bp); 782*37735Smckusick return (NULL); 783*37735Smckusick } 7846531Smckusick cgp = bp->b_un.b_cg; 785*37735Smckusick if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 7865960Smckusic brelse(bp); 7876294Smckusick return (NULL); 7885960Smckusic } 7898105Sroot cgp->cg_time = time.tv_sec; 7904359Smckusick if (ipref) { 7914359Smckusick ipref %= fs->fs_ipg; 79234143Smckusick if (isclr(cg_inosused(cgp), ipref)) 7934359Smckusick goto gotit; 79416784Smckusick } 79516784Smckusick start = cgp->cg_irotor / NBBY; 79616784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 79734143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 79816784Smckusick if (loc == 0) { 79917697Smckusick len = start + 1; 80017697Smckusick start = 0; 80134143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 80217697Smckusick if (loc == 0) { 80317697Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 80417697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 80517697Smckusick panic("ialloccg: map corrupted"); 80617697Smckusick /* NOTREACHED */ 80717697Smckusick } 80816784Smckusick } 80916784Smckusick i = start + len - loc; 81034143Smckusick map = cg_inosused(cgp)[i]; 81116784Smckusick ipref = i * NBBY; 81216784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 81316784Smckusick if ((map & i) == 0) { 8144359Smckusick cgp->cg_irotor = ipref; 8154359Smckusick goto gotit; 8164359Smckusick } 8174359Smckusick } 81816784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 81916784Smckusick panic("ialloccg: block not in map"); 82016784Smckusick /* NOTREACHED */ 8214359Smckusick gotit: 82234143Smckusick setbit(cg_inosused(cgp), ipref); 8234792Smckusic cgp->cg_cs.cs_nifree--; 8244792Smckusic fs->fs_cstotal.cs_nifree--; 8255322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 8264359Smckusick fs->fs_fmod++; 8274359Smckusick if ((mode & IFMT) == IFDIR) { 8284792Smckusic cgp->cg_cs.cs_ndir++; 8294792Smckusic fs->fs_cstotal.cs_ndir++; 8305322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 8314359Smckusick } 8324359Smckusick bdwrite(bp); 8334359Smckusick return (cg * fs->fs_ipg + ipref); 8344359Smckusick } 8354359Smckusick 8365375Smckusic /* 8375375Smckusic * Free a block or fragment. 8385375Smckusic * 8395375Smckusic * The specified block or fragment is placed back in the 8405375Smckusic * free map. If a fragment is deallocated, a possible 8415375Smckusic * block reassembly is checked. 8425375Smckusic */ 84331402Smckusick blkfree(ip, bno, size) 8445965Smckusic register struct inode *ip; 8454359Smckusick daddr_t bno; 8465212Smckusic off_t size; 8474359Smckusick { 8484359Smckusick register struct fs *fs; 8494359Smckusick register struct cg *cgp; 850*37735Smckusick struct buf *bp; 851*37735Smckusick int error, cg, blk, frags, bbase; 8524463Smckusic register int i; 8534359Smckusick 8545965Smckusic fs = ip->i_fs; 8556716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 8566716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 8576716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 85831402Smckusick panic("blkfree: bad size"); 8596716Smckusick } 8605377Smckusic cg = dtog(fs, bno); 8616567Smckusic if (badblock(fs, bno)) { 8626567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 8634359Smckusick return; 8646567Smckusic } 865*37735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 866*37735Smckusick (int)fs->fs_cgsize, &bp); 867*37735Smckusick if (error) { 868*37735Smckusick brelse(bp); 869*37735Smckusick return; 870*37735Smckusick } 8716531Smckusick cgp = bp->b_un.b_cg; 872*37735Smckusick if (!cg_chkmagic(cgp)) { 8735960Smckusic brelse(bp); 8744359Smckusick return; 8755960Smckusic } 8768105Sroot cgp->cg_time = time.tv_sec; 8775377Smckusic bno = dtogd(fs, bno); 8785322Smckusic if (size == fs->fs_bsize) { 87934143Smckusick if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) { 8806716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 8816716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 88231402Smckusick panic("blkfree: freeing free block"); 8836567Smckusic } 88434143Smckusick setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno)); 8854792Smckusic cgp->cg_cs.cs_nbfree++; 8864792Smckusic fs->fs_cstotal.cs_nbfree++; 8875322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 8885375Smckusic i = cbtocylno(fs, bno); 88934143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; 89034143Smckusick cg_blktot(cgp)[i]++; 8914426Smckusic } else { 89217224Smckusick bbase = bno - fragnum(fs, bno); 8934463Smckusic /* 8944463Smckusic * decrement the counts associated with the old frags 8954463Smckusic */ 89634143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 8975322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 8984463Smckusic /* 8994463Smckusic * deallocate the fragment 9004463Smckusic */ 9015960Smckusic frags = numfrags(fs, size); 9024463Smckusic for (i = 0; i < frags; i++) { 90334143Smckusick if (isset(cg_blksfree(cgp), bno + i)) { 9046716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9056716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 90631402Smckusick panic("blkfree: freeing free frag"); 9076716Smckusick } 90834143Smckusick setbit(cg_blksfree(cgp), bno + i); 9094426Smckusic } 9106294Smckusick cgp->cg_cs.cs_nffree += i; 9116294Smckusick fs->fs_cstotal.cs_nffree += i; 9126294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 9134463Smckusic /* 9144463Smckusic * add back in counts associated with the new frags 9154463Smckusic */ 91634143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 9175322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 9184463Smckusic /* 9194463Smckusic * if a complete block has been reassembled, account for it 9204463Smckusic */ 92134476Smckusick if (isblock(fs, cg_blksfree(cgp), 92234476Smckusick (daddr_t)fragstoblks(fs, bbase))) { 9235322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 9245322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 9255322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 9264792Smckusic cgp->cg_cs.cs_nbfree++; 9274792Smckusic fs->fs_cstotal.cs_nbfree++; 9285322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9295375Smckusic i = cbtocylno(fs, bbase); 93034143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 93134143Smckusick cg_blktot(cgp)[i]++; 9324426Smckusic } 9334426Smckusic } 9344359Smckusick fs->fs_fmod++; 9354359Smckusick bdwrite(bp); 9364359Smckusick } 9374359Smckusick 9385375Smckusic /* 9395375Smckusic * Free an inode. 9405375Smckusic * 9415375Smckusic * The specified inode is placed back in the free map. 9425375Smckusic */ 9435965Smckusic ifree(ip, ino, mode) 9445965Smckusic struct inode *ip; 9454359Smckusick ino_t ino; 9464359Smckusick int mode; 9474359Smckusick { 9484359Smckusick register struct fs *fs; 9494359Smckusick register struct cg *cgp; 950*37735Smckusick struct buf *bp; 951*37735Smckusick int error, cg; 9524359Smckusick 9535965Smckusic fs = ip->i_fs; 9546716Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 9556716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9566716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9574359Smckusick panic("ifree: range"); 9586716Smckusick } 9595377Smckusic cg = itog(fs, ino); 960*37735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 961*37735Smckusick (int)fs->fs_cgsize, &bp); 962*37735Smckusick if (error) { 963*37735Smckusick brelse(bp); 964*37735Smckusick return; 965*37735Smckusick } 9666531Smckusick cgp = bp->b_un.b_cg; 967*37735Smckusick if (!cg_chkmagic(cgp)) { 9685960Smckusic brelse(bp); 9694359Smckusick return; 9705960Smckusic } 9718105Sroot cgp->cg_time = time.tv_sec; 9724359Smckusick ino %= fs->fs_ipg; 97334143Smckusick if (isclr(cg_inosused(cgp), ino)) { 9746716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9756716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9764359Smckusick panic("ifree: freeing free inode"); 9776716Smckusick } 97834143Smckusick clrbit(cg_inosused(cgp), ino); 97916784Smckusick if (ino < cgp->cg_irotor) 98016784Smckusick cgp->cg_irotor = ino; 9814792Smckusic cgp->cg_cs.cs_nifree++; 9824792Smckusic fs->fs_cstotal.cs_nifree++; 9835322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 9844359Smckusick if ((mode & IFMT) == IFDIR) { 9854792Smckusic cgp->cg_cs.cs_ndir--; 9864792Smckusic fs->fs_cstotal.cs_ndir--; 9875322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 9884359Smckusick } 9894359Smckusick fs->fs_fmod++; 9904359Smckusick bdwrite(bp); 9914359Smckusick } 9924359Smckusick 9934463Smckusic /* 9945375Smckusic * Find a block of the specified size in the specified cylinder group. 9955375Smckusic * 9964651Smckusic * It is a panic if a request is made to find a block if none are 9974651Smckusic * available. 9984651Smckusic */ 9994651Smckusic daddr_t 10004651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 10014651Smckusic register struct fs *fs; 10024651Smckusic register struct cg *cgp; 10034651Smckusic daddr_t bpref; 10044651Smckusic int allocsiz; 10054651Smckusic { 10064651Smckusic daddr_t bno; 10074651Smckusic int start, len, loc, i; 10084651Smckusic int blk, field, subfield, pos; 10094651Smckusic 10104651Smckusic /* 10114651Smckusic * find the fragment by searching through the free block 10124651Smckusic * map for an appropriate bit pattern 10134651Smckusic */ 10144651Smckusic if (bpref) 10155377Smckusic start = dtogd(fs, bpref) / NBBY; 10164651Smckusic else 10174651Smckusic start = cgp->cg_frotor / NBBY; 10185398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 101934476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start], 102034476Smckusick (u_char *)fragtbl[fs->fs_frag], 102134476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 10224651Smckusic if (loc == 0) { 10236531Smckusick len = start + 1; 10246531Smckusick start = 0; 102534476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0], 102634476Smckusick (u_char *)fragtbl[fs->fs_frag], 102734476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 102816784Smckusick if (loc == 0) { 102916784Smckusick printf("start = %d, len = %d, fs = %s\n", 103016784Smckusick start, len, fs->fs_fsmnt); 103116784Smckusick panic("alloccg: map corrupted"); 103217697Smckusick /* NOTREACHED */ 103316784Smckusick } 10344651Smckusic } 10354651Smckusic bno = (start + len - loc) * NBBY; 10364651Smckusic cgp->cg_frotor = bno; 10374651Smckusic /* 10384651Smckusic * found the byte in the map 10394651Smckusic * sift through the bits to find the selected frag 10404651Smckusic */ 10416294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 104234143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bno); 10434651Smckusic blk <<= 1; 10444651Smckusic field = around[allocsiz]; 10454651Smckusic subfield = inside[allocsiz]; 10465322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 10476294Smckusick if ((blk & field) == subfield) 10486294Smckusick return (bno + pos); 10494651Smckusic field <<= 1; 10504651Smckusic subfield <<= 1; 10514651Smckusic } 10524651Smckusic } 10536716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 10544651Smckusic panic("alloccg: block not in map"); 10556531Smckusick return (-1); 10564651Smckusic } 10574651Smckusic 10584651Smckusic /* 10595375Smckusic * Fserr prints the name of a file system with an error diagnostic. 10605375Smckusic * 10615375Smckusic * The form of the error message is: 10624359Smckusick * fs: error message 10634359Smckusick */ 10644359Smckusick fserr(fs, cp) 10654359Smckusick struct fs *fs; 10664359Smckusick char *cp; 10674359Smckusick { 10684359Smckusick 106924839Seric log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp); 10704359Smckusick } 1071