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*39516Smckusick * @(#)lfs_alloc.c 7.12 (Berkeley) 11/12/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), 15638776Smckusick osize, NOCRED, &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) { 21738776Smckusick error = bread(ip->i_devvp, fsbtodb(fs, bprev), 21838776Smckusick osize, NOCRED, &obp); 21937735Smckusick if (error) { 2205960Smckusic brelse(obp); 22137735Smckusick return (error); 2225960Smckusic } 22325471Smckusick bn = fsbtodb(fs, bno); 22437735Smckusick bp = getblk(ip->i_devvp, bn, nsize); 22517658Smckusick bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize); 22630749Skarels count = howmany(osize, CLBYTES); 22730749Skarels for (i = 0; i < count; i++) 22837735Smckusick munhash(ip->i_devvp, bn + i * CLBYTES / DEV_BSIZE); 22917658Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 23017658Smckusick if (obp->b_flags & B_DELWRI) { 23117658Smckusick obp->b_flags &= ~B_DELWRI; 23217658Smckusick u.u_ru.ru_oublock--; /* delete charge */ 23317658Smckusick } 2344463Smckusic brelse(obp); 23531402Smckusick blkfree(ip, bprev, (off_t)osize); 23624698Smckusick if (nsize < request) 23731402Smckusick blkfree(ip, bno + numfrags(fs, nsize), 23824698Smckusick (off_t)(request - nsize)); 23912643Ssam ip->i_blocks += btodb(nsize - osize); 24012643Ssam ip->i_flag |= IUPD|ICHG; 24137735Smckusick *bpp = bp; 24237735Smckusick return (0); 2434463Smckusic } 2444792Smckusic nospace: 2454463Smckusic /* 2464463Smckusic * no space available 2474463Smckusic */ 2484426Smckusic fserr(fs, "file system full"); 2494426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 25037735Smckusick return (ENOSPC); 2514426Smckusic } 2524426Smckusic 2535375Smckusic /* 2545375Smckusic * Allocate an inode in the file system. 2555375Smckusic * 2565375Smckusic * A preference may be optionally specified. If a preference is given 2575375Smckusic * the following hierarchy is used to allocate an inode: 2585375Smckusic * 1) allocate the requested inode. 2595375Smckusic * 2) allocate an inode in the same cylinder group. 2605375Smckusic * 3) quadradically rehash into other cylinder groups, until an 2615375Smckusic * available inode is located. 2625375Smckusic * If no inode preference is given the following heirarchy is used 2635375Smckusic * to allocate an inode: 2645375Smckusic * 1) allocate an inode in cylinder group 0. 2655375Smckusic * 2) quadradically rehash into other cylinder groups, until an 2665375Smckusic * available inode is located. 2675375Smckusic */ 26837735Smckusick ialloc(pip, ipref, mode, ipp) 2695965Smckusic register struct inode *pip; 2704359Smckusick ino_t ipref; 2714359Smckusick int mode; 27237735Smckusick struct inode **ipp; 2734359Smckusick { 2745212Smckusic ino_t ino; 2754359Smckusick register struct fs *fs; 2764359Smckusick register struct inode *ip; 27737735Smckusick int cg, error; 2784359Smckusick 27937735Smckusick *ipp = 0; 2805965Smckusic fs = pip->i_fs; 2814792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 2824359Smckusick goto noinodes; 2837650Ssam #ifdef QUOTA 28437735Smckusick if (error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0)) 28537735Smckusick return (error); 2867483Skre #endif 2874948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 2884948Smckusic ipref = 0; 2895377Smckusic cg = itog(fs, ipref); 2905965Smckusic ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); 2914359Smckusick if (ino == 0) 2924359Smckusick goto noinodes; 29337735Smckusick error = iget(pip, ino, ipp); 29437735Smckusick if (error) { 29515120Skarels ifree(pip, ino, 0); 29637735Smckusick return (error); 2974359Smckusick } 29838255Smckusick ip = *ipp; 2996716Smckusick if (ip->i_mode) { 3006716Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 3016716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 3024359Smckusick panic("ialloc: dup alloc"); 3036716Smckusick } 30412643Ssam if (ip->i_blocks) { /* XXX */ 30512643Ssam printf("free inode %s/%d had %d blocks\n", 30612643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 30712643Ssam ip->i_blocks = 0; 30812643Ssam } 309*39516Smckusick ip->i_flags = 0; 31038255Smckusick /* 31138255Smckusick * Set up a new generation number for this inode. 31238255Smckusick */ 31338255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 31438255Smckusick nextgennumber = time.tv_sec; 31538255Smckusick ip->i_gen = nextgennumber; 31637735Smckusick return (0); 3174359Smckusick noinodes: 3184359Smckusick fserr(fs, "out of inodes"); 3196294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 32037735Smckusick return (ENOSPC); 3214359Smckusick } 3224359Smckusick 3234651Smckusic /* 3245375Smckusic * Find a cylinder to place a directory. 3255375Smckusic * 3265375Smckusic * The policy implemented by this algorithm is to select from 3275375Smckusic * among those cylinder groups with above the average number of 3285375Smckusic * free inodes, the one with the smallest number of directories. 3294651Smckusic */ 3309163Ssam ino_t 3315965Smckusic dirpref(fs) 3325965Smckusic register struct fs *fs; 3334359Smckusick { 3344651Smckusic int cg, minndir, mincg, avgifree; 3354359Smckusick 3364792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 3374651Smckusic minndir = fs->fs_ipg; 3384359Smckusick mincg = 0; 3394651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 3405322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 3415322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 3424359Smckusick mincg = cg; 3435322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 3444359Smckusick } 3459163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 3464359Smckusick } 3474359Smckusick 3484651Smckusic /* 3499163Ssam * Select the desired position for the next block in a file. The file is 3509163Ssam * logically divided into sections. The first section is composed of the 3519163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 3529163Ssam * 3539163Ssam * If no blocks have been allocated in the first section, the policy is to 3549163Ssam * request a block in the same cylinder group as the inode that describes 3559163Ssam * the file. If no blocks have been allocated in any other section, the 3569163Ssam * policy is to place the section in a cylinder group with a greater than 3579163Ssam * average number of free blocks. An appropriate cylinder group is found 35817696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of 35917696Smckusick * blocks is needed, the sweep begins in the cylinder group following the 36017696Smckusick * cylinder group from which the previous allocation was made. The sweep 36117696Smckusick * continues until a cylinder group with greater than the average number 36217696Smckusick * of free blocks is found. If the allocation is for the first block in an 36317696Smckusick * indirect block, the information on the previous allocation is unavailable; 36417696Smckusick * here a best guess is made based upon the logical block number being 36517696Smckusick * allocated. 3669163Ssam * 3679163Ssam * If a section is already partially allocated, the policy is to 3689163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 3699163Ssam * contiguous blocks and the beginning of the next is physically separated 3709163Ssam * so that the disk head will be in transit between them for at least 3719163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 3729163Ssam * schedule another I/O transfer. 3734651Smckusic */ 3745212Smckusic daddr_t 3759163Ssam blkpref(ip, lbn, indx, bap) 3769163Ssam struct inode *ip; 3779163Ssam daddr_t lbn; 3789163Ssam int indx; 3799163Ssam daddr_t *bap; 3809163Ssam { 3815965Smckusic register struct fs *fs; 38217696Smckusick register int cg; 38317696Smckusick int avgbfree, startcg; 3849163Ssam daddr_t nextblk; 3854651Smckusic 3869163Ssam fs = ip->i_fs; 3879163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 3889163Ssam if (lbn < NDADDR) { 3899163Ssam cg = itog(fs, ip->i_number); 3905322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 3914651Smckusic } 3929163Ssam /* 3939163Ssam * Find a cylinder with greater than average number of 3949163Ssam * unused data blocks. 3959163Ssam */ 39617696Smckusick if (indx == 0 || bap[indx - 1] == 0) 39717696Smckusick startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; 39817696Smckusick else 39917696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1; 40017696Smckusick startcg %= fs->fs_ncg; 4019163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 40217696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++) 4039163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4049163Ssam fs->fs_cgrotor = cg; 4059163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4069163Ssam } 40717696Smckusick for (cg = 0; cg <= startcg; cg++) 4089163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4099163Ssam fs->fs_cgrotor = cg; 4109163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4119163Ssam } 4129163Ssam return (NULL); 4139163Ssam } 4149163Ssam /* 4159163Ssam * One or more previous blocks have been laid out. If less 4169163Ssam * than fs_maxcontig previous blocks are contiguous, the 4179163Ssam * next block is requested contiguously, otherwise it is 4189163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 4199163Ssam */ 4209163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 4219163Ssam if (indx > fs->fs_maxcontig && 42211638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 4239163Ssam != nextblk) 4249163Ssam return (nextblk); 4259163Ssam if (fs->fs_rotdelay != 0) 4269163Ssam /* 4279163Ssam * Here we convert ms of delay to frags as: 4289163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 4299163Ssam * ((sect/frag) * (ms/sec)) 4309163Ssam * then round up to the next block. 4319163Ssam */ 4329163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 4339163Ssam (NSPF(fs) * 1000), fs->fs_frag); 4349163Ssam return (nextblk); 4354651Smckusic } 4364651Smckusic 4375375Smckusic /* 4385375Smckusic * Implement the cylinder overflow algorithm. 4395375Smckusic * 4405375Smckusic * The policy implemented by this algorithm is: 4415375Smckusic * 1) allocate the block in its requested cylinder group. 4425375Smckusic * 2) quadradically rehash on the cylinder group number. 4435375Smckusic * 3) brute force search for a free block. 4445375Smckusic */ 4455212Smckusic /*VARARGS5*/ 4465212Smckusic u_long 4475965Smckusic hashalloc(ip, cg, pref, size, allocator) 4485965Smckusic struct inode *ip; 4494359Smckusick int cg; 4504359Smckusick long pref; 4514359Smckusick int size; /* size for data blocks, mode for inodes */ 4525212Smckusic u_long (*allocator)(); 4534359Smckusick { 4545965Smckusic register struct fs *fs; 4554359Smckusick long result; 4564359Smckusick int i, icg = cg; 4574359Smckusick 4585965Smckusic fs = ip->i_fs; 4594359Smckusick /* 4604359Smckusick * 1: preferred cylinder group 4614359Smckusick */ 4625965Smckusic result = (*allocator)(ip, cg, pref, size); 4634359Smckusick if (result) 4644359Smckusick return (result); 4654359Smckusick /* 4664359Smckusick * 2: quadratic rehash 4674359Smckusick */ 4684359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 4694359Smckusick cg += i; 4704359Smckusick if (cg >= fs->fs_ncg) 4714359Smckusick cg -= fs->fs_ncg; 4725965Smckusic result = (*allocator)(ip, cg, 0, size); 4734359Smckusick if (result) 4744359Smckusick return (result); 4754359Smckusick } 4764359Smckusick /* 4774359Smckusick * 3: brute force search 47810847Ssam * Note that we start at i == 2, since 0 was checked initially, 47910847Ssam * and 1 is always checked in the quadratic rehash. 4804359Smckusick */ 48110848Smckusick cg = (icg + 2) % fs->fs_ncg; 48210847Ssam for (i = 2; i < fs->fs_ncg; i++) { 4835965Smckusic result = (*allocator)(ip, cg, 0, size); 4844359Smckusick if (result) 4854359Smckusick return (result); 4864359Smckusick cg++; 4874359Smckusick if (cg == fs->fs_ncg) 4884359Smckusick cg = 0; 4894359Smckusick } 4906294Smckusick return (NULL); 4914359Smckusick } 4924359Smckusick 4935375Smckusic /* 4945375Smckusic * Determine whether a fragment can be extended. 4955375Smckusic * 4965375Smckusic * Check to see if the necessary fragments are available, and 4975375Smckusic * if they are, allocate them. 4985375Smckusic */ 4994359Smckusick daddr_t 5005965Smckusic fragextend(ip, cg, bprev, osize, nsize) 5015965Smckusic struct inode *ip; 5024426Smckusic int cg; 5034463Smckusic long bprev; 5044426Smckusic int osize, nsize; 5054426Smckusic { 5065965Smckusic register struct fs *fs; 5074463Smckusic register struct cg *cgp; 50837735Smckusick struct buf *bp; 5094463Smckusic long bno; 5104463Smckusic int frags, bbase; 51137735Smckusick int i, error; 5124426Smckusic 5135965Smckusic fs = ip->i_fs; 51417224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 5156531Smckusick return (NULL); 5165960Smckusic frags = numfrags(fs, nsize); 51717224Smckusick bbase = fragnum(fs, bprev); 51817224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 51930749Skarels /* cannot extend across a block boundary */ 5206294Smckusick return (NULL); 5214463Smckusic } 52237735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 52338776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 52437735Smckusick if (error) { 52537735Smckusick brelse(bp); 52637735Smckusick return (NULL); 52737735Smckusick } 5286531Smckusick cgp = bp->b_un.b_cg; 52937735Smckusick if (!cg_chkmagic(cgp)) { 5305960Smckusic brelse(bp); 5316294Smckusick return (NULL); 5325960Smckusic } 5338105Sroot cgp->cg_time = time.tv_sec; 5345377Smckusic bno = dtogd(fs, bprev); 5355960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 53634143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) { 5375361Smckusic brelse(bp); 5386294Smckusick return (NULL); 5395361Smckusic } 5405361Smckusic /* 5415361Smckusic * the current fragment can be extended 5425361Smckusic * deduct the count on fragment being extended into 5435361Smckusic * increase the count on the remaining fragment (if any) 5445361Smckusic * allocate the extended piece 5455361Smckusic */ 5465361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 54734143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) 5484463Smckusic break; 5495960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 5505361Smckusic if (i != frags) 5515361Smckusic cgp->cg_frsum[i - frags]++; 5525960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 55334143Smckusick clrbit(cg_blksfree(cgp), bno + i); 5545361Smckusic cgp->cg_cs.cs_nffree--; 5555361Smckusic fs->fs_cstotal.cs_nffree--; 5565361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 5574463Smckusic } 5585361Smckusic fs->fs_fmod++; 5595361Smckusic bdwrite(bp); 5605361Smckusic return (bprev); 5614426Smckusic } 5624426Smckusic 5635375Smckusic /* 5645375Smckusic * Determine whether a block can be allocated. 5655375Smckusic * 5665375Smckusic * Check to see if a block of the apprpriate size is available, 5675375Smckusic * and if it is, allocate it. 5685375Smckusic */ 5699163Ssam daddr_t 5705965Smckusic alloccg(ip, cg, bpref, size) 5715965Smckusic struct inode *ip; 5724359Smckusick int cg; 5734359Smckusick daddr_t bpref; 5744359Smckusick int size; 5754359Smckusick { 5765965Smckusic register struct fs *fs; 5774463Smckusic register struct cg *cgp; 57837735Smckusick struct buf *bp; 5794463Smckusic register int i; 58037735Smckusick int error, bno, frags, allocsiz; 5814359Smckusick 5825965Smckusic fs = ip->i_fs; 5835322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 5846294Smckusick return (NULL); 58537735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 58638776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 58737735Smckusick if (error) { 58837735Smckusick brelse(bp); 58937735Smckusick return (NULL); 59037735Smckusick } 5916531Smckusick cgp = bp->b_un.b_cg; 59237735Smckusick if (!cg_chkmagic(cgp) || 59315950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 5945960Smckusic brelse(bp); 5956294Smckusick return (NULL); 5965960Smckusic } 5978105Sroot cgp->cg_time = time.tv_sec; 5985322Smckusic if (size == fs->fs_bsize) { 5995212Smckusic bno = alloccgblk(fs, cgp, bpref); 6004463Smckusic bdwrite(bp); 6014463Smckusic return (bno); 6024463Smckusic } 6034463Smckusic /* 6044463Smckusic * check to see if any fragments are already available 6054463Smckusic * allocsiz is the size which will be allocated, hacking 6064463Smckusic * it down to a smaller size if necessary 6074463Smckusic */ 6085960Smckusic frags = numfrags(fs, size); 6095322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 6104463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 6114463Smckusic break; 6125322Smckusic if (allocsiz == fs->fs_frag) { 6134463Smckusic /* 6144463Smckusic * no fragments were available, so a block will be 6154463Smckusic * allocated, and hacked up 6164463Smckusic */ 6174792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 6184463Smckusic brelse(bp); 6196294Smckusick return (NULL); 6204463Smckusic } 6215212Smckusic bno = alloccgblk(fs, cgp, bpref); 6225377Smckusic bpref = dtogd(fs, bno); 6235322Smckusic for (i = frags; i < fs->fs_frag; i++) 62434143Smckusick setbit(cg_blksfree(cgp), bpref + i); 6255322Smckusic i = fs->fs_frag - frags; 6264792Smckusic cgp->cg_cs.cs_nffree += i; 6274792Smckusic fs->fs_cstotal.cs_nffree += i; 6285322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 6299762Ssam fs->fs_fmod++; 6304463Smckusic cgp->cg_frsum[i]++; 6314463Smckusic bdwrite(bp); 6324463Smckusic return (bno); 6334463Smckusic } 6344651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 63515950Smckusick if (bno < 0) { 63615950Smckusick brelse(bp); 6376294Smckusick return (NULL); 63815950Smckusick } 6394463Smckusic for (i = 0; i < frags; i++) 64034143Smckusick clrbit(cg_blksfree(cgp), bno + i); 6414792Smckusic cgp->cg_cs.cs_nffree -= frags; 6424792Smckusic fs->fs_cstotal.cs_nffree -= frags; 6435322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 6449762Ssam fs->fs_fmod++; 6454463Smckusic cgp->cg_frsum[allocsiz]--; 6464463Smckusic if (frags != allocsiz) 6474463Smckusic cgp->cg_frsum[allocsiz - frags]++; 6484463Smckusic bdwrite(bp); 6494463Smckusic return (cg * fs->fs_fpg + bno); 6504463Smckusic } 6514463Smckusic 6525375Smckusic /* 6535375Smckusic * Allocate a block in a cylinder group. 6545375Smckusic * 6555375Smckusic * This algorithm implements the following policy: 6565375Smckusic * 1) allocate the requested block. 6575375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 6585375Smckusic * 3) allocate the next available block on the block rotor for the 6595375Smckusic * specified cylinder group. 6605375Smckusic * Note that this routine only allocates fs_bsize blocks; these 6615375Smckusic * blocks may be fragmented by the routine that allocates them. 6625375Smckusic */ 6634463Smckusic daddr_t 6645212Smckusic alloccgblk(fs, cgp, bpref) 6655965Smckusic register struct fs *fs; 6664463Smckusic register struct cg *cgp; 6674463Smckusic daddr_t bpref; 6684463Smckusic { 6694651Smckusic daddr_t bno; 6706294Smckusick int cylno, pos, delta; 6714651Smckusic short *cylbp; 6725361Smckusic register int i; 6734463Smckusic 6744651Smckusic if (bpref == 0) { 6754651Smckusic bpref = cgp->cg_rotor; 6765361Smckusic goto norot; 6775361Smckusic } 67817224Smckusick bpref = blknum(fs, bpref); 6795377Smckusic bpref = dtogd(fs, bpref); 6805361Smckusic /* 6815361Smckusic * if the requested block is available, use it 6825361Smckusic */ 68334143Smckusick if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 6845361Smckusic bno = bpref; 6855361Smckusic goto gotit; 6865361Smckusic } 6875361Smckusic /* 6885361Smckusic * check for a block available on the same cylinder 6895361Smckusic */ 6905361Smckusic cylno = cbtocylno(fs, bpref); 69134143Smckusick if (cg_blktot(cgp)[cylno] == 0) 6925375Smckusic goto norot; 6935375Smckusic if (fs->fs_cpc == 0) { 6945375Smckusic /* 6955375Smckusic * block layout info is not available, so just have 6965375Smckusic * to take any block in this cylinder. 6975375Smckusic */ 6985375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 6995375Smckusic goto norot; 7005375Smckusic } 7015375Smckusic /* 7025361Smckusic * check the summary information to see if a block is 7035361Smckusic * available in the requested cylinder starting at the 7049163Ssam * requested rotational position and proceeding around. 7055361Smckusic */ 70634143Smckusick cylbp = cg_blks(fs, cgp, cylno); 7079163Ssam pos = cbtorpos(fs, bpref); 70834143Smckusick for (i = pos; i < fs->fs_nrpos; i++) 7095361Smckusic if (cylbp[i] > 0) 7105361Smckusic break; 71134143Smckusick if (i == fs->fs_nrpos) 7125361Smckusic for (i = 0; i < pos; i++) 7135361Smckusic if (cylbp[i] > 0) 7145361Smckusic break; 7155361Smckusic if (cylbp[i] > 0) { 7164651Smckusic /* 7175361Smckusic * found a rotational position, now find the actual 7185361Smckusic * block. A panic if none is actually there. 7194651Smckusic */ 7205361Smckusic pos = cylno % fs->fs_cpc; 7215361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 72234143Smckusick if (fs_postbl(fs, pos)[i] == -1) { 7236716Smckusick printf("pos = %d, i = %d, fs = %s\n", 7246716Smckusick pos, i, fs->fs_fsmnt); 7255361Smckusic panic("alloccgblk: cyl groups corrupted"); 7266716Smckusick } 72734143Smckusick for (i = fs_postbl(fs, pos)[i];; ) { 72834143Smckusick if (isblock(fs, cg_blksfree(cgp), bno + i)) { 72911638Ssam bno = blkstofrags(fs, (bno + i)); 7305361Smckusic goto gotit; 7315361Smckusic } 73234143Smckusick delta = fs_rotbl(fs)[i]; 73334143Smckusick if (delta <= 0 || 73434143Smckusick delta + i > fragstoblks(fs, fs->fs_fpg)) 7354651Smckusic break; 7366294Smckusick i += delta; 7374651Smckusic } 7386716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 7395361Smckusic panic("alloccgblk: can't find blk in cyl"); 7404359Smckusick } 7415361Smckusic norot: 7425361Smckusic /* 7435361Smckusic * no blocks in the requested cylinder, so take next 7445361Smckusic * available one in this cylinder group. 7455361Smckusic */ 7468628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 7476567Smckusic if (bno < 0) 7486294Smckusick return (NULL); 7494651Smckusic cgp->cg_rotor = bno; 7504359Smckusick gotit: 75134143Smckusick clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno)); 7524792Smckusic cgp->cg_cs.cs_nbfree--; 7534792Smckusic fs->fs_cstotal.cs_nbfree--; 7545322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 7555375Smckusic cylno = cbtocylno(fs, bno); 75634143Smckusick cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 75734143Smckusick cg_blktot(cgp)[cylno]--; 7584359Smckusick fs->fs_fmod++; 7594651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 7604359Smckusick } 76134143Smckusick 7625375Smckusic /* 7635375Smckusic * Determine whether an inode can be allocated. 7645375Smckusic * 7655375Smckusic * Check to see if an inode is available, and if it is, 7665375Smckusic * allocate it using the following policy: 7675375Smckusic * 1) allocate the requested inode. 7685375Smckusic * 2) allocate the next available inode after the requested 7695375Smckusic * inode in the specified cylinder group. 7705375Smckusic */ 7719163Ssam ino_t 7725965Smckusic ialloccg(ip, cg, ipref, mode) 7735965Smckusic struct inode *ip; 7744359Smckusick int cg; 7754359Smckusick daddr_t ipref; 7764359Smckusick int mode; 7774359Smckusick { 7785965Smckusic register struct fs *fs; 7794463Smckusic register struct cg *cgp; 78016784Smckusick struct buf *bp; 78137735Smckusick int error, start, len, loc, map, i; 7824359Smckusick 7835965Smckusic fs = ip->i_fs; 7845322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 7856294Smckusick return (NULL); 78637735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 78738776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 78837735Smckusick if (error) { 78937735Smckusick brelse(bp); 79037735Smckusick return (NULL); 79137735Smckusick } 7926531Smckusick cgp = bp->b_un.b_cg; 79337735Smckusick if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 7945960Smckusic brelse(bp); 7956294Smckusick return (NULL); 7965960Smckusic } 7978105Sroot cgp->cg_time = time.tv_sec; 7984359Smckusick if (ipref) { 7994359Smckusick ipref %= fs->fs_ipg; 80034143Smckusick if (isclr(cg_inosused(cgp), ipref)) 8014359Smckusick goto gotit; 80216784Smckusick } 80316784Smckusick start = cgp->cg_irotor / NBBY; 80416784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 80534143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 80616784Smckusick if (loc == 0) { 80717697Smckusick len = start + 1; 80817697Smckusick start = 0; 80934143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 81017697Smckusick if (loc == 0) { 81117697Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 81217697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 81317697Smckusick panic("ialloccg: map corrupted"); 81417697Smckusick /* NOTREACHED */ 81517697Smckusick } 81616784Smckusick } 81716784Smckusick i = start + len - loc; 81834143Smckusick map = cg_inosused(cgp)[i]; 81916784Smckusick ipref = i * NBBY; 82016784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 82116784Smckusick if ((map & i) == 0) { 8224359Smckusick cgp->cg_irotor = ipref; 8234359Smckusick goto gotit; 8244359Smckusick } 8254359Smckusick } 82616784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 82716784Smckusick panic("ialloccg: block not in map"); 82816784Smckusick /* NOTREACHED */ 8294359Smckusick gotit: 83034143Smckusick setbit(cg_inosused(cgp), ipref); 8314792Smckusic cgp->cg_cs.cs_nifree--; 8324792Smckusic fs->fs_cstotal.cs_nifree--; 8335322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 8344359Smckusick fs->fs_fmod++; 8354359Smckusick if ((mode & IFMT) == IFDIR) { 8364792Smckusic cgp->cg_cs.cs_ndir++; 8374792Smckusic fs->fs_cstotal.cs_ndir++; 8385322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 8394359Smckusick } 8404359Smckusick bdwrite(bp); 8414359Smckusick return (cg * fs->fs_ipg + ipref); 8424359Smckusick } 8434359Smckusick 8445375Smckusic /* 8455375Smckusic * Free a block or fragment. 8465375Smckusic * 8475375Smckusic * The specified block or fragment is placed back in the 8485375Smckusic * free map. If a fragment is deallocated, a possible 8495375Smckusic * block reassembly is checked. 8505375Smckusic */ 85131402Smckusick blkfree(ip, bno, size) 8525965Smckusic register struct inode *ip; 8534359Smckusick daddr_t bno; 8545212Smckusic off_t size; 8554359Smckusick { 8564359Smckusick register struct fs *fs; 8574359Smckusick register struct cg *cgp; 85837735Smckusick struct buf *bp; 85937735Smckusick int error, cg, blk, frags, bbase; 8604463Smckusic register int i; 8614359Smckusick 8625965Smckusic fs = ip->i_fs; 8636716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 8646716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 8656716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 86631402Smckusick panic("blkfree: bad size"); 8676716Smckusick } 8685377Smckusic cg = dtog(fs, bno); 8696567Smckusic if (badblock(fs, bno)) { 8706567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 8714359Smckusick return; 8726567Smckusic } 87337735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 87438776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 87537735Smckusick if (error) { 87637735Smckusick brelse(bp); 87737735Smckusick return; 87837735Smckusick } 8796531Smckusick cgp = bp->b_un.b_cg; 88037735Smckusick if (!cg_chkmagic(cgp)) { 8815960Smckusic brelse(bp); 8824359Smckusick return; 8835960Smckusic } 8848105Sroot cgp->cg_time = time.tv_sec; 8855377Smckusic bno = dtogd(fs, bno); 8865322Smckusic if (size == fs->fs_bsize) { 88734143Smckusick if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) { 8886716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 8896716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 89031402Smckusick panic("blkfree: freeing free block"); 8916567Smckusic } 89234143Smckusick setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno)); 8934792Smckusic cgp->cg_cs.cs_nbfree++; 8944792Smckusic fs->fs_cstotal.cs_nbfree++; 8955322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 8965375Smckusic i = cbtocylno(fs, bno); 89734143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; 89834143Smckusick cg_blktot(cgp)[i]++; 8994426Smckusic } else { 90017224Smckusick bbase = bno - fragnum(fs, bno); 9014463Smckusic /* 9024463Smckusic * decrement the counts associated with the old frags 9034463Smckusic */ 90434143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 9055322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 9064463Smckusic /* 9074463Smckusic * deallocate the fragment 9084463Smckusic */ 9095960Smckusic frags = numfrags(fs, size); 9104463Smckusic for (i = 0; i < frags; i++) { 91134143Smckusick if (isset(cg_blksfree(cgp), bno + i)) { 9126716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9136716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 91431402Smckusick panic("blkfree: freeing free frag"); 9156716Smckusick } 91634143Smckusick setbit(cg_blksfree(cgp), bno + i); 9174426Smckusic } 9186294Smckusick cgp->cg_cs.cs_nffree += i; 9196294Smckusick fs->fs_cstotal.cs_nffree += i; 9206294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 9214463Smckusic /* 9224463Smckusic * add back in counts associated with the new frags 9234463Smckusic */ 92434143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 9255322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 9264463Smckusic /* 9274463Smckusic * if a complete block has been reassembled, account for it 9284463Smckusic */ 92934476Smckusick if (isblock(fs, cg_blksfree(cgp), 93034476Smckusick (daddr_t)fragstoblks(fs, bbase))) { 9315322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 9325322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 9335322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 9344792Smckusic cgp->cg_cs.cs_nbfree++; 9354792Smckusic fs->fs_cstotal.cs_nbfree++; 9365322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9375375Smckusic i = cbtocylno(fs, bbase); 93834143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 93934143Smckusick cg_blktot(cgp)[i]++; 9404426Smckusic } 9414426Smckusic } 9424359Smckusick fs->fs_fmod++; 9434359Smckusick bdwrite(bp); 9444359Smckusick } 9454359Smckusick 9465375Smckusic /* 9475375Smckusic * Free an inode. 9485375Smckusic * 9495375Smckusic * The specified inode is placed back in the free map. 9505375Smckusic */ 9515965Smckusic ifree(ip, ino, mode) 9525965Smckusic struct inode *ip; 9534359Smckusick ino_t ino; 9544359Smckusick int mode; 9554359Smckusick { 9564359Smckusick register struct fs *fs; 9574359Smckusick register struct cg *cgp; 95837735Smckusick struct buf *bp; 95937735Smckusick int error, cg; 9604359Smckusick 9615965Smckusic fs = ip->i_fs; 9626716Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 9636716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9646716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9654359Smckusick panic("ifree: range"); 9666716Smckusick } 9675377Smckusic cg = itog(fs, ino); 96837735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 96938776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 97037735Smckusick if (error) { 97137735Smckusick brelse(bp); 97237735Smckusick return; 97337735Smckusick } 9746531Smckusick cgp = bp->b_un.b_cg; 97537735Smckusick if (!cg_chkmagic(cgp)) { 9765960Smckusic brelse(bp); 9774359Smckusick return; 9785960Smckusic } 9798105Sroot cgp->cg_time = time.tv_sec; 9804359Smckusick ino %= fs->fs_ipg; 98134143Smckusick if (isclr(cg_inosused(cgp), ino)) { 9826716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9836716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9844359Smckusick panic("ifree: freeing free inode"); 9856716Smckusick } 98634143Smckusick clrbit(cg_inosused(cgp), ino); 98716784Smckusick if (ino < cgp->cg_irotor) 98816784Smckusick cgp->cg_irotor = ino; 9894792Smckusic cgp->cg_cs.cs_nifree++; 9904792Smckusic fs->fs_cstotal.cs_nifree++; 9915322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 9924359Smckusick if ((mode & IFMT) == IFDIR) { 9934792Smckusic cgp->cg_cs.cs_ndir--; 9944792Smckusic fs->fs_cstotal.cs_ndir--; 9955322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 9964359Smckusick } 9974359Smckusick fs->fs_fmod++; 9984359Smckusick bdwrite(bp); 9994359Smckusick } 10004359Smckusick 10014463Smckusic /* 10025375Smckusic * Find a block of the specified size in the specified cylinder group. 10035375Smckusic * 10044651Smckusic * It is a panic if a request is made to find a block if none are 10054651Smckusic * available. 10064651Smckusic */ 10074651Smckusic daddr_t 10084651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 10094651Smckusic register struct fs *fs; 10104651Smckusic register struct cg *cgp; 10114651Smckusic daddr_t bpref; 10124651Smckusic int allocsiz; 10134651Smckusic { 10144651Smckusic daddr_t bno; 10154651Smckusic int start, len, loc, i; 10164651Smckusic int blk, field, subfield, pos; 10174651Smckusic 10184651Smckusic /* 10194651Smckusic * find the fragment by searching through the free block 10204651Smckusic * map for an appropriate bit pattern 10214651Smckusic */ 10224651Smckusic if (bpref) 10235377Smckusic start = dtogd(fs, bpref) / NBBY; 10244651Smckusic else 10254651Smckusic start = cgp->cg_frotor / NBBY; 10265398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 102734476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start], 102834476Smckusick (u_char *)fragtbl[fs->fs_frag], 102934476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 10304651Smckusic if (loc == 0) { 10316531Smckusick len = start + 1; 10326531Smckusick start = 0; 103334476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0], 103434476Smckusick (u_char *)fragtbl[fs->fs_frag], 103534476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 103616784Smckusick if (loc == 0) { 103716784Smckusick printf("start = %d, len = %d, fs = %s\n", 103816784Smckusick start, len, fs->fs_fsmnt); 103916784Smckusick panic("alloccg: map corrupted"); 104017697Smckusick /* NOTREACHED */ 104116784Smckusick } 10424651Smckusic } 10434651Smckusic bno = (start + len - loc) * NBBY; 10444651Smckusic cgp->cg_frotor = bno; 10454651Smckusic /* 10464651Smckusic * found the byte in the map 10474651Smckusic * sift through the bits to find the selected frag 10484651Smckusic */ 10496294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 105034143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bno); 10514651Smckusic blk <<= 1; 10524651Smckusic field = around[allocsiz]; 10534651Smckusic subfield = inside[allocsiz]; 10545322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 10556294Smckusick if ((blk & field) == subfield) 10566294Smckusick return (bno + pos); 10574651Smckusic field <<= 1; 10584651Smckusic subfield <<= 1; 10594651Smckusic } 10604651Smckusic } 10616716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 10624651Smckusic panic("alloccg: block not in map"); 10636531Smckusick return (-1); 10644651Smckusic } 10654651Smckusic 10664651Smckusic /* 10675375Smckusic * Fserr prints the name of a file system with an error diagnostic. 10685375Smckusic * 10695375Smckusic * The form of the error message is: 10704359Smckusick * fs: error message 10714359Smckusick */ 10724359Smckusick fserr(fs, cp) 10734359Smckusick struct fs *fs; 10744359Smckusick char *cp; 10754359Smckusick { 10764359Smckusick 107724839Seric log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp); 10784359Smckusick } 1079