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*39678Smckusick * @(#)lfs_alloc.c 7.13 (Berkeley) 11/30/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 */ 62*39678Smckusick alloc(ip, lbn, bpref, size, bnp) 634463Smckusic register struct inode *ip; 64*39678Smckusick daddr_t lbn, bpref; 654359Smckusick int size; 66*39678Smckusick daddr_t *bnp; 674359Smckusick { 684359Smckusick daddr_t bno; 694359Smckusick register struct fs *fs; 704463Smckusic register struct buf *bp; 7137735Smckusick int cg, error; 724359Smckusick 73*39678Smckusick *bnp = 0; 745965Smckusic fs = ip->i_fs; 756716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 766716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 776716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 784463Smckusic panic("alloc: bad size"); 796716Smckusick } 805322Smckusic if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 814359Smckusick goto nospace; 8211638Ssam if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 834792Smckusic goto nospace; 847650Ssam #ifdef QUOTA 8537735Smckusick if (error = chkdq(ip, (long)btodb(size), 0)) 8637735Smckusick return (error); 877483Skre #endif 884948Smckusic if (bpref >= fs->fs_size) 894948Smckusic bpref = 0; 904359Smckusick if (bpref == 0) 915377Smckusic cg = itog(fs, ip->i_number); 924359Smckusick else 935377Smckusic cg = dtog(fs, bpref); 949163Ssam bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, 959163Ssam (u_long (*)())alloccg); 96*39678Smckusick if (bno > 0) { 97*39678Smckusick ip->i_blocks += btodb(size); 98*39678Smckusick ip->i_flag |= IUPD|ICHG; 99*39678Smckusick *bnp = bno; 100*39678Smckusick return (0); 101*39678Smckusick } 1024359Smckusick nospace: 1034359Smckusick fserr(fs, "file system full"); 1044359Smckusick uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 10537735Smckusick return (ENOSPC); 1064359Smckusick } 1074359Smckusick 1085375Smckusic /* 1095375Smckusic * Reallocate a fragment to a bigger size 1105375Smckusic * 1115375Smckusic * The number and size of the old block is given, and a preference 1125375Smckusic * and new size is also specified. The allocator attempts to extend 1135375Smckusic * the original block. Failing that, the regular block allocator is 1145375Smckusic * invoked to get an appropriate block. 1155375Smckusic */ 116*39678Smckusick realloccg(ip, lbprev, bpref, osize, nsize, bpp) 1175965Smckusic register struct inode *ip; 118*39678Smckusick off_t lbprev; 119*39678Smckusick daddr_t bpref; 1204426Smckusic int osize, nsize; 12137735Smckusick struct buf **bpp; 1224426Smckusic { 1234426Smckusic register struct fs *fs; 12437735Smckusick struct buf *bp, *obp; 12524698Smckusick int cg, request; 126*39678Smckusick daddr_t bprev, bno, bn; 12737735Smckusick int i, error, count; 1284426Smckusic 12937735Smckusick *bpp = 0; 1305965Smckusic fs = ip->i_fs; 1315960Smckusic if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 1326716Smckusick (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 1336716Smckusick printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 1346716Smckusick ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 1354463Smckusic panic("realloccg: bad size"); 1366716Smckusick } 13711638Ssam if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 1384792Smckusic goto nospace; 139*39678Smckusick if ((bprev = ip->i_db[lbprev]) == 0) { 1406716Smckusick printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 1416716Smckusick ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 1424463Smckusic panic("realloccg: bad bprev"); 1436716Smckusick } 1447650Ssam #ifdef QUOTA 14537735Smckusick if (error = chkdq(ip, (long)btodb(nsize - osize), 0)) 14637735Smckusick return (error); 1477483Skre #endif 148*39678Smckusick /* 149*39678Smckusick * Allocate the extra space in the buffer. 150*39678Smckusick */ 151*39678Smckusick if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) { 152*39678Smckusick brelse(bp); 153*39678Smckusick return (error); 154*39678Smckusick } 155*39678Smckusick brealloc(bp, nsize); 156*39678Smckusick bp->b_flags |= B_DONE; 157*39678Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 158*39678Smckusick ip->i_blocks += btodb(nsize - osize); 159*39678Smckusick ip->i_flag |= IUPD|ICHG; 160*39678Smckusick /* 161*39678Smckusick * Check for extension in the existing location. 162*39678Smckusick */ 1636294Smckusick cg = dtog(fs, bprev); 164*39678Smckusick if (bno = fragextend(ip, cg, (long)bprev, osize, nsize)) { 165*39678Smckusick if (bp->b_blkno != bno) 166*39678Smckusick panic("bad blockno"); 16737735Smckusick *bpp = bp; 16837735Smckusick return (0); 1694463Smckusic } 170*39678Smckusick /* 171*39678Smckusick * Allocate a new disk location. 172*39678Smckusick */ 1734948Smckusic if (bpref >= fs->fs_size) 1744948Smckusic bpref = 0; 17526253Skarels switch ((int)fs->fs_optim) { 17625256Smckusick case FS_OPTSPACE: 17725256Smckusick /* 17825256Smckusick * Allocate an exact sized fragment. Although this makes 17925256Smckusick * best use of space, we will waste time relocating it if 18025256Smckusick * the file continues to grow. If the fragmentation is 18125256Smckusick * less than half of the minimum free reserve, we choose 18225256Smckusick * to begin optimizing for time. 18325256Smckusick */ 18424698Smckusick request = nsize; 18525256Smckusick if (fs->fs_minfree < 5 || 18625256Smckusick fs->fs_cstotal.cs_nffree > 18725256Smckusick fs->fs_dsize * fs->fs_minfree / (2 * 100)) 18825256Smckusick break; 18925256Smckusick log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 19025256Smckusick fs->fs_fsmnt); 19125256Smckusick fs->fs_optim = FS_OPTTIME; 19225256Smckusick break; 19325256Smckusick case FS_OPTTIME: 19425256Smckusick /* 19525256Smckusick * At this point we have discovered a file that is trying 19625256Smckusick * to grow a small fragment to a larger fragment. To save 19725256Smckusick * time, we allocate a full sized block, then free the 19825256Smckusick * unused portion. If the file continues to grow, the 19925256Smckusick * `fragextend' call above will be able to grow it in place 20025256Smckusick * without further copying. If aberrant programs cause 20125256Smckusick * disk fragmentation to grow within 2% of the free reserve, 20225256Smckusick * we choose to begin optimizing for space. 20325256Smckusick */ 20424698Smckusick request = fs->fs_bsize; 20525256Smckusick if (fs->fs_cstotal.cs_nffree < 20625256Smckusick fs->fs_dsize * (fs->fs_minfree - 2) / 100) 20725256Smckusick break; 20825256Smckusick log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 20925256Smckusick fs->fs_fsmnt); 21025256Smckusick fs->fs_optim = FS_OPTSPACE; 21125256Smckusick break; 21225256Smckusick default: 21325256Smckusick printf("dev = 0x%x, optim = %d, fs = %s\n", 21425256Smckusick ip->i_dev, fs->fs_optim, fs->fs_fsmnt); 21525256Smckusick panic("realloccg: bad optim"); 21625256Smckusick /* NOTREACHED */ 21725256Smckusick } 21824698Smckusick bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request, 2199163Ssam (u_long (*)())alloccg); 2206567Smckusic if (bno > 0) { 221*39678Smckusick bp->b_blkno = bn = fsbtodb(fs, bno); 22230749Skarels count = howmany(osize, CLBYTES); 22330749Skarels for (i = 0; i < count; i++) 22437735Smckusick munhash(ip->i_devvp, bn + i * CLBYTES / DEV_BSIZE); 22531402Smckusick blkfree(ip, bprev, (off_t)osize); 22624698Smckusick if (nsize < request) 22731402Smckusick blkfree(ip, bno + numfrags(fs, nsize), 22824698Smckusick (off_t)(request - nsize)); 22912643Ssam ip->i_blocks += btodb(nsize - osize); 23012643Ssam ip->i_flag |= IUPD|ICHG; 23137735Smckusick *bpp = bp; 23237735Smckusick return (0); 2334463Smckusic } 234*39678Smckusick brelse(bp); 2354792Smckusic nospace: 2364463Smckusic /* 2374463Smckusic * no space available 2384463Smckusic */ 2394426Smckusic fserr(fs, "file system full"); 2404426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 24137735Smckusick return (ENOSPC); 2424426Smckusic } 2434426Smckusic 2445375Smckusic /* 2455375Smckusic * Allocate an inode in the file system. 2465375Smckusic * 2475375Smckusic * A preference may be optionally specified. If a preference is given 2485375Smckusic * the following hierarchy is used to allocate an inode: 2495375Smckusic * 1) allocate the requested inode. 2505375Smckusic * 2) allocate an inode in the same cylinder group. 2515375Smckusic * 3) quadradically rehash into other cylinder groups, until an 2525375Smckusic * available inode is located. 2535375Smckusic * If no inode preference is given the following heirarchy is used 2545375Smckusic * to allocate an inode: 2555375Smckusic * 1) allocate an inode in cylinder group 0. 2565375Smckusic * 2) quadradically rehash into other cylinder groups, until an 2575375Smckusic * available inode is located. 2585375Smckusic */ 25937735Smckusick ialloc(pip, ipref, mode, ipp) 2605965Smckusic register struct inode *pip; 2614359Smckusick ino_t ipref; 2624359Smckusick int mode; 26337735Smckusick struct inode **ipp; 2644359Smckusick { 2655212Smckusic ino_t ino; 2664359Smckusick register struct fs *fs; 2674359Smckusick register struct inode *ip; 26837735Smckusick int cg, error; 2694359Smckusick 27037735Smckusick *ipp = 0; 2715965Smckusic fs = pip->i_fs; 2724792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 2734359Smckusick goto noinodes; 2747650Ssam #ifdef QUOTA 27537735Smckusick if (error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0)) 27637735Smckusick return (error); 2777483Skre #endif 2784948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 2794948Smckusic ipref = 0; 2805377Smckusic cg = itog(fs, ipref); 2815965Smckusic ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); 2824359Smckusick if (ino == 0) 2834359Smckusick goto noinodes; 28437735Smckusick error = iget(pip, ino, ipp); 28537735Smckusick if (error) { 28615120Skarels ifree(pip, ino, 0); 28737735Smckusick return (error); 2884359Smckusick } 28938255Smckusick ip = *ipp; 2906716Smckusick if (ip->i_mode) { 2916716Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 2926716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 2934359Smckusick panic("ialloc: dup alloc"); 2946716Smckusick } 29512643Ssam if (ip->i_blocks) { /* XXX */ 29612643Ssam printf("free inode %s/%d had %d blocks\n", 29712643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 29812643Ssam ip->i_blocks = 0; 29912643Ssam } 30039516Smckusick ip->i_flags = 0; 30138255Smckusick /* 30238255Smckusick * Set up a new generation number for this inode. 30338255Smckusick */ 30438255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 30538255Smckusick nextgennumber = time.tv_sec; 30638255Smckusick ip->i_gen = nextgennumber; 30737735Smckusick return (0); 3084359Smckusick noinodes: 3094359Smckusick fserr(fs, "out of inodes"); 3106294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 31137735Smckusick return (ENOSPC); 3124359Smckusick } 3134359Smckusick 3144651Smckusic /* 3155375Smckusic * Find a cylinder to place a directory. 3165375Smckusic * 3175375Smckusic * The policy implemented by this algorithm is to select from 3185375Smckusic * among those cylinder groups with above the average number of 3195375Smckusic * free inodes, the one with the smallest number of directories. 3204651Smckusic */ 3219163Ssam ino_t 3225965Smckusic dirpref(fs) 3235965Smckusic register struct fs *fs; 3244359Smckusick { 3254651Smckusic int cg, minndir, mincg, avgifree; 3264359Smckusick 3274792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 3284651Smckusic minndir = fs->fs_ipg; 3294359Smckusick mincg = 0; 3304651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 3315322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 3325322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 3334359Smckusick mincg = cg; 3345322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 3354359Smckusick } 3369163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 3374359Smckusick } 3384359Smckusick 3394651Smckusic /* 3409163Ssam * Select the desired position for the next block in a file. The file is 3419163Ssam * logically divided into sections. The first section is composed of the 3429163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 3439163Ssam * 3449163Ssam * If no blocks have been allocated in the first section, the policy is to 3459163Ssam * request a block in the same cylinder group as the inode that describes 3469163Ssam * the file. If no blocks have been allocated in any other section, the 3479163Ssam * policy is to place the section in a cylinder group with a greater than 3489163Ssam * average number of free blocks. An appropriate cylinder group is found 34917696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of 35017696Smckusick * blocks is needed, the sweep begins in the cylinder group following the 35117696Smckusick * cylinder group from which the previous allocation was made. The sweep 35217696Smckusick * continues until a cylinder group with greater than the average number 35317696Smckusick * of free blocks is found. If the allocation is for the first block in an 35417696Smckusick * indirect block, the information on the previous allocation is unavailable; 35517696Smckusick * here a best guess is made based upon the logical block number being 35617696Smckusick * allocated. 3579163Ssam * 3589163Ssam * If a section is already partially allocated, the policy is to 3599163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 3609163Ssam * contiguous blocks and the beginning of the next is physically separated 3619163Ssam * so that the disk head will be in transit between them for at least 3629163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 3639163Ssam * schedule another I/O transfer. 3644651Smckusic */ 3655212Smckusic daddr_t 3669163Ssam blkpref(ip, lbn, indx, bap) 3679163Ssam struct inode *ip; 3689163Ssam daddr_t lbn; 3699163Ssam int indx; 3709163Ssam daddr_t *bap; 3719163Ssam { 3725965Smckusic register struct fs *fs; 37317696Smckusick register int cg; 37417696Smckusick int avgbfree, startcg; 3759163Ssam daddr_t nextblk; 3764651Smckusic 3779163Ssam fs = ip->i_fs; 3789163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 3799163Ssam if (lbn < NDADDR) { 3809163Ssam cg = itog(fs, ip->i_number); 3815322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 3824651Smckusic } 3839163Ssam /* 3849163Ssam * Find a cylinder with greater than average number of 3859163Ssam * unused data blocks. 3869163Ssam */ 38717696Smckusick if (indx == 0 || bap[indx - 1] == 0) 38817696Smckusick startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; 38917696Smckusick else 39017696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1; 39117696Smckusick startcg %= fs->fs_ncg; 3929163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 39317696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++) 3949163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3959163Ssam fs->fs_cgrotor = cg; 3969163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3979163Ssam } 39817696Smckusick for (cg = 0; cg <= startcg; cg++) 3999163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 4009163Ssam fs->fs_cgrotor = cg; 4019163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4029163Ssam } 4039163Ssam return (NULL); 4049163Ssam } 4059163Ssam /* 4069163Ssam * One or more previous blocks have been laid out. If less 4079163Ssam * than fs_maxcontig previous blocks are contiguous, the 4089163Ssam * next block is requested contiguously, otherwise it is 4099163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 4109163Ssam */ 4119163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 4129163Ssam if (indx > fs->fs_maxcontig && 41311638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 4149163Ssam != nextblk) 4159163Ssam return (nextblk); 4169163Ssam if (fs->fs_rotdelay != 0) 4179163Ssam /* 4189163Ssam * Here we convert ms of delay to frags as: 4199163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 4209163Ssam * ((sect/frag) * (ms/sec)) 4219163Ssam * then round up to the next block. 4229163Ssam */ 4239163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 4249163Ssam (NSPF(fs) * 1000), fs->fs_frag); 4259163Ssam return (nextblk); 4264651Smckusic } 4274651Smckusic 4285375Smckusic /* 4295375Smckusic * Implement the cylinder overflow algorithm. 4305375Smckusic * 4315375Smckusic * The policy implemented by this algorithm is: 4325375Smckusic * 1) allocate the block in its requested cylinder group. 4335375Smckusic * 2) quadradically rehash on the cylinder group number. 4345375Smckusic * 3) brute force search for a free block. 4355375Smckusic */ 4365212Smckusic /*VARARGS5*/ 4375212Smckusic u_long 4385965Smckusic hashalloc(ip, cg, pref, size, allocator) 4395965Smckusic struct inode *ip; 4404359Smckusick int cg; 4414359Smckusick long pref; 4424359Smckusick int size; /* size for data blocks, mode for inodes */ 4435212Smckusic u_long (*allocator)(); 4444359Smckusick { 4455965Smckusic register struct fs *fs; 4464359Smckusick long result; 4474359Smckusick int i, icg = cg; 4484359Smckusick 4495965Smckusic fs = ip->i_fs; 4504359Smckusick /* 4514359Smckusick * 1: preferred cylinder group 4524359Smckusick */ 4535965Smckusic result = (*allocator)(ip, cg, pref, size); 4544359Smckusick if (result) 4554359Smckusick return (result); 4564359Smckusick /* 4574359Smckusick * 2: quadratic rehash 4584359Smckusick */ 4594359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 4604359Smckusick cg += i; 4614359Smckusick if (cg >= fs->fs_ncg) 4624359Smckusick cg -= fs->fs_ncg; 4635965Smckusic result = (*allocator)(ip, cg, 0, size); 4644359Smckusick if (result) 4654359Smckusick return (result); 4664359Smckusick } 4674359Smckusick /* 4684359Smckusick * 3: brute force search 46910847Ssam * Note that we start at i == 2, since 0 was checked initially, 47010847Ssam * and 1 is always checked in the quadratic rehash. 4714359Smckusick */ 47210848Smckusick cg = (icg + 2) % fs->fs_ncg; 47310847Ssam for (i = 2; i < fs->fs_ncg; i++) { 4745965Smckusic result = (*allocator)(ip, cg, 0, size); 4754359Smckusick if (result) 4764359Smckusick return (result); 4774359Smckusick cg++; 4784359Smckusick if (cg == fs->fs_ncg) 4794359Smckusick cg = 0; 4804359Smckusick } 4816294Smckusick return (NULL); 4824359Smckusick } 4834359Smckusick 4845375Smckusic /* 4855375Smckusic * Determine whether a fragment can be extended. 4865375Smckusic * 4875375Smckusic * Check to see if the necessary fragments are available, and 4885375Smckusic * if they are, allocate them. 4895375Smckusic */ 4904359Smckusick daddr_t 4915965Smckusic fragextend(ip, cg, bprev, osize, nsize) 4925965Smckusic struct inode *ip; 4934426Smckusic int cg; 4944463Smckusic long bprev; 4954426Smckusic int osize, nsize; 4964426Smckusic { 4975965Smckusic register struct fs *fs; 4984463Smckusic register struct cg *cgp; 49937735Smckusick struct buf *bp; 5004463Smckusic long bno; 5014463Smckusic int frags, bbase; 50237735Smckusick int i, error; 5034426Smckusic 5045965Smckusic fs = ip->i_fs; 50517224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 5066531Smckusick return (NULL); 5075960Smckusic frags = numfrags(fs, nsize); 50817224Smckusick bbase = fragnum(fs, bprev); 50917224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 51030749Skarels /* cannot extend across a block boundary */ 5116294Smckusick return (NULL); 5124463Smckusic } 51337735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 51438776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 51537735Smckusick if (error) { 51637735Smckusick brelse(bp); 51737735Smckusick return (NULL); 51837735Smckusick } 5196531Smckusick cgp = bp->b_un.b_cg; 52037735Smckusick if (!cg_chkmagic(cgp)) { 5215960Smckusic brelse(bp); 5226294Smckusick return (NULL); 5235960Smckusic } 5248105Sroot cgp->cg_time = time.tv_sec; 5255377Smckusic bno = dtogd(fs, bprev); 5265960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 52734143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) { 5285361Smckusic brelse(bp); 5296294Smckusick return (NULL); 5305361Smckusic } 5315361Smckusic /* 5325361Smckusic * the current fragment can be extended 5335361Smckusic * deduct the count on fragment being extended into 5345361Smckusic * increase the count on the remaining fragment (if any) 5355361Smckusic * allocate the extended piece 5365361Smckusic */ 5375361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 53834143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) 5394463Smckusic break; 5405960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 5415361Smckusic if (i != frags) 5425361Smckusic cgp->cg_frsum[i - frags]++; 5435960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 54434143Smckusick clrbit(cg_blksfree(cgp), bno + i); 5455361Smckusic cgp->cg_cs.cs_nffree--; 5465361Smckusic fs->fs_cstotal.cs_nffree--; 5475361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 5484463Smckusic } 5495361Smckusic fs->fs_fmod++; 5505361Smckusic bdwrite(bp); 5515361Smckusic return (bprev); 5524426Smckusic } 5534426Smckusic 5545375Smckusic /* 5555375Smckusic * Determine whether a block can be allocated. 5565375Smckusic * 5575375Smckusic * Check to see if a block of the apprpriate size is available, 5585375Smckusic * and if it is, allocate it. 5595375Smckusic */ 5609163Ssam daddr_t 5615965Smckusic alloccg(ip, cg, bpref, size) 5625965Smckusic struct inode *ip; 5634359Smckusick int cg; 5644359Smckusick daddr_t bpref; 5654359Smckusick int size; 5664359Smckusick { 5675965Smckusic register struct fs *fs; 5684463Smckusic register struct cg *cgp; 56937735Smckusick struct buf *bp; 5704463Smckusic register int i; 57137735Smckusick int error, bno, frags, allocsiz; 5724359Smckusick 5735965Smckusic fs = ip->i_fs; 5745322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 5756294Smckusick return (NULL); 57637735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 57738776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 57837735Smckusick if (error) { 57937735Smckusick brelse(bp); 58037735Smckusick return (NULL); 58137735Smckusick } 5826531Smckusick cgp = bp->b_un.b_cg; 58337735Smckusick if (!cg_chkmagic(cgp) || 58415950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 5855960Smckusic brelse(bp); 5866294Smckusick return (NULL); 5875960Smckusic } 5888105Sroot cgp->cg_time = time.tv_sec; 5895322Smckusic if (size == fs->fs_bsize) { 5905212Smckusic bno = alloccgblk(fs, cgp, bpref); 5914463Smckusic bdwrite(bp); 5924463Smckusic return (bno); 5934463Smckusic } 5944463Smckusic /* 5954463Smckusic * check to see if any fragments are already available 5964463Smckusic * allocsiz is the size which will be allocated, hacking 5974463Smckusic * it down to a smaller size if necessary 5984463Smckusic */ 5995960Smckusic frags = numfrags(fs, size); 6005322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 6014463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 6024463Smckusic break; 6035322Smckusic if (allocsiz == fs->fs_frag) { 6044463Smckusic /* 6054463Smckusic * no fragments were available, so a block will be 6064463Smckusic * allocated, and hacked up 6074463Smckusic */ 6084792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 6094463Smckusic brelse(bp); 6106294Smckusick return (NULL); 6114463Smckusic } 6125212Smckusic bno = alloccgblk(fs, cgp, bpref); 6135377Smckusic bpref = dtogd(fs, bno); 6145322Smckusic for (i = frags; i < fs->fs_frag; i++) 61534143Smckusick setbit(cg_blksfree(cgp), bpref + i); 6165322Smckusic i = fs->fs_frag - frags; 6174792Smckusic cgp->cg_cs.cs_nffree += i; 6184792Smckusic fs->fs_cstotal.cs_nffree += i; 6195322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 6209762Ssam fs->fs_fmod++; 6214463Smckusic cgp->cg_frsum[i]++; 6224463Smckusic bdwrite(bp); 6234463Smckusic return (bno); 6244463Smckusic } 6254651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 62615950Smckusick if (bno < 0) { 62715950Smckusick brelse(bp); 6286294Smckusick return (NULL); 62915950Smckusick } 6304463Smckusic for (i = 0; i < frags; i++) 63134143Smckusick clrbit(cg_blksfree(cgp), bno + i); 6324792Smckusic cgp->cg_cs.cs_nffree -= frags; 6334792Smckusic fs->fs_cstotal.cs_nffree -= frags; 6345322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 6359762Ssam fs->fs_fmod++; 6364463Smckusic cgp->cg_frsum[allocsiz]--; 6374463Smckusic if (frags != allocsiz) 6384463Smckusic cgp->cg_frsum[allocsiz - frags]++; 6394463Smckusic bdwrite(bp); 6404463Smckusic return (cg * fs->fs_fpg + bno); 6414463Smckusic } 6424463Smckusic 6435375Smckusic /* 6445375Smckusic * Allocate a block in a cylinder group. 6455375Smckusic * 6465375Smckusic * This algorithm implements the following policy: 6475375Smckusic * 1) allocate the requested block. 6485375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 6495375Smckusic * 3) allocate the next available block on the block rotor for the 6505375Smckusic * specified cylinder group. 6515375Smckusic * Note that this routine only allocates fs_bsize blocks; these 6525375Smckusic * blocks may be fragmented by the routine that allocates them. 6535375Smckusic */ 6544463Smckusic daddr_t 6555212Smckusic alloccgblk(fs, cgp, bpref) 6565965Smckusic register struct fs *fs; 6574463Smckusic register struct cg *cgp; 6584463Smckusic daddr_t bpref; 6594463Smckusic { 6604651Smckusic daddr_t bno; 6616294Smckusick int cylno, pos, delta; 6624651Smckusic short *cylbp; 6635361Smckusic register int i; 6644463Smckusic 6654651Smckusic if (bpref == 0) { 6664651Smckusic bpref = cgp->cg_rotor; 6675361Smckusic goto norot; 6685361Smckusic } 66917224Smckusick bpref = blknum(fs, bpref); 6705377Smckusic bpref = dtogd(fs, bpref); 6715361Smckusic /* 6725361Smckusic * if the requested block is available, use it 6735361Smckusic */ 67434143Smckusick if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 6755361Smckusic bno = bpref; 6765361Smckusic goto gotit; 6775361Smckusic } 6785361Smckusic /* 6795361Smckusic * check for a block available on the same cylinder 6805361Smckusic */ 6815361Smckusic cylno = cbtocylno(fs, bpref); 68234143Smckusick if (cg_blktot(cgp)[cylno] == 0) 6835375Smckusic goto norot; 6845375Smckusic if (fs->fs_cpc == 0) { 6855375Smckusic /* 6865375Smckusic * block layout info is not available, so just have 6875375Smckusic * to take any block in this cylinder. 6885375Smckusic */ 6895375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 6905375Smckusic goto norot; 6915375Smckusic } 6925375Smckusic /* 6935361Smckusic * check the summary information to see if a block is 6945361Smckusic * available in the requested cylinder starting at the 6959163Ssam * requested rotational position and proceeding around. 6965361Smckusic */ 69734143Smckusick cylbp = cg_blks(fs, cgp, cylno); 6989163Ssam pos = cbtorpos(fs, bpref); 69934143Smckusick for (i = pos; i < fs->fs_nrpos; i++) 7005361Smckusic if (cylbp[i] > 0) 7015361Smckusic break; 70234143Smckusick if (i == fs->fs_nrpos) 7035361Smckusic for (i = 0; i < pos; i++) 7045361Smckusic if (cylbp[i] > 0) 7055361Smckusic break; 7065361Smckusic if (cylbp[i] > 0) { 7074651Smckusic /* 7085361Smckusic * found a rotational position, now find the actual 7095361Smckusic * block. A panic if none is actually there. 7104651Smckusic */ 7115361Smckusic pos = cylno % fs->fs_cpc; 7125361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 71334143Smckusick if (fs_postbl(fs, pos)[i] == -1) { 7146716Smckusick printf("pos = %d, i = %d, fs = %s\n", 7156716Smckusick pos, i, fs->fs_fsmnt); 7165361Smckusic panic("alloccgblk: cyl groups corrupted"); 7176716Smckusick } 71834143Smckusick for (i = fs_postbl(fs, pos)[i];; ) { 71934143Smckusick if (isblock(fs, cg_blksfree(cgp), bno + i)) { 72011638Ssam bno = blkstofrags(fs, (bno + i)); 7215361Smckusic goto gotit; 7225361Smckusic } 72334143Smckusick delta = fs_rotbl(fs)[i]; 72434143Smckusick if (delta <= 0 || 72534143Smckusick delta + i > fragstoblks(fs, fs->fs_fpg)) 7264651Smckusic break; 7276294Smckusick i += delta; 7284651Smckusic } 7296716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 7305361Smckusic panic("alloccgblk: can't find blk in cyl"); 7314359Smckusick } 7325361Smckusic norot: 7335361Smckusic /* 7345361Smckusic * no blocks in the requested cylinder, so take next 7355361Smckusic * available one in this cylinder group. 7365361Smckusic */ 7378628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 7386567Smckusic if (bno < 0) 7396294Smckusick return (NULL); 7404651Smckusic cgp->cg_rotor = bno; 7414359Smckusick gotit: 74234143Smckusick clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno)); 7434792Smckusic cgp->cg_cs.cs_nbfree--; 7444792Smckusic fs->fs_cstotal.cs_nbfree--; 7455322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 7465375Smckusic cylno = cbtocylno(fs, bno); 74734143Smckusick cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 74834143Smckusick cg_blktot(cgp)[cylno]--; 7494359Smckusick fs->fs_fmod++; 7504651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 7514359Smckusick } 75234143Smckusick 7535375Smckusic /* 7545375Smckusic * Determine whether an inode can be allocated. 7555375Smckusic * 7565375Smckusic * Check to see if an inode is available, and if it is, 7575375Smckusic * allocate it using the following policy: 7585375Smckusic * 1) allocate the requested inode. 7595375Smckusic * 2) allocate the next available inode after the requested 7605375Smckusic * inode in the specified cylinder group. 7615375Smckusic */ 7629163Ssam ino_t 7635965Smckusic ialloccg(ip, cg, ipref, mode) 7645965Smckusic struct inode *ip; 7654359Smckusick int cg; 7664359Smckusick daddr_t ipref; 7674359Smckusick int mode; 7684359Smckusick { 7695965Smckusic register struct fs *fs; 7704463Smckusic register struct cg *cgp; 77116784Smckusick struct buf *bp; 77237735Smckusick int error, start, len, loc, map, i; 7734359Smckusick 7745965Smckusic fs = ip->i_fs; 7755322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 7766294Smckusick return (NULL); 77737735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 77838776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 77937735Smckusick if (error) { 78037735Smckusick brelse(bp); 78137735Smckusick return (NULL); 78237735Smckusick } 7836531Smckusick cgp = bp->b_un.b_cg; 78437735Smckusick if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 7855960Smckusic brelse(bp); 7866294Smckusick return (NULL); 7875960Smckusic } 7888105Sroot cgp->cg_time = time.tv_sec; 7894359Smckusick if (ipref) { 7904359Smckusick ipref %= fs->fs_ipg; 79134143Smckusick if (isclr(cg_inosused(cgp), ipref)) 7924359Smckusick goto gotit; 79316784Smckusick } 79416784Smckusick start = cgp->cg_irotor / NBBY; 79516784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 79634143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 79716784Smckusick if (loc == 0) { 79817697Smckusick len = start + 1; 79917697Smckusick start = 0; 80034143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 80117697Smckusick if (loc == 0) { 80217697Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 80317697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 80417697Smckusick panic("ialloccg: map corrupted"); 80517697Smckusick /* NOTREACHED */ 80617697Smckusick } 80716784Smckusick } 80816784Smckusick i = start + len - loc; 80934143Smckusick map = cg_inosused(cgp)[i]; 81016784Smckusick ipref = i * NBBY; 81116784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 81216784Smckusick if ((map & i) == 0) { 8134359Smckusick cgp->cg_irotor = ipref; 8144359Smckusick goto gotit; 8154359Smckusick } 8164359Smckusick } 81716784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 81816784Smckusick panic("ialloccg: block not in map"); 81916784Smckusick /* NOTREACHED */ 8204359Smckusick gotit: 82134143Smckusick setbit(cg_inosused(cgp), ipref); 8224792Smckusic cgp->cg_cs.cs_nifree--; 8234792Smckusic fs->fs_cstotal.cs_nifree--; 8245322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 8254359Smckusick fs->fs_fmod++; 8264359Smckusick if ((mode & IFMT) == IFDIR) { 8274792Smckusic cgp->cg_cs.cs_ndir++; 8284792Smckusic fs->fs_cstotal.cs_ndir++; 8295322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 8304359Smckusick } 8314359Smckusick bdwrite(bp); 8324359Smckusick return (cg * fs->fs_ipg + ipref); 8334359Smckusick } 8344359Smckusick 8355375Smckusic /* 8365375Smckusic * Free a block or fragment. 8375375Smckusic * 8385375Smckusic * The specified block or fragment is placed back in the 8395375Smckusic * free map. If a fragment is deallocated, a possible 8405375Smckusic * block reassembly is checked. 8415375Smckusic */ 84231402Smckusick blkfree(ip, bno, size) 8435965Smckusic register struct inode *ip; 8444359Smckusick daddr_t bno; 8455212Smckusic off_t size; 8464359Smckusick { 8474359Smckusick register struct fs *fs; 8484359Smckusick register struct cg *cgp; 84937735Smckusick struct buf *bp; 85037735Smckusick int error, cg, blk, frags, bbase; 8514463Smckusic register int i; 8524359Smckusick 8535965Smckusic fs = ip->i_fs; 8546716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 8556716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 8566716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 85731402Smckusick panic("blkfree: bad size"); 8586716Smckusick } 8595377Smckusic cg = dtog(fs, bno); 8606567Smckusic if (badblock(fs, bno)) { 8616567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 8624359Smckusick return; 8636567Smckusic } 86437735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 86538776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 86637735Smckusick if (error) { 86737735Smckusick brelse(bp); 86837735Smckusick return; 86937735Smckusick } 8706531Smckusick cgp = bp->b_un.b_cg; 87137735Smckusick if (!cg_chkmagic(cgp)) { 8725960Smckusic brelse(bp); 8734359Smckusick return; 8745960Smckusic } 8758105Sroot cgp->cg_time = time.tv_sec; 8765377Smckusic bno = dtogd(fs, bno); 8775322Smckusic if (size == fs->fs_bsize) { 87834143Smckusick if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) { 8796716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 8806716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 88131402Smckusick panic("blkfree: freeing free block"); 8826567Smckusic } 88334143Smckusick setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno)); 8844792Smckusic cgp->cg_cs.cs_nbfree++; 8854792Smckusic fs->fs_cstotal.cs_nbfree++; 8865322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 8875375Smckusic i = cbtocylno(fs, bno); 88834143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; 88934143Smckusick cg_blktot(cgp)[i]++; 8904426Smckusic } else { 89117224Smckusick bbase = bno - fragnum(fs, bno); 8924463Smckusic /* 8934463Smckusic * decrement the counts associated with the old frags 8944463Smckusic */ 89534143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 8965322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 8974463Smckusic /* 8984463Smckusic * deallocate the fragment 8994463Smckusic */ 9005960Smckusic frags = numfrags(fs, size); 9014463Smckusic for (i = 0; i < frags; i++) { 90234143Smckusick if (isset(cg_blksfree(cgp), bno + i)) { 9036716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9046716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 90531402Smckusick panic("blkfree: freeing free frag"); 9066716Smckusick } 90734143Smckusick setbit(cg_blksfree(cgp), bno + i); 9084426Smckusic } 9096294Smckusick cgp->cg_cs.cs_nffree += i; 9106294Smckusick fs->fs_cstotal.cs_nffree += i; 9116294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 9124463Smckusic /* 9134463Smckusic * add back in counts associated with the new frags 9144463Smckusic */ 91534143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 9165322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 9174463Smckusic /* 9184463Smckusic * if a complete block has been reassembled, account for it 9194463Smckusic */ 92034476Smckusick if (isblock(fs, cg_blksfree(cgp), 92134476Smckusick (daddr_t)fragstoblks(fs, bbase))) { 9225322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 9235322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 9245322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 9254792Smckusic cgp->cg_cs.cs_nbfree++; 9264792Smckusic fs->fs_cstotal.cs_nbfree++; 9275322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9285375Smckusic i = cbtocylno(fs, bbase); 92934143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 93034143Smckusick cg_blktot(cgp)[i]++; 9314426Smckusic } 9324426Smckusic } 9334359Smckusick fs->fs_fmod++; 9344359Smckusick bdwrite(bp); 9354359Smckusick } 9364359Smckusick 9375375Smckusic /* 9385375Smckusic * Free an inode. 9395375Smckusic * 9405375Smckusic * The specified inode is placed back in the free map. 9415375Smckusic */ 9425965Smckusic ifree(ip, ino, mode) 9435965Smckusic struct inode *ip; 9444359Smckusick ino_t ino; 9454359Smckusick int mode; 9464359Smckusick { 9474359Smckusick register struct fs *fs; 9484359Smckusick register struct cg *cgp; 94937735Smckusick struct buf *bp; 95037735Smckusick int error, cg; 9514359Smckusick 9525965Smckusic fs = ip->i_fs; 9536716Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 9546716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9556716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9564359Smckusick panic("ifree: range"); 9576716Smckusick } 9585377Smckusic cg = itog(fs, ino); 95937735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 96038776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 96137735Smckusick if (error) { 96237735Smckusick brelse(bp); 96337735Smckusick return; 96437735Smckusick } 9656531Smckusick cgp = bp->b_un.b_cg; 96637735Smckusick if (!cg_chkmagic(cgp)) { 9675960Smckusic brelse(bp); 9684359Smckusick return; 9695960Smckusic } 9708105Sroot cgp->cg_time = time.tv_sec; 9714359Smckusick ino %= fs->fs_ipg; 97234143Smckusick if (isclr(cg_inosused(cgp), ino)) { 9736716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9746716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9754359Smckusick panic("ifree: freeing free inode"); 9766716Smckusick } 97734143Smckusick clrbit(cg_inosused(cgp), ino); 97816784Smckusick if (ino < cgp->cg_irotor) 97916784Smckusick cgp->cg_irotor = ino; 9804792Smckusic cgp->cg_cs.cs_nifree++; 9814792Smckusic fs->fs_cstotal.cs_nifree++; 9825322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 9834359Smckusick if ((mode & IFMT) == IFDIR) { 9844792Smckusic cgp->cg_cs.cs_ndir--; 9854792Smckusic fs->fs_cstotal.cs_ndir--; 9865322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 9874359Smckusick } 9884359Smckusick fs->fs_fmod++; 9894359Smckusick bdwrite(bp); 9904359Smckusick } 9914359Smckusick 9924463Smckusic /* 9935375Smckusic * Find a block of the specified size in the specified cylinder group. 9945375Smckusic * 9954651Smckusic * It is a panic if a request is made to find a block if none are 9964651Smckusic * available. 9974651Smckusic */ 9984651Smckusic daddr_t 9994651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 10004651Smckusic register struct fs *fs; 10014651Smckusic register struct cg *cgp; 10024651Smckusic daddr_t bpref; 10034651Smckusic int allocsiz; 10044651Smckusic { 10054651Smckusic daddr_t bno; 10064651Smckusic int start, len, loc, i; 10074651Smckusic int blk, field, subfield, pos; 10084651Smckusic 10094651Smckusic /* 10104651Smckusic * find the fragment by searching through the free block 10114651Smckusic * map for an appropriate bit pattern 10124651Smckusic */ 10134651Smckusic if (bpref) 10145377Smckusic start = dtogd(fs, bpref) / NBBY; 10154651Smckusic else 10164651Smckusic start = cgp->cg_frotor / NBBY; 10175398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 101834476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start], 101934476Smckusick (u_char *)fragtbl[fs->fs_frag], 102034476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 10214651Smckusic if (loc == 0) { 10226531Smckusick len = start + 1; 10236531Smckusick start = 0; 102434476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0], 102534476Smckusick (u_char *)fragtbl[fs->fs_frag], 102634476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 102716784Smckusick if (loc == 0) { 102816784Smckusick printf("start = %d, len = %d, fs = %s\n", 102916784Smckusick start, len, fs->fs_fsmnt); 103016784Smckusick panic("alloccg: map corrupted"); 103117697Smckusick /* NOTREACHED */ 103216784Smckusick } 10334651Smckusic } 10344651Smckusic bno = (start + len - loc) * NBBY; 10354651Smckusic cgp->cg_frotor = bno; 10364651Smckusic /* 10374651Smckusic * found the byte in the map 10384651Smckusic * sift through the bits to find the selected frag 10394651Smckusic */ 10406294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 104134143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bno); 10424651Smckusic blk <<= 1; 10434651Smckusic field = around[allocsiz]; 10444651Smckusic subfield = inside[allocsiz]; 10455322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 10466294Smckusick if ((blk & field) == subfield) 10476294Smckusick return (bno + pos); 10484651Smckusic field <<= 1; 10494651Smckusic subfield <<= 1; 10504651Smckusic } 10514651Smckusic } 10526716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 10534651Smckusic panic("alloccg: block not in map"); 10546531Smckusick return (-1); 10554651Smckusic } 10564651Smckusic 10574651Smckusic /* 10585375Smckusic * Fserr prints the name of a file system with an error diagnostic. 10595375Smckusic * 10605375Smckusic * The form of the error message is: 10614359Smckusick * fs: error message 10624359Smckusick */ 10634359Smckusick fserr(fs, cp) 10644359Smckusick struct fs *fs; 10654359Smckusick char *cp; 10664359Smckusick { 10674359Smckusick 106824839Seric log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp); 10694359Smckusick } 1070