123394Smckusick /* 223394Smckusick * Copyright (c) 1982 Regents of the University of California. 323394Smckusick * All rights reserved. The Berkeley software License Agreement 423394Smckusick * specifies the terms and conditions for redistribution. 523394Smckusick * 6*25256Smckusick * @(#)lfs_alloc.c 6.17 (Berkeley) 10/23/85 723394Smckusick */ 84359Smckusick 917097Sbloom #include "param.h" 1017097Sbloom #include "systm.h" 1117097Sbloom #include "mount.h" 1217097Sbloom #include "fs.h" 1317097Sbloom #include "buf.h" 1417097Sbloom #include "inode.h" 1517097Sbloom #include "dir.h" 1617097Sbloom #include "user.h" 1717097Sbloom #include "quota.h" 1817097Sbloom #include "kernel.h" 1918307Sralph #include "syslog.h" 204359Smckusick 215212Smckusic extern u_long hashalloc(); 229163Ssam extern ino_t ialloccg(); 239163Ssam extern daddr_t alloccg(); 244651Smckusic extern daddr_t alloccgblk(); 254651Smckusic extern daddr_t fragextend(); 264651Smckusic extern daddr_t blkpref(); 274651Smckusic extern daddr_t mapsearch(); 284607Smckusic extern int inside[], around[]; 295322Smckusic extern unsigned char *fragtbl[]; 304359Smckusick 315375Smckusic /* 325375Smckusic * Allocate a block in the file system. 335375Smckusic * 345375Smckusic * The size of the requested block is given, which must be some 355375Smckusic * multiple of fs_fsize and <= fs_bsize. 365375Smckusic * A preference may be optionally specified. If a preference is given 375375Smckusic * the following hierarchy is used to allocate a block: 385375Smckusic * 1) allocate the requested block. 395375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 405375Smckusic * 3) allocate a block in the same cylinder group. 415375Smckusic * 4) quadradically rehash into other cylinder groups, until an 425375Smckusic * available block is located. 435375Smckusic * If no block preference is given the following heirarchy is used 445375Smckusic * to allocate a block: 455375Smckusic * 1) allocate a block in the cylinder group that contains the 465375Smckusic * inode for the file. 475375Smckusic * 2) quadradically rehash into other cylinder groups, until an 485375Smckusic * available block is located. 495375Smckusic */ 504359Smckusick struct buf * 515965Smckusic alloc(ip, bpref, size) 524463Smckusic register struct inode *ip; 534359Smckusick daddr_t bpref; 544359Smckusick int size; 554359Smckusick { 564359Smckusick daddr_t bno; 574359Smckusick register struct fs *fs; 584463Smckusic register struct buf *bp; 594359Smckusick int cg; 604359Smckusick 615965Smckusic fs = ip->i_fs; 626716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 636716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 646716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 654463Smckusic panic("alloc: bad size"); 666716Smckusick } 675322Smckusic if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 684359Smckusick goto nospace; 6911638Ssam if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 704792Smckusic goto nospace; 717650Ssam #ifdef QUOTA 7212643Ssam u.u_error = chkdq(ip, (long)btodb(size), 0); 7312643Ssam if (u.u_error) 7412643Ssam return (NULL); 757483Skre #endif 764948Smckusic if (bpref >= fs->fs_size) 774948Smckusic bpref = 0; 784359Smckusick if (bpref == 0) 795377Smckusic cg = itog(fs, ip->i_number); 804359Smckusick else 815377Smckusic cg = dtog(fs, bpref); 829163Ssam bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, 839163Ssam (u_long (*)())alloccg); 846567Smckusic if (bno <= 0) 854359Smckusick goto nospace; 8612643Ssam ip->i_blocks += btodb(size); 8712643Ssam ip->i_flag |= IUPD|ICHG; 885965Smckusic bp = getblk(ip->i_dev, fsbtodb(fs, bno), size); 894359Smckusick clrbuf(bp); 904359Smckusick return (bp); 914359Smckusick nospace: 924359Smckusick fserr(fs, "file system full"); 934359Smckusick uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 944359Smckusick u.u_error = ENOSPC; 954359Smckusick return (NULL); 964359Smckusick } 974359Smckusick 985375Smckusic /* 995375Smckusic * Reallocate a fragment to a bigger size 1005375Smckusic * 1015375Smckusic * The number and size of the old block is given, and a preference 1025375Smckusic * and new size is also specified. The allocator attempts to extend 1035375Smckusic * the original block. Failing that, the regular block allocator is 1045375Smckusic * invoked to get an appropriate block. 1055375Smckusic */ 1064426Smckusic struct buf * 1075965Smckusic realloccg(ip, bprev, bpref, osize, nsize) 1085965Smckusic register struct inode *ip; 1094651Smckusic daddr_t bprev, bpref; 1104426Smckusic int osize, nsize; 1114426Smckusic { 1124426Smckusic daddr_t bno; 1134426Smckusic register struct fs *fs; 1144463Smckusic register struct buf *bp, *obp; 11524698Smckusick int cg, request; 1164426Smckusic 1175965Smckusic fs = ip->i_fs; 1185960Smckusic if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 1196716Smckusick (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 1206716Smckusick printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 1216716Smckusick ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 1224463Smckusic panic("realloccg: bad size"); 1236716Smckusick } 12411638Ssam if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 1254792Smckusic goto nospace; 1266716Smckusick if (bprev == 0) { 1276716Smckusick printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 1286716Smckusick ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 1294463Smckusic panic("realloccg: bad bprev"); 1306716Smckusick } 1317650Ssam #ifdef QUOTA 13212643Ssam u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0); 13312643Ssam if (u.u_error) 13412643Ssam return (NULL); 1357483Skre #endif 1366294Smckusick cg = dtog(fs, bprev); 1375965Smckusic bno = fragextend(ip, cg, (long)bprev, osize, nsize); 1384463Smckusic if (bno != 0) { 1397187Sroot do { 1407187Sroot bp = bread(ip->i_dev, fsbtodb(fs, bno), osize); 1417187Sroot if (bp->b_flags & B_ERROR) { 1427187Sroot brelse(bp); 1437187Sroot return (NULL); 1447187Sroot } 1457187Sroot } while (brealloc(bp, nsize) == 0); 1467187Sroot bp->b_flags |= B_DONE; 1479163Ssam bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 14812643Ssam ip->i_blocks += btodb(nsize - osize); 14912643Ssam ip->i_flag |= IUPD|ICHG; 1504463Smckusic return (bp); 1514463Smckusic } 1524948Smckusic if (bpref >= fs->fs_size) 1534948Smckusic bpref = 0; 154*25256Smckusick switch (fs->fs_optim) { 155*25256Smckusick case FS_OPTSPACE: 156*25256Smckusick /* 157*25256Smckusick * Allocate an exact sized fragment. Although this makes 158*25256Smckusick * best use of space, we will waste time relocating it if 159*25256Smckusick * the file continues to grow. If the fragmentation is 160*25256Smckusick * less than half of the minimum free reserve, we choose 161*25256Smckusick * to begin optimizing for time. 162*25256Smckusick */ 16324698Smckusick request = nsize; 164*25256Smckusick if (fs->fs_minfree < 5 || 165*25256Smckusick fs->fs_cstotal.cs_nffree > 166*25256Smckusick fs->fs_dsize * fs->fs_minfree / (2 * 100)) 167*25256Smckusick break; 168*25256Smckusick log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 169*25256Smckusick fs->fs_fsmnt); 170*25256Smckusick fs->fs_optim = FS_OPTTIME; 171*25256Smckusick break; 172*25256Smckusick case FS_OPTTIME: 173*25256Smckusick /* 174*25256Smckusick * At this point we have discovered a file that is trying 175*25256Smckusick * to grow a small fragment to a larger fragment. To save 176*25256Smckusick * time, we allocate a full sized block, then free the 177*25256Smckusick * unused portion. If the file continues to grow, the 178*25256Smckusick * `fragextend' call above will be able to grow it in place 179*25256Smckusick * without further copying. If aberrant programs cause 180*25256Smckusick * disk fragmentation to grow within 2% of the free reserve, 181*25256Smckusick * we choose to begin optimizing for space. 182*25256Smckusick */ 18324698Smckusick request = fs->fs_bsize; 184*25256Smckusick if (fs->fs_cstotal.cs_nffree < 185*25256Smckusick fs->fs_dsize * (fs->fs_minfree - 2) / 100) 186*25256Smckusick break; 187*25256Smckusick log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 188*25256Smckusick fs->fs_fsmnt); 189*25256Smckusick fs->fs_optim = FS_OPTSPACE; 190*25256Smckusick break; 191*25256Smckusick default: 192*25256Smckusick printf("dev = 0x%x, optim = %d, fs = %s\n", 193*25256Smckusick ip->i_dev, fs->fs_optim, fs->fs_fsmnt); 194*25256Smckusick panic("realloccg: bad optim"); 195*25256Smckusick /* NOTREACHED */ 196*25256Smckusick } 19724698Smckusick bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request, 1989163Ssam (u_long (*)())alloccg); 1996567Smckusic if (bno > 0) { 2005965Smckusic obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize); 2015960Smckusic if (obp->b_flags & B_ERROR) { 2025960Smckusic brelse(obp); 2036294Smckusick return (NULL); 2045960Smckusic } 20517658Smckusick bp = getblk(ip->i_dev, fsbtodb(fs, bno), nsize); 20617658Smckusick bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize); 20717658Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 20817658Smckusick if (obp->b_flags & B_DELWRI) { 20917658Smckusick obp->b_flags &= ~B_DELWRI; 21017658Smckusick u.u_ru.ru_oublock--; /* delete charge */ 21117658Smckusick } 2124463Smckusic brelse(obp); 2139163Ssam free(ip, bprev, (off_t)osize); 21424698Smckusick if (nsize < request) 21517709Smckusick free(ip, bno + numfrags(fs, nsize), 21624698Smckusick (off_t)(request - nsize)); 21712643Ssam ip->i_blocks += btodb(nsize - osize); 21812643Ssam ip->i_flag |= IUPD|ICHG; 2196294Smckusick return (bp); 2204463Smckusic } 2214792Smckusic nospace: 2224463Smckusic /* 2234463Smckusic * no space available 2244463Smckusic */ 2254426Smckusic fserr(fs, "file system full"); 2264426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 2274426Smckusic u.u_error = ENOSPC; 2284426Smckusic return (NULL); 2294426Smckusic } 2304426Smckusic 2315375Smckusic /* 2325375Smckusic * Allocate an inode in the file system. 2335375Smckusic * 2345375Smckusic * A preference may be optionally specified. If a preference is given 2355375Smckusic * the following hierarchy is used to allocate an inode: 2365375Smckusic * 1) allocate the requested inode. 2375375Smckusic * 2) allocate an inode in the same cylinder group. 2385375Smckusic * 3) quadradically rehash into other cylinder groups, until an 2395375Smckusic * available inode is located. 2405375Smckusic * If no inode preference is given the following heirarchy is used 2415375Smckusic * to allocate an inode: 2425375Smckusic * 1) allocate an inode in cylinder group 0. 2435375Smckusic * 2) quadradically rehash into other cylinder groups, until an 2445375Smckusic * available inode is located. 2455375Smckusic */ 2464359Smckusick struct inode * 2475965Smckusic ialloc(pip, ipref, mode) 2485965Smckusic register struct inode *pip; 2494359Smckusick ino_t ipref; 2504359Smckusick int mode; 2514359Smckusick { 2525212Smckusic ino_t ino; 2534359Smckusick register struct fs *fs; 2544359Smckusick register struct inode *ip; 2554359Smckusick int cg; 2564359Smckusick 2575965Smckusic fs = pip->i_fs; 2584792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 2594359Smckusick goto noinodes; 2607650Ssam #ifdef QUOTA 26112643Ssam u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0); 26212643Ssam if (u.u_error) 26312643Ssam return (NULL); 2647483Skre #endif 2654948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 2664948Smckusic ipref = 0; 2675377Smckusic cg = itog(fs, ipref); 2685965Smckusic ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); 2694359Smckusick if (ino == 0) 2704359Smckusick goto noinodes; 2715965Smckusic ip = iget(pip->i_dev, pip->i_fs, ino); 2724359Smckusick if (ip == NULL) { 27315120Skarels ifree(pip, ino, 0); 2744359Smckusick return (NULL); 2754359Smckusick } 2766716Smckusick if (ip->i_mode) { 2776716Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 2786716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 2794359Smckusick panic("ialloc: dup alloc"); 2806716Smckusick } 28112643Ssam if (ip->i_blocks) { /* XXX */ 28212643Ssam printf("free inode %s/%d had %d blocks\n", 28312643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 28412643Ssam ip->i_blocks = 0; 28512643Ssam } 2864359Smckusick return (ip); 2874359Smckusick noinodes: 2884359Smckusick fserr(fs, "out of inodes"); 2896294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 2904359Smckusick u.u_error = ENOSPC; 2914359Smckusick return (NULL); 2924359Smckusick } 2934359Smckusick 2944651Smckusic /* 2955375Smckusic * Find a cylinder to place a directory. 2965375Smckusic * 2975375Smckusic * The policy implemented by this algorithm is to select from 2985375Smckusic * among those cylinder groups with above the average number of 2995375Smckusic * free inodes, the one with the smallest number of directories. 3004651Smckusic */ 3019163Ssam ino_t 3025965Smckusic dirpref(fs) 3035965Smckusic register struct fs *fs; 3044359Smckusick { 3054651Smckusic int cg, minndir, mincg, avgifree; 3064359Smckusick 3074792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 3084651Smckusic minndir = fs->fs_ipg; 3094359Smckusick mincg = 0; 3104651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 3115322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 3125322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 3134359Smckusick mincg = cg; 3145322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 3154359Smckusick } 3169163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 3174359Smckusick } 3184359Smckusick 3194651Smckusic /* 3209163Ssam * Select the desired position for the next block in a file. The file is 3219163Ssam * logically divided into sections. The first section is composed of the 3229163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 3239163Ssam * 3249163Ssam * If no blocks have been allocated in the first section, the policy is to 3259163Ssam * request a block in the same cylinder group as the inode that describes 3269163Ssam * the file. If no blocks have been allocated in any other section, the 3279163Ssam * policy is to place the section in a cylinder group with a greater than 3289163Ssam * average number of free blocks. An appropriate cylinder group is found 32917696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of 33017696Smckusick * blocks is needed, the sweep begins in the cylinder group following the 33117696Smckusick * cylinder group from which the previous allocation was made. The sweep 33217696Smckusick * continues until a cylinder group with greater than the average number 33317696Smckusick * of free blocks is found. If the allocation is for the first block in an 33417696Smckusick * indirect block, the information on the previous allocation is unavailable; 33517696Smckusick * here a best guess is made based upon the logical block number being 33617696Smckusick * allocated. 3379163Ssam * 3389163Ssam * If a section is already partially allocated, the policy is to 3399163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 3409163Ssam * contiguous blocks and the beginning of the next is physically separated 3419163Ssam * so that the disk head will be in transit between them for at least 3429163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 3439163Ssam * schedule another I/O transfer. 3444651Smckusic */ 3455212Smckusic daddr_t 3469163Ssam blkpref(ip, lbn, indx, bap) 3479163Ssam struct inode *ip; 3489163Ssam daddr_t lbn; 3499163Ssam int indx; 3509163Ssam daddr_t *bap; 3519163Ssam { 3525965Smckusic register struct fs *fs; 35317696Smckusick register int cg; 35417696Smckusick int avgbfree, startcg; 3559163Ssam daddr_t nextblk; 3564651Smckusic 3579163Ssam fs = ip->i_fs; 3589163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 3599163Ssam if (lbn < NDADDR) { 3609163Ssam cg = itog(fs, ip->i_number); 3615322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 3624651Smckusic } 3639163Ssam /* 3649163Ssam * Find a cylinder with greater than average number of 3659163Ssam * unused data blocks. 3669163Ssam */ 36717696Smckusick if (indx == 0 || bap[indx - 1] == 0) 36817696Smckusick startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; 36917696Smckusick else 37017696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1; 37117696Smckusick startcg %= fs->fs_ncg; 3729163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 37317696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++) 3749163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3759163Ssam fs->fs_cgrotor = cg; 3769163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3779163Ssam } 37817696Smckusick for (cg = 0; cg <= startcg; cg++) 3799163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3809163Ssam fs->fs_cgrotor = cg; 3819163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3829163Ssam } 3839163Ssam return (NULL); 3849163Ssam } 3859163Ssam /* 3869163Ssam * One or more previous blocks have been laid out. If less 3879163Ssam * than fs_maxcontig previous blocks are contiguous, the 3889163Ssam * next block is requested contiguously, otherwise it is 3899163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 3909163Ssam */ 3919163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 3929163Ssam if (indx > fs->fs_maxcontig && 39311638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 3949163Ssam != nextblk) 3959163Ssam return (nextblk); 3969163Ssam if (fs->fs_rotdelay != 0) 3979163Ssam /* 3989163Ssam * Here we convert ms of delay to frags as: 3999163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 4009163Ssam * ((sect/frag) * (ms/sec)) 4019163Ssam * then round up to the next block. 4029163Ssam */ 4039163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 4049163Ssam (NSPF(fs) * 1000), fs->fs_frag); 4059163Ssam return (nextblk); 4064651Smckusic } 4074651Smckusic 4085375Smckusic /* 4095375Smckusic * Implement the cylinder overflow algorithm. 4105375Smckusic * 4115375Smckusic * The policy implemented by this algorithm is: 4125375Smckusic * 1) allocate the block in its requested cylinder group. 4135375Smckusic * 2) quadradically rehash on the cylinder group number. 4145375Smckusic * 3) brute force search for a free block. 4155375Smckusic */ 4165212Smckusic /*VARARGS5*/ 4175212Smckusic u_long 4185965Smckusic hashalloc(ip, cg, pref, size, allocator) 4195965Smckusic struct inode *ip; 4204359Smckusick int cg; 4214359Smckusick long pref; 4224359Smckusick int size; /* size for data blocks, mode for inodes */ 4235212Smckusic u_long (*allocator)(); 4244359Smckusick { 4255965Smckusic register struct fs *fs; 4264359Smckusick long result; 4274359Smckusick int i, icg = cg; 4284359Smckusick 4295965Smckusic fs = ip->i_fs; 4304359Smckusick /* 4314359Smckusick * 1: preferred cylinder group 4324359Smckusick */ 4335965Smckusic result = (*allocator)(ip, cg, pref, size); 4344359Smckusick if (result) 4354359Smckusick return (result); 4364359Smckusick /* 4374359Smckusick * 2: quadratic rehash 4384359Smckusick */ 4394359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 4404359Smckusick cg += i; 4414359Smckusick if (cg >= fs->fs_ncg) 4424359Smckusick cg -= fs->fs_ncg; 4435965Smckusic result = (*allocator)(ip, cg, 0, size); 4444359Smckusick if (result) 4454359Smckusick return (result); 4464359Smckusick } 4474359Smckusick /* 4484359Smckusick * 3: brute force search 44910847Ssam * Note that we start at i == 2, since 0 was checked initially, 45010847Ssam * and 1 is always checked in the quadratic rehash. 4514359Smckusick */ 45210848Smckusick cg = (icg + 2) % fs->fs_ncg; 45310847Ssam for (i = 2; i < fs->fs_ncg; i++) { 4545965Smckusic result = (*allocator)(ip, cg, 0, size); 4554359Smckusick if (result) 4564359Smckusick return (result); 4574359Smckusick cg++; 4584359Smckusick if (cg == fs->fs_ncg) 4594359Smckusick cg = 0; 4604359Smckusick } 4616294Smckusick return (NULL); 4624359Smckusick } 4634359Smckusick 4645375Smckusic /* 4655375Smckusic * Determine whether a fragment can be extended. 4665375Smckusic * 4675375Smckusic * Check to see if the necessary fragments are available, and 4685375Smckusic * if they are, allocate them. 4695375Smckusic */ 4704359Smckusick daddr_t 4715965Smckusic fragextend(ip, cg, bprev, osize, nsize) 4725965Smckusic struct inode *ip; 4734426Smckusic int cg; 4744463Smckusic long bprev; 4754426Smckusic int osize, nsize; 4764426Smckusic { 4775965Smckusic register struct fs *fs; 4784463Smckusic register struct buf *bp; 4794463Smckusic register struct cg *cgp; 4804463Smckusic long bno; 4814463Smckusic int frags, bbase; 4824426Smckusic int i; 4834426Smckusic 4845965Smckusic fs = ip->i_fs; 48517224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 4866531Smckusick return (NULL); 4875960Smckusic frags = numfrags(fs, nsize); 48817224Smckusick bbase = fragnum(fs, bprev); 48917224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 4904463Smckusic /* cannot extend across a block boundry */ 4916294Smckusick return (NULL); 4924463Smckusic } 49310278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 4946531Smckusick cgp = bp->b_un.b_cg; 4956531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 4965960Smckusic brelse(bp); 4976294Smckusick return (NULL); 4985960Smckusic } 4998105Sroot cgp->cg_time = time.tv_sec; 5005377Smckusic bno = dtogd(fs, bprev); 5015960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 5025361Smckusic if (isclr(cgp->cg_free, bno + i)) { 5035361Smckusic brelse(bp); 5046294Smckusick return (NULL); 5055361Smckusic } 5065361Smckusic /* 5075361Smckusic * the current fragment can be extended 5085361Smckusic * deduct the count on fragment being extended into 5095361Smckusic * increase the count on the remaining fragment (if any) 5105361Smckusic * allocate the extended piece 5115361Smckusic */ 5125361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 5134463Smckusic if (isclr(cgp->cg_free, bno + i)) 5144463Smckusic break; 5155960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 5165361Smckusic if (i != frags) 5175361Smckusic cgp->cg_frsum[i - frags]++; 5185960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 5195361Smckusic clrbit(cgp->cg_free, bno + i); 5205361Smckusic cgp->cg_cs.cs_nffree--; 5215361Smckusic fs->fs_cstotal.cs_nffree--; 5225361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 5234463Smckusic } 5245361Smckusic fs->fs_fmod++; 5255361Smckusic bdwrite(bp); 5265361Smckusic return (bprev); 5274426Smckusic } 5284426Smckusic 5295375Smckusic /* 5305375Smckusic * Determine whether a block can be allocated. 5315375Smckusic * 5325375Smckusic * Check to see if a block of the apprpriate size is available, 5335375Smckusic * and if it is, allocate it. 5345375Smckusic */ 5359163Ssam daddr_t 5365965Smckusic alloccg(ip, cg, bpref, size) 5375965Smckusic struct inode *ip; 5384359Smckusick int cg; 5394359Smckusick daddr_t bpref; 5404359Smckusick int size; 5414359Smckusick { 5425965Smckusic register struct fs *fs; 5434463Smckusic register struct buf *bp; 5444463Smckusic register struct cg *cgp; 5454463Smckusic int bno, frags; 5464463Smckusic int allocsiz; 5474463Smckusic register int i; 5484359Smckusick 5495965Smckusic fs = ip->i_fs; 5505322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 5516294Smckusick return (NULL); 55210278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 5536531Smckusick cgp = bp->b_un.b_cg; 55415950Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC || 55515950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 5565960Smckusic brelse(bp); 5576294Smckusick return (NULL); 5585960Smckusic } 5598105Sroot cgp->cg_time = time.tv_sec; 5605322Smckusic if (size == fs->fs_bsize) { 5615212Smckusic bno = alloccgblk(fs, cgp, bpref); 5624463Smckusic bdwrite(bp); 5634463Smckusic return (bno); 5644463Smckusic } 5654463Smckusic /* 5664463Smckusic * check to see if any fragments are already available 5674463Smckusic * allocsiz is the size which will be allocated, hacking 5684463Smckusic * it down to a smaller size if necessary 5694463Smckusic */ 5705960Smckusic frags = numfrags(fs, size); 5715322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 5724463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 5734463Smckusic break; 5745322Smckusic if (allocsiz == fs->fs_frag) { 5754463Smckusic /* 5764463Smckusic * no fragments were available, so a block will be 5774463Smckusic * allocated, and hacked up 5784463Smckusic */ 5794792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 5804463Smckusic brelse(bp); 5816294Smckusick return (NULL); 5824463Smckusic } 5835212Smckusic bno = alloccgblk(fs, cgp, bpref); 5845377Smckusic bpref = dtogd(fs, bno); 5855322Smckusic for (i = frags; i < fs->fs_frag; i++) 5864463Smckusic setbit(cgp->cg_free, bpref + i); 5875322Smckusic i = fs->fs_frag - frags; 5884792Smckusic cgp->cg_cs.cs_nffree += i; 5894792Smckusic fs->fs_cstotal.cs_nffree += i; 5905322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 5919762Ssam fs->fs_fmod++; 5924463Smckusic cgp->cg_frsum[i]++; 5934463Smckusic bdwrite(bp); 5944463Smckusic return (bno); 5954463Smckusic } 5964651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 59715950Smckusick if (bno < 0) { 59815950Smckusick brelse(bp); 5996294Smckusick return (NULL); 60015950Smckusick } 6014463Smckusic for (i = 0; i < frags; i++) 6024463Smckusic clrbit(cgp->cg_free, bno + i); 6034792Smckusic cgp->cg_cs.cs_nffree -= frags; 6044792Smckusic fs->fs_cstotal.cs_nffree -= frags; 6055322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 6069762Ssam fs->fs_fmod++; 6074463Smckusic cgp->cg_frsum[allocsiz]--; 6084463Smckusic if (frags != allocsiz) 6094463Smckusic cgp->cg_frsum[allocsiz - frags]++; 6104463Smckusic bdwrite(bp); 6114463Smckusic return (cg * fs->fs_fpg + bno); 6124463Smckusic } 6134463Smckusic 6145375Smckusic /* 6155375Smckusic * Allocate a block in a cylinder group. 6165375Smckusic * 6175375Smckusic * This algorithm implements the following policy: 6185375Smckusic * 1) allocate the requested block. 6195375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 6205375Smckusic * 3) allocate the next available block on the block rotor for the 6215375Smckusic * specified cylinder group. 6225375Smckusic * Note that this routine only allocates fs_bsize blocks; these 6235375Smckusic * blocks may be fragmented by the routine that allocates them. 6245375Smckusic */ 6254463Smckusic daddr_t 6265212Smckusic alloccgblk(fs, cgp, bpref) 6275965Smckusic register struct fs *fs; 6284463Smckusic register struct cg *cgp; 6294463Smckusic daddr_t bpref; 6304463Smckusic { 6314651Smckusic daddr_t bno; 6326294Smckusick int cylno, pos, delta; 6334651Smckusic short *cylbp; 6345361Smckusic register int i; 6354463Smckusic 6364651Smckusic if (bpref == 0) { 6374651Smckusic bpref = cgp->cg_rotor; 6385361Smckusic goto norot; 6395361Smckusic } 64017224Smckusick bpref = blknum(fs, bpref); 6415377Smckusic bpref = dtogd(fs, bpref); 6425361Smckusic /* 6435361Smckusic * if the requested block is available, use it 6445361Smckusic */ 64511638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) { 6465361Smckusic bno = bpref; 6475361Smckusic goto gotit; 6485361Smckusic } 6495361Smckusic /* 6505361Smckusic * check for a block available on the same cylinder 6515361Smckusic */ 6525361Smckusic cylno = cbtocylno(fs, bpref); 6535375Smckusic if (cgp->cg_btot[cylno] == 0) 6545375Smckusic goto norot; 6555375Smckusic if (fs->fs_cpc == 0) { 6565375Smckusic /* 6575375Smckusic * block layout info is not available, so just have 6585375Smckusic * to take any block in this cylinder. 6595375Smckusic */ 6605375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 6615375Smckusic goto norot; 6625375Smckusic } 6635375Smckusic /* 6645361Smckusic * check the summary information to see if a block is 6655361Smckusic * available in the requested cylinder starting at the 6669163Ssam * requested rotational position and proceeding around. 6675361Smckusic */ 6689163Ssam cylbp = cgp->cg_b[cylno]; 6699163Ssam pos = cbtorpos(fs, bpref); 6705361Smckusic for (i = pos; i < NRPOS; i++) 6715361Smckusic if (cylbp[i] > 0) 6725361Smckusic break; 6735361Smckusic if (i == NRPOS) 6745361Smckusic for (i = 0; i < pos; i++) 6755361Smckusic if (cylbp[i] > 0) 6765361Smckusic break; 6775361Smckusic if (cylbp[i] > 0) { 6784651Smckusic /* 6795361Smckusic * found a rotational position, now find the actual 6805361Smckusic * block. A panic if none is actually there. 6814651Smckusic */ 6825361Smckusic pos = cylno % fs->fs_cpc; 6835361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 6846716Smckusick if (fs->fs_postbl[pos][i] == -1) { 6856716Smckusick printf("pos = %d, i = %d, fs = %s\n", 6866716Smckusick pos, i, fs->fs_fsmnt); 6875361Smckusic panic("alloccgblk: cyl groups corrupted"); 6886716Smckusick } 6896294Smckusick for (i = fs->fs_postbl[pos][i];; ) { 6905361Smckusic if (isblock(fs, cgp->cg_free, bno + i)) { 69111638Ssam bno = blkstofrags(fs, (bno + i)); 6925361Smckusic goto gotit; 6935361Smckusic } 6946294Smckusick delta = fs->fs_rotbl[i]; 6956294Smckusick if (delta <= 0 || delta > MAXBPC - i) 6964651Smckusic break; 6976294Smckusick i += delta; 6984651Smckusic } 6996716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 7005361Smckusic panic("alloccgblk: can't find blk in cyl"); 7014359Smckusick } 7025361Smckusic norot: 7035361Smckusic /* 7045361Smckusic * no blocks in the requested cylinder, so take next 7055361Smckusic * available one in this cylinder group. 7065361Smckusic */ 7078628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 7086567Smckusic if (bno < 0) 7096294Smckusick return (NULL); 7104651Smckusic cgp->cg_rotor = bno; 7114359Smckusick gotit: 71211638Ssam clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno)); 7134792Smckusic cgp->cg_cs.cs_nbfree--; 7144792Smckusic fs->fs_cstotal.cs_nbfree--; 7155322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 7165375Smckusic cylno = cbtocylno(fs, bno); 7175375Smckusic cgp->cg_b[cylno][cbtorpos(fs, bno)]--; 7185375Smckusic cgp->cg_btot[cylno]--; 7194359Smckusick fs->fs_fmod++; 7204651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 7214359Smckusick } 7224359Smckusick 7235375Smckusic /* 7245375Smckusic * Determine whether an inode can be allocated. 7255375Smckusic * 7265375Smckusic * Check to see if an inode is available, and if it is, 7275375Smckusic * allocate it using the following policy: 7285375Smckusic * 1) allocate the requested inode. 7295375Smckusic * 2) allocate the next available inode after the requested 7305375Smckusic * inode in the specified cylinder group. 7315375Smckusic */ 7329163Ssam ino_t 7335965Smckusic ialloccg(ip, cg, ipref, mode) 7345965Smckusic struct inode *ip; 7354359Smckusick int cg; 7364359Smckusick daddr_t ipref; 7374359Smckusick int mode; 7384359Smckusick { 7395965Smckusic register struct fs *fs; 7404463Smckusic register struct cg *cgp; 74116784Smckusick struct buf *bp; 74216784Smckusick int start, len, loc, map, i; 7434359Smckusick 7445965Smckusic fs = ip->i_fs; 7455322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 7466294Smckusick return (NULL); 74710278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 7486531Smckusick cgp = bp->b_un.b_cg; 74915950Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC || 75015950Smckusick cgp->cg_cs.cs_nifree == 0) { 7515960Smckusic brelse(bp); 7526294Smckusick return (NULL); 7535960Smckusic } 7548105Sroot cgp->cg_time = time.tv_sec; 7554359Smckusick if (ipref) { 7564359Smckusick ipref %= fs->fs_ipg; 7574359Smckusick if (isclr(cgp->cg_iused, ipref)) 7584359Smckusick goto gotit; 75916784Smckusick } 76016784Smckusick start = cgp->cg_irotor / NBBY; 76116784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 76216784Smckusick loc = skpc(0xff, len, &cgp->cg_iused[start]); 76316784Smckusick if (loc == 0) { 76417697Smckusick len = start + 1; 76517697Smckusick start = 0; 76617697Smckusick loc = skpc(0xff, len, &cgp->cg_iused[0]); 76717697Smckusick if (loc == 0) { 76817697Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 76917697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 77017697Smckusick panic("ialloccg: map corrupted"); 77117697Smckusick /* NOTREACHED */ 77217697Smckusick } 77316784Smckusick } 77416784Smckusick i = start + len - loc; 77516784Smckusick map = cgp->cg_iused[i]; 77616784Smckusick ipref = i * NBBY; 77716784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 77816784Smckusick if ((map & i) == 0) { 7794359Smckusick cgp->cg_irotor = ipref; 7804359Smckusick goto gotit; 7814359Smckusick } 7824359Smckusick } 78316784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 78416784Smckusick panic("ialloccg: block not in map"); 78516784Smckusick /* NOTREACHED */ 7864359Smckusick gotit: 7874359Smckusick setbit(cgp->cg_iused, ipref); 7884792Smckusic cgp->cg_cs.cs_nifree--; 7894792Smckusic fs->fs_cstotal.cs_nifree--; 7905322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 7914359Smckusick fs->fs_fmod++; 7924359Smckusick if ((mode & IFMT) == IFDIR) { 7934792Smckusic cgp->cg_cs.cs_ndir++; 7944792Smckusic fs->fs_cstotal.cs_ndir++; 7955322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 7964359Smckusick } 7974359Smckusick bdwrite(bp); 7984359Smckusick return (cg * fs->fs_ipg + ipref); 7994359Smckusick } 8004359Smckusick 8015375Smckusic /* 8025375Smckusic * Free a block or fragment. 8035375Smckusic * 8045375Smckusic * The specified block or fragment is placed back in the 8055375Smckusic * free map. If a fragment is deallocated, a possible 8065375Smckusic * block reassembly is checked. 8075375Smckusic */ 8089163Ssam free(ip, bno, size) 8095965Smckusic register struct inode *ip; 8104359Smckusick daddr_t bno; 8115212Smckusic off_t size; 8124359Smckusick { 8134359Smckusick register struct fs *fs; 8144359Smckusick register struct cg *cgp; 8154359Smckusick register struct buf *bp; 8164463Smckusic int cg, blk, frags, bbase; 8174463Smckusic register int i; 8184359Smckusick 8195965Smckusic fs = ip->i_fs; 8206716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 8216716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 8226716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 8234426Smckusic panic("free: bad size"); 8246716Smckusick } 8255377Smckusic cg = dtog(fs, bno); 8266567Smckusic if (badblock(fs, bno)) { 8276567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 8284359Smckusick return; 8296567Smckusic } 83010278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 8316531Smckusick cgp = bp->b_un.b_cg; 8326531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 8335960Smckusic brelse(bp); 8344359Smckusick return; 8355960Smckusic } 8368105Sroot cgp->cg_time = time.tv_sec; 8375377Smckusic bno = dtogd(fs, bno); 8385322Smckusic if (size == fs->fs_bsize) { 83911638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) { 8406716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 8416716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 8424426Smckusic panic("free: freeing free block"); 8436567Smckusic } 84411638Ssam setblock(fs, cgp->cg_free, fragstoblks(fs, bno)); 8454792Smckusic cgp->cg_cs.cs_nbfree++; 8464792Smckusic fs->fs_cstotal.cs_nbfree++; 8475322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 8485375Smckusic i = cbtocylno(fs, bno); 8495375Smckusic cgp->cg_b[i][cbtorpos(fs, bno)]++; 8505375Smckusic cgp->cg_btot[i]++; 8514426Smckusic } else { 85217224Smckusick bbase = bno - fragnum(fs, bno); 8534463Smckusic /* 8544463Smckusic * decrement the counts associated with the old frags 8554463Smckusic */ 8566294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 8575322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 8584463Smckusic /* 8594463Smckusic * deallocate the fragment 8604463Smckusic */ 8615960Smckusic frags = numfrags(fs, size); 8624463Smckusic for (i = 0; i < frags; i++) { 8636716Smckusick if (isset(cgp->cg_free, bno + i)) { 8646716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 8656716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 8664426Smckusic panic("free: freeing free frag"); 8676716Smckusick } 8684426Smckusic setbit(cgp->cg_free, bno + i); 8694426Smckusic } 8706294Smckusick cgp->cg_cs.cs_nffree += i; 8716294Smckusick fs->fs_cstotal.cs_nffree += i; 8726294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 8734463Smckusic /* 8744463Smckusic * add back in counts associated with the new frags 8754463Smckusic */ 8766294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 8775322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 8784463Smckusic /* 8794463Smckusic * if a complete block has been reassembled, account for it 8804463Smckusic */ 88111638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) { 8825322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 8835322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 8845322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 8854792Smckusic cgp->cg_cs.cs_nbfree++; 8864792Smckusic fs->fs_cstotal.cs_nbfree++; 8875322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 8885375Smckusic i = cbtocylno(fs, bbase); 8895375Smckusic cgp->cg_b[i][cbtorpos(fs, bbase)]++; 8905375Smckusic cgp->cg_btot[i]++; 8914426Smckusic } 8924426Smckusic } 8934359Smckusick fs->fs_fmod++; 8944359Smckusick bdwrite(bp); 8954359Smckusick } 8964359Smckusick 8975375Smckusic /* 8985375Smckusic * Free an inode. 8995375Smckusic * 9005375Smckusic * The specified inode is placed back in the free map. 9015375Smckusic */ 9025965Smckusic ifree(ip, ino, mode) 9035965Smckusic struct inode *ip; 9044359Smckusick ino_t ino; 9054359Smckusick int mode; 9064359Smckusick { 9074359Smckusick register struct fs *fs; 9084359Smckusick register struct cg *cgp; 9094359Smckusick register struct buf *bp; 9104359Smckusick int cg; 9114359Smckusick 9125965Smckusic fs = ip->i_fs; 9136716Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 9146716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9156716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9164359Smckusick panic("ifree: range"); 9176716Smckusick } 9185377Smckusic cg = itog(fs, ino); 91910278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 9206531Smckusick cgp = bp->b_un.b_cg; 9216531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 9225960Smckusic brelse(bp); 9234359Smckusick return; 9245960Smckusic } 9258105Sroot cgp->cg_time = time.tv_sec; 9264359Smckusick ino %= fs->fs_ipg; 9276716Smckusick if (isclr(cgp->cg_iused, ino)) { 9286716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9296716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9304359Smckusick panic("ifree: freeing free inode"); 9316716Smckusick } 9324359Smckusick clrbit(cgp->cg_iused, ino); 93316784Smckusick if (ino < cgp->cg_irotor) 93416784Smckusick cgp->cg_irotor = ino; 9354792Smckusic cgp->cg_cs.cs_nifree++; 9364792Smckusic fs->fs_cstotal.cs_nifree++; 9375322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 9384359Smckusick if ((mode & IFMT) == IFDIR) { 9394792Smckusic cgp->cg_cs.cs_ndir--; 9404792Smckusic fs->fs_cstotal.cs_ndir--; 9415322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 9424359Smckusick } 9434359Smckusick fs->fs_fmod++; 9444359Smckusick bdwrite(bp); 9454359Smckusick } 9464359Smckusick 9474463Smckusic /* 9485375Smckusic * Find a block of the specified size in the specified cylinder group. 9495375Smckusic * 9504651Smckusic * It is a panic if a request is made to find a block if none are 9514651Smckusic * available. 9524651Smckusic */ 9534651Smckusic daddr_t 9544651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 9554651Smckusic register struct fs *fs; 9564651Smckusic register struct cg *cgp; 9574651Smckusic daddr_t bpref; 9584651Smckusic int allocsiz; 9594651Smckusic { 9604651Smckusic daddr_t bno; 9614651Smckusic int start, len, loc, i; 9624651Smckusic int blk, field, subfield, pos; 9634651Smckusic 9644651Smckusic /* 9654651Smckusic * find the fragment by searching through the free block 9664651Smckusic * map for an appropriate bit pattern 9674651Smckusic */ 9684651Smckusic if (bpref) 9695377Smckusic start = dtogd(fs, bpref) / NBBY; 9704651Smckusic else 9714651Smckusic start = cgp->cg_frotor / NBBY; 9725398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 97312755Ssam loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start], 97412755Ssam (caddr_t)fragtbl[fs->fs_frag], 97512755Ssam (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 9764651Smckusic if (loc == 0) { 9776531Smckusick len = start + 1; 9786531Smckusick start = 0; 97917697Smckusick loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[0], 98012755Ssam (caddr_t)fragtbl[fs->fs_frag], 98112755Ssam (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 98216784Smckusick if (loc == 0) { 98316784Smckusick printf("start = %d, len = %d, fs = %s\n", 98416784Smckusick start, len, fs->fs_fsmnt); 98516784Smckusick panic("alloccg: map corrupted"); 98617697Smckusick /* NOTREACHED */ 98716784Smckusick } 9884651Smckusic } 9894651Smckusic bno = (start + len - loc) * NBBY; 9904651Smckusic cgp->cg_frotor = bno; 9914651Smckusic /* 9924651Smckusic * found the byte in the map 9934651Smckusic * sift through the bits to find the selected frag 9944651Smckusic */ 9956294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 9966294Smckusick blk = blkmap(fs, cgp->cg_free, bno); 9974651Smckusic blk <<= 1; 9984651Smckusic field = around[allocsiz]; 9994651Smckusic subfield = inside[allocsiz]; 10005322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 10016294Smckusick if ((blk & field) == subfield) 10026294Smckusick return (bno + pos); 10034651Smckusic field <<= 1; 10044651Smckusic subfield <<= 1; 10054651Smckusic } 10064651Smckusic } 10076716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 10084651Smckusic panic("alloccg: block not in map"); 10096531Smckusick return (-1); 10104651Smckusic } 10114651Smckusic 10124651Smckusic /* 10135375Smckusic * Fserr prints the name of a file system with an error diagnostic. 10145375Smckusic * 10155375Smckusic * The form of the error message is: 10164359Smckusick * fs: error message 10174359Smckusick */ 10184359Smckusick fserr(fs, cp) 10194359Smckusick struct fs *fs; 10204359Smckusick char *cp; 10214359Smckusick { 10224359Smckusick 102324839Seric log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp); 10244359Smckusick } 1025