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*42318Smckusick * @(#)lfs_alloc.c 7.19 (Berkeley) 05/24/90 1823394Smckusick */ 194359Smckusick 2017097Sbloom #include "param.h" 2117097Sbloom #include "systm.h" 2217097Sbloom #include "buf.h" 2317097Sbloom #include "user.h" 2437735Smckusick #include "vnode.h" 2517097Sbloom #include "kernel.h" 2618307Sralph #include "syslog.h" 2726253Skarels #include "cmap.h" 2837735Smckusick #include "../ufs/quota.h" 2937735Smckusick #include "../ufs/inode.h" 3037735Smckusick #include "../ufs/fs.h" 314359Smckusick 325212Smckusic extern u_long hashalloc(); 339163Ssam extern ino_t ialloccg(); 349163Ssam extern daddr_t alloccg(); 354651Smckusic extern daddr_t alloccgblk(); 364651Smckusic extern daddr_t fragextend(); 374651Smckusic extern daddr_t blkpref(); 384651Smckusic extern daddr_t mapsearch(); 394607Smckusic extern int inside[], around[]; 405322Smckusic extern unsigned char *fragtbl[]; 414359Smckusick 425375Smckusic /* 435375Smckusic * Allocate a block in the file system. 445375Smckusic * 455375Smckusic * The size of the requested block is given, which must be some 465375Smckusic * multiple of fs_fsize and <= fs_bsize. 475375Smckusic * A preference may be optionally specified. If a preference is given 485375Smckusic * the following hierarchy is used to allocate a block: 495375Smckusic * 1) allocate the requested block. 505375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 515375Smckusic * 3) allocate a block in the same cylinder group. 525375Smckusic * 4) quadradically rehash into other cylinder groups, until an 535375Smckusic * available block is located. 545375Smckusic * If no block preference is given the following heirarchy is used 555375Smckusic * to allocate a block: 565375Smckusic * 1) allocate a block in the cylinder group that contains the 575375Smckusic * inode for the file. 585375Smckusic * 2) quadradically rehash into other cylinder groups, until an 595375Smckusic * available block is located. 605375Smckusic */ 6139678Smckusick alloc(ip, lbn, bpref, size, bnp) 624463Smckusic register struct inode *ip; 6339678Smckusick daddr_t lbn, bpref; 644359Smckusick int size; 6539678Smckusick daddr_t *bnp; 664359Smckusick { 674359Smckusick daddr_t bno; 684359Smckusick register struct fs *fs; 694463Smckusic register struct buf *bp; 7037735Smckusick int cg, error; 7141309Smckusick struct ucred *cred = u.u_cred; /* XXX */ 724359Smckusick 7339678Smckusick *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; 8241309Smckusick if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 834792Smckusic goto nospace; 847650Ssam #ifdef QUOTA 8541309Smckusick if (error = chkdq(ip, (long)btodb(size), cred, 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); 9639678Smckusick if (bno > 0) { 9739678Smckusick ip->i_blocks += btodb(size); 9839678Smckusick ip->i_flag |= IUPD|ICHG; 9939678Smckusick *bnp = bno; 10039678Smckusick return (0); 10139678Smckusick } 1024359Smckusick nospace: 103*42318Smckusick fserr(fs, cred->cr_uid, "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 */ 11639678Smckusick realloccg(ip, lbprev, bpref, osize, nsize, bpp) 1175965Smckusic register struct inode *ip; 11839678Smckusick off_t lbprev; 11939678Smckusick 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; 12639678Smckusick daddr_t bprev, bno, bn; 12737735Smckusick int i, error, count; 12841309Smckusick struct ucred *cred = u.u_cred; /* XXX */ 1294426Smckusic 13037735Smckusick *bpp = 0; 1315965Smckusic fs = ip->i_fs; 1325960Smckusic if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 1336716Smckusick (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 1346716Smckusick printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 1356716Smckusick ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 1364463Smckusic panic("realloccg: bad size"); 1376716Smckusick } 13841309Smckusick if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 1394792Smckusic goto nospace; 14039678Smckusick if ((bprev = ip->i_db[lbprev]) == 0) { 1416716Smckusick printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 1426716Smckusick ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 1434463Smckusic panic("realloccg: bad bprev"); 1446716Smckusick } 1457650Ssam #ifdef QUOTA 14641309Smckusick if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) 14737735Smckusick return (error); 1487483Skre #endif 14939678Smckusick /* 15039678Smckusick * Allocate the extra space in the buffer. 15139678Smckusick */ 15239678Smckusick if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) { 15339678Smckusick brelse(bp); 15439678Smckusick return (error); 15539678Smckusick } 15639678Smckusick brealloc(bp, nsize); 15739678Smckusick bp->b_flags |= B_DONE; 15839678Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 15939678Smckusick /* 16039678Smckusick * Check for extension in the existing location. 16139678Smckusick */ 1626294Smckusick cg = dtog(fs, bprev); 16339678Smckusick if (bno = fragextend(ip, cg, (long)bprev, osize, nsize)) { 16439887Smckusick if (bp->b_blkno != fsbtodb(fs, bno)) 16539678Smckusick panic("bad blockno"); 16639762Smckusick ip->i_blocks += btodb(nsize - osize); 16739762Smckusick ip->i_flag |= IUPD|ICHG; 16837735Smckusick *bpp = bp; 16937735Smckusick return (0); 1704463Smckusic } 17139678Smckusick /* 17239678Smckusick * Allocate a new disk location. 17339678Smckusick */ 1744948Smckusic if (bpref >= fs->fs_size) 1754948Smckusic bpref = 0; 17626253Skarels switch ((int)fs->fs_optim) { 17725256Smckusick case FS_OPTSPACE: 17825256Smckusick /* 17925256Smckusick * Allocate an exact sized fragment. Although this makes 18025256Smckusick * best use of space, we will waste time relocating it if 18125256Smckusick * the file continues to grow. If the fragmentation is 18225256Smckusick * less than half of the minimum free reserve, we choose 18325256Smckusick * to begin optimizing for time. 18425256Smckusick */ 18524698Smckusick request = nsize; 18625256Smckusick if (fs->fs_minfree < 5 || 18725256Smckusick fs->fs_cstotal.cs_nffree > 18825256Smckusick fs->fs_dsize * fs->fs_minfree / (2 * 100)) 18925256Smckusick break; 19025256Smckusick log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 19125256Smckusick fs->fs_fsmnt); 19225256Smckusick fs->fs_optim = FS_OPTTIME; 19325256Smckusick break; 19425256Smckusick case FS_OPTTIME: 19525256Smckusick /* 19625256Smckusick * At this point we have discovered a file that is trying 19725256Smckusick * to grow a small fragment to a larger fragment. To save 19825256Smckusick * time, we allocate a full sized block, then free the 19925256Smckusick * unused portion. If the file continues to grow, the 20025256Smckusick * `fragextend' call above will be able to grow it in place 20125256Smckusick * without further copying. If aberrant programs cause 20225256Smckusick * disk fragmentation to grow within 2% of the free reserve, 20325256Smckusick * we choose to begin optimizing for space. 20425256Smckusick */ 20524698Smckusick request = fs->fs_bsize; 20625256Smckusick if (fs->fs_cstotal.cs_nffree < 20725256Smckusick fs->fs_dsize * (fs->fs_minfree - 2) / 100) 20825256Smckusick break; 20925256Smckusick log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 21025256Smckusick fs->fs_fsmnt); 21125256Smckusick fs->fs_optim = FS_OPTSPACE; 21225256Smckusick break; 21325256Smckusick default: 21425256Smckusick printf("dev = 0x%x, optim = %d, fs = %s\n", 21525256Smckusick ip->i_dev, fs->fs_optim, fs->fs_fsmnt); 21625256Smckusick panic("realloccg: bad optim"); 21725256Smckusick /* NOTREACHED */ 21825256Smckusick } 21924698Smckusick bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request, 2209163Ssam (u_long (*)())alloccg); 2216567Smckusic if (bno > 0) { 22239678Smckusick bp->b_blkno = bn = fsbtodb(fs, bno); 22330749Skarels count = howmany(osize, CLBYTES); 22430749Skarels for (i = 0; i < count; i++) 22537735Smckusick munhash(ip->i_devvp, bn + i * CLBYTES / DEV_BSIZE); 22631402Smckusick blkfree(ip, bprev, (off_t)osize); 22724698Smckusick if (nsize < request) 22831402Smckusick blkfree(ip, bno + numfrags(fs, nsize), 22924698Smckusick (off_t)(request - nsize)); 23012643Ssam ip->i_blocks += btodb(nsize - osize); 23112643Ssam ip->i_flag |= IUPD|ICHG; 23237735Smckusick *bpp = bp; 23337735Smckusick return (0); 2344463Smckusic } 23539678Smckusick brelse(bp); 2364792Smckusic nospace: 2374463Smckusic /* 2384463Smckusic * no space available 2394463Smckusic */ 240*42318Smckusick fserr(fs, cred->cr_uid, "file system full"); 2414426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 24237735Smckusick return (ENOSPC); 2434426Smckusic } 2444426Smckusic 2455375Smckusic /* 2465375Smckusic * Allocate an inode in the file system. 2475375Smckusic * 2485375Smckusic * A preference may be optionally specified. If a preference is given 2495375Smckusic * the following hierarchy is used to allocate an inode: 2505375Smckusic * 1) allocate the requested inode. 2515375Smckusic * 2) allocate an inode in the same cylinder group. 2525375Smckusic * 3) quadradically rehash into other cylinder groups, until an 2535375Smckusic * available inode is located. 2545375Smckusic * If no inode preference is given the following heirarchy is used 2555375Smckusic * to allocate an inode: 2565375Smckusic * 1) allocate an inode in cylinder group 0. 2575375Smckusic * 2) quadradically rehash into other cylinder groups, until an 2585375Smckusic * available inode is located. 2595375Smckusic */ 26041309Smckusick ialloc(pip, ipref, mode, cred, ipp) 2615965Smckusic register struct inode *pip; 2624359Smckusick ino_t ipref; 2634359Smckusick int mode; 26441309Smckusick struct ucred *cred; 26537735Smckusick struct inode **ipp; 2664359Smckusick { 2675212Smckusic ino_t ino; 2684359Smckusick register struct fs *fs; 2694359Smckusick register struct inode *ip; 27037735Smckusick int cg, error; 2714359Smckusick 27237735Smckusick *ipp = 0; 2735965Smckusic fs = pip->i_fs; 2744792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 2754359Smckusick goto noinodes; 2764948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 2774948Smckusic ipref = 0; 2785377Smckusic cg = itog(fs, ipref); 2795965Smckusic ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); 2804359Smckusick if (ino == 0) 2814359Smckusick goto noinodes; 28237735Smckusick error = iget(pip, ino, ipp); 28337735Smckusick if (error) { 28441309Smckusick ifree(pip, ino, mode); 28537735Smckusick return (error); 2864359Smckusick } 28738255Smckusick ip = *ipp; 2886716Smckusick if (ip->i_mode) { 2896716Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 2906716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 2914359Smckusick panic("ialloc: dup alloc"); 2926716Smckusick } 29312643Ssam if (ip->i_blocks) { /* XXX */ 29412643Ssam printf("free inode %s/%d had %d blocks\n", 29512643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 29612643Ssam ip->i_blocks = 0; 29712643Ssam } 29839516Smckusick ip->i_flags = 0; 29938255Smckusick /* 30038255Smckusick * Set up a new generation number for this inode. 30138255Smckusick */ 30238255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 30338255Smckusick nextgennumber = time.tv_sec; 30438255Smckusick ip->i_gen = nextgennumber; 30537735Smckusick return (0); 3064359Smckusick noinodes: 307*42318Smckusick fserr(fs, cred->cr_uid, "out of inodes"); 3086294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 30937735Smckusick return (ENOSPC); 3104359Smckusick } 3114359Smckusick 3124651Smckusic /* 3135375Smckusic * Find a cylinder to place a directory. 3145375Smckusic * 3155375Smckusic * The policy implemented by this algorithm is to select from 3165375Smckusic * among those cylinder groups with above the average number of 3175375Smckusic * free inodes, the one with the smallest number of directories. 3184651Smckusic */ 3199163Ssam ino_t 3205965Smckusic dirpref(fs) 3215965Smckusic register struct fs *fs; 3224359Smckusick { 3234651Smckusic int cg, minndir, mincg, avgifree; 3244359Smckusick 3254792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 3264651Smckusic minndir = fs->fs_ipg; 3274359Smckusick mincg = 0; 3284651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 3295322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 3305322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 3314359Smckusick mincg = cg; 3325322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 3334359Smckusick } 3349163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 3354359Smckusick } 3364359Smckusick 3374651Smckusic /* 3389163Ssam * Select the desired position for the next block in a file. The file is 3399163Ssam * logically divided into sections. The first section is composed of the 3409163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 3419163Ssam * 3429163Ssam * If no blocks have been allocated in the first section, the policy is to 3439163Ssam * request a block in the same cylinder group as the inode that describes 3449163Ssam * the file. If no blocks have been allocated in any other section, the 3459163Ssam * policy is to place the section in a cylinder group with a greater than 3469163Ssam * average number of free blocks. An appropriate cylinder group is found 34717696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of 34817696Smckusick * blocks is needed, the sweep begins in the cylinder group following the 34917696Smckusick * cylinder group from which the previous allocation was made. The sweep 35017696Smckusick * continues until a cylinder group with greater than the average number 35117696Smckusick * of free blocks is found. If the allocation is for the first block in an 35217696Smckusick * indirect block, the information on the previous allocation is unavailable; 35317696Smckusick * here a best guess is made based upon the logical block number being 35417696Smckusick * allocated. 3559163Ssam * 3569163Ssam * If a section is already partially allocated, the policy is to 3579163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 3589163Ssam * contiguous blocks and the beginning of the next is physically separated 3599163Ssam * so that the disk head will be in transit between them for at least 3609163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 3619163Ssam * schedule another I/O transfer. 3624651Smckusic */ 3635212Smckusic daddr_t 3649163Ssam blkpref(ip, lbn, indx, bap) 3659163Ssam struct inode *ip; 3669163Ssam daddr_t lbn; 3679163Ssam int indx; 3689163Ssam daddr_t *bap; 3699163Ssam { 3705965Smckusic register struct fs *fs; 37117696Smckusick register int cg; 37217696Smckusick int avgbfree, startcg; 3739163Ssam daddr_t nextblk; 3744651Smckusic 3759163Ssam fs = ip->i_fs; 3769163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 3779163Ssam if (lbn < NDADDR) { 3789163Ssam cg = itog(fs, ip->i_number); 3795322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 3804651Smckusic } 3819163Ssam /* 3829163Ssam * Find a cylinder with greater than average number of 3839163Ssam * unused data blocks. 3849163Ssam */ 38517696Smckusick if (indx == 0 || bap[indx - 1] == 0) 38617696Smckusick startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; 38717696Smckusick else 38817696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1; 38917696Smckusick startcg %= fs->fs_ncg; 3909163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 39117696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++) 3929163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3939163Ssam fs->fs_cgrotor = cg; 3949163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3959163Ssam } 39617696Smckusick for (cg = 0; cg <= startcg; cg++) 3979163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3989163Ssam fs->fs_cgrotor = cg; 3999163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4009163Ssam } 4019163Ssam return (NULL); 4029163Ssam } 4039163Ssam /* 4049163Ssam * One or more previous blocks have been laid out. If less 4059163Ssam * than fs_maxcontig previous blocks are contiguous, the 4069163Ssam * next block is requested contiguously, otherwise it is 4079163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 4089163Ssam */ 4099163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 4109163Ssam if (indx > fs->fs_maxcontig && 41111638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 4129163Ssam != nextblk) 4139163Ssam return (nextblk); 4149163Ssam if (fs->fs_rotdelay != 0) 4159163Ssam /* 4169163Ssam * Here we convert ms of delay to frags as: 4179163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 4189163Ssam * ((sect/frag) * (ms/sec)) 4199163Ssam * then round up to the next block. 4209163Ssam */ 4219163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 4229163Ssam (NSPF(fs) * 1000), fs->fs_frag); 4239163Ssam return (nextblk); 4244651Smckusic } 4254651Smckusic 4265375Smckusic /* 4275375Smckusic * Implement the cylinder overflow algorithm. 4285375Smckusic * 4295375Smckusic * The policy implemented by this algorithm is: 4305375Smckusic * 1) allocate the block in its requested cylinder group. 4315375Smckusic * 2) quadradically rehash on the cylinder group number. 4325375Smckusic * 3) brute force search for a free block. 4335375Smckusic */ 4345212Smckusic /*VARARGS5*/ 4355212Smckusic u_long 4365965Smckusic hashalloc(ip, cg, pref, size, allocator) 4375965Smckusic struct inode *ip; 4384359Smckusick int cg; 4394359Smckusick long pref; 4404359Smckusick int size; /* size for data blocks, mode for inodes */ 4415212Smckusic u_long (*allocator)(); 4424359Smckusick { 4435965Smckusic register struct fs *fs; 4444359Smckusick long result; 4454359Smckusick int i, icg = cg; 4464359Smckusick 4475965Smckusic fs = ip->i_fs; 4484359Smckusick /* 4494359Smckusick * 1: preferred cylinder group 4504359Smckusick */ 4515965Smckusic result = (*allocator)(ip, cg, pref, size); 4524359Smckusick if (result) 4534359Smckusick return (result); 4544359Smckusick /* 4554359Smckusick * 2: quadratic rehash 4564359Smckusick */ 4574359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 4584359Smckusick cg += i; 4594359Smckusick if (cg >= fs->fs_ncg) 4604359Smckusick cg -= fs->fs_ncg; 4615965Smckusic result = (*allocator)(ip, cg, 0, size); 4624359Smckusick if (result) 4634359Smckusick return (result); 4644359Smckusick } 4654359Smckusick /* 4664359Smckusick * 3: brute force search 46710847Ssam * Note that we start at i == 2, since 0 was checked initially, 46810847Ssam * and 1 is always checked in the quadratic rehash. 4694359Smckusick */ 47010848Smckusick cg = (icg + 2) % fs->fs_ncg; 47110847Ssam for (i = 2; i < fs->fs_ncg; i++) { 4725965Smckusic result = (*allocator)(ip, cg, 0, size); 4734359Smckusick if (result) 4744359Smckusick return (result); 4754359Smckusick cg++; 4764359Smckusick if (cg == fs->fs_ncg) 4774359Smckusick cg = 0; 4784359Smckusick } 4796294Smckusick return (NULL); 4804359Smckusick } 4814359Smckusick 4825375Smckusic /* 4835375Smckusic * Determine whether a fragment can be extended. 4845375Smckusic * 4855375Smckusic * Check to see if the necessary fragments are available, and 4865375Smckusic * if they are, allocate them. 4875375Smckusic */ 4884359Smckusick daddr_t 4895965Smckusic fragextend(ip, cg, bprev, osize, nsize) 4905965Smckusic struct inode *ip; 4914426Smckusic int cg; 4924463Smckusic long bprev; 4934426Smckusic int osize, nsize; 4944426Smckusic { 4955965Smckusic register struct fs *fs; 4964463Smckusic register struct cg *cgp; 49737735Smckusick struct buf *bp; 4984463Smckusic long bno; 4994463Smckusic int frags, bbase; 50037735Smckusick int i, error; 5014426Smckusic 5025965Smckusic fs = ip->i_fs; 50317224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 5046531Smckusick return (NULL); 5055960Smckusic frags = numfrags(fs, nsize); 50617224Smckusick bbase = fragnum(fs, bprev); 50717224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 50830749Skarels /* cannot extend across a block boundary */ 5096294Smckusick return (NULL); 5104463Smckusic } 51137735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 51238776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 51337735Smckusick if (error) { 51437735Smckusick brelse(bp); 51537735Smckusick return (NULL); 51637735Smckusick } 5176531Smckusick cgp = bp->b_un.b_cg; 51837735Smckusick if (!cg_chkmagic(cgp)) { 5195960Smckusic brelse(bp); 5206294Smckusick return (NULL); 5215960Smckusic } 5228105Sroot cgp->cg_time = time.tv_sec; 5235377Smckusic bno = dtogd(fs, bprev); 5245960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 52534143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) { 5265361Smckusic brelse(bp); 5276294Smckusick return (NULL); 5285361Smckusic } 5295361Smckusic /* 5305361Smckusic * the current fragment can be extended 5315361Smckusic * deduct the count on fragment being extended into 5325361Smckusic * increase the count on the remaining fragment (if any) 5335361Smckusic * allocate the extended piece 5345361Smckusic */ 5355361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 53634143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) 5374463Smckusic break; 5385960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 5395361Smckusic if (i != frags) 5405361Smckusic cgp->cg_frsum[i - frags]++; 5415960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 54234143Smckusick clrbit(cg_blksfree(cgp), bno + i); 5435361Smckusic cgp->cg_cs.cs_nffree--; 5445361Smckusic fs->fs_cstotal.cs_nffree--; 5455361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 5464463Smckusic } 5475361Smckusic fs->fs_fmod++; 5485361Smckusic bdwrite(bp); 5495361Smckusic return (bprev); 5504426Smckusic } 5514426Smckusic 5525375Smckusic /* 5535375Smckusic * Determine whether a block can be allocated. 5545375Smckusic * 5555375Smckusic * Check to see if a block of the apprpriate size is available, 5565375Smckusic * and if it is, allocate it. 5575375Smckusic */ 5589163Ssam daddr_t 5595965Smckusic alloccg(ip, cg, bpref, size) 5605965Smckusic struct inode *ip; 5614359Smckusick int cg; 5624359Smckusick daddr_t bpref; 5634359Smckusick int size; 5644359Smckusick { 5655965Smckusic register struct fs *fs; 5664463Smckusic register struct cg *cgp; 56737735Smckusick struct buf *bp; 5684463Smckusic register int i; 56937735Smckusick int error, bno, frags, allocsiz; 5704359Smckusick 5715965Smckusic fs = ip->i_fs; 5725322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 5736294Smckusick return (NULL); 57437735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 57538776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 57637735Smckusick if (error) { 57737735Smckusick brelse(bp); 57837735Smckusick return (NULL); 57937735Smckusick } 5806531Smckusick cgp = bp->b_un.b_cg; 58137735Smckusick if (!cg_chkmagic(cgp) || 58215950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 5835960Smckusic brelse(bp); 5846294Smckusick return (NULL); 5855960Smckusic } 5868105Sroot cgp->cg_time = time.tv_sec; 5875322Smckusic if (size == fs->fs_bsize) { 5885212Smckusic bno = alloccgblk(fs, cgp, bpref); 5894463Smckusic bdwrite(bp); 5904463Smckusic return (bno); 5914463Smckusic } 5924463Smckusic /* 5934463Smckusic * check to see if any fragments are already available 5944463Smckusic * allocsiz is the size which will be allocated, hacking 5954463Smckusic * it down to a smaller size if necessary 5964463Smckusic */ 5975960Smckusic frags = numfrags(fs, size); 5985322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 5994463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 6004463Smckusic break; 6015322Smckusic if (allocsiz == fs->fs_frag) { 6024463Smckusic /* 6034463Smckusic * no fragments were available, so a block will be 6044463Smckusic * allocated, and hacked up 6054463Smckusic */ 6064792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 6074463Smckusic brelse(bp); 6086294Smckusick return (NULL); 6094463Smckusic } 6105212Smckusic bno = alloccgblk(fs, cgp, bpref); 6115377Smckusic bpref = dtogd(fs, bno); 6125322Smckusic for (i = frags; i < fs->fs_frag; i++) 61334143Smckusick setbit(cg_blksfree(cgp), bpref + i); 6145322Smckusic i = fs->fs_frag - frags; 6154792Smckusic cgp->cg_cs.cs_nffree += i; 6164792Smckusic fs->fs_cstotal.cs_nffree += i; 6175322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 6189762Ssam fs->fs_fmod++; 6194463Smckusic cgp->cg_frsum[i]++; 6204463Smckusic bdwrite(bp); 6214463Smckusic return (bno); 6224463Smckusic } 6234651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 62415950Smckusick if (bno < 0) { 62515950Smckusick brelse(bp); 6266294Smckusick return (NULL); 62715950Smckusick } 6284463Smckusic for (i = 0; i < frags; i++) 62934143Smckusick clrbit(cg_blksfree(cgp), bno + i); 6304792Smckusic cgp->cg_cs.cs_nffree -= frags; 6314792Smckusic fs->fs_cstotal.cs_nffree -= frags; 6325322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 6339762Ssam fs->fs_fmod++; 6344463Smckusic cgp->cg_frsum[allocsiz]--; 6354463Smckusic if (frags != allocsiz) 6364463Smckusic cgp->cg_frsum[allocsiz - frags]++; 6374463Smckusic bdwrite(bp); 6384463Smckusic return (cg * fs->fs_fpg + bno); 6394463Smckusic } 6404463Smckusic 6415375Smckusic /* 6425375Smckusic * Allocate a block in a cylinder group. 6435375Smckusic * 6445375Smckusic * This algorithm implements the following policy: 6455375Smckusic * 1) allocate the requested block. 6465375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 6475375Smckusic * 3) allocate the next available block on the block rotor for the 6485375Smckusic * specified cylinder group. 6495375Smckusic * Note that this routine only allocates fs_bsize blocks; these 6505375Smckusic * blocks may be fragmented by the routine that allocates them. 6515375Smckusic */ 6524463Smckusic daddr_t 6535212Smckusic alloccgblk(fs, cgp, bpref) 6545965Smckusic register struct fs *fs; 6554463Smckusic register struct cg *cgp; 6564463Smckusic daddr_t bpref; 6574463Smckusic { 6584651Smckusic daddr_t bno; 6596294Smckusick int cylno, pos, delta; 6604651Smckusic short *cylbp; 6615361Smckusic register int i; 6624463Smckusic 6634651Smckusic if (bpref == 0) { 6644651Smckusic bpref = cgp->cg_rotor; 6655361Smckusic goto norot; 6665361Smckusic } 66717224Smckusick bpref = blknum(fs, bpref); 6685377Smckusic bpref = dtogd(fs, bpref); 6695361Smckusic /* 6705361Smckusic * if the requested block is available, use it 6715361Smckusic */ 67234143Smckusick if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 6735361Smckusic bno = bpref; 6745361Smckusic goto gotit; 6755361Smckusic } 6765361Smckusic /* 6775361Smckusic * check for a block available on the same cylinder 6785361Smckusic */ 6795361Smckusic cylno = cbtocylno(fs, bpref); 68034143Smckusick if (cg_blktot(cgp)[cylno] == 0) 6815375Smckusic goto norot; 6825375Smckusic if (fs->fs_cpc == 0) { 6835375Smckusic /* 6845375Smckusic * block layout info is not available, so just have 6855375Smckusic * to take any block in this cylinder. 6865375Smckusic */ 6875375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 6885375Smckusic goto norot; 6895375Smckusic } 6905375Smckusic /* 6915361Smckusic * check the summary information to see if a block is 6925361Smckusic * available in the requested cylinder starting at the 6939163Ssam * requested rotational position and proceeding around. 6945361Smckusic */ 69534143Smckusick cylbp = cg_blks(fs, cgp, cylno); 6969163Ssam pos = cbtorpos(fs, bpref); 69734143Smckusick for (i = pos; i < fs->fs_nrpos; i++) 6985361Smckusic if (cylbp[i] > 0) 6995361Smckusic break; 70034143Smckusick if (i == fs->fs_nrpos) 7015361Smckusic for (i = 0; i < pos; i++) 7025361Smckusic if (cylbp[i] > 0) 7035361Smckusic break; 7045361Smckusic if (cylbp[i] > 0) { 7054651Smckusic /* 7065361Smckusic * found a rotational position, now find the actual 7075361Smckusic * block. A panic if none is actually there. 7084651Smckusic */ 7095361Smckusic pos = cylno % fs->fs_cpc; 7105361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 71134143Smckusick if (fs_postbl(fs, pos)[i] == -1) { 7126716Smckusick printf("pos = %d, i = %d, fs = %s\n", 7136716Smckusick pos, i, fs->fs_fsmnt); 7145361Smckusic panic("alloccgblk: cyl groups corrupted"); 7156716Smckusick } 71634143Smckusick for (i = fs_postbl(fs, pos)[i];; ) { 71734143Smckusick if (isblock(fs, cg_blksfree(cgp), bno + i)) { 71811638Ssam bno = blkstofrags(fs, (bno + i)); 7195361Smckusic goto gotit; 7205361Smckusic } 72134143Smckusick delta = fs_rotbl(fs)[i]; 72234143Smckusick if (delta <= 0 || 72334143Smckusick delta + i > fragstoblks(fs, fs->fs_fpg)) 7244651Smckusic break; 7256294Smckusick i += delta; 7264651Smckusic } 7276716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 7285361Smckusic panic("alloccgblk: can't find blk in cyl"); 7294359Smckusick } 7305361Smckusic norot: 7315361Smckusic /* 7325361Smckusic * no blocks in the requested cylinder, so take next 7335361Smckusic * available one in this cylinder group. 7345361Smckusic */ 7358628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 7366567Smckusic if (bno < 0) 7376294Smckusick return (NULL); 7384651Smckusic cgp->cg_rotor = bno; 7394359Smckusick gotit: 74034143Smckusick clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno)); 7414792Smckusic cgp->cg_cs.cs_nbfree--; 7424792Smckusic fs->fs_cstotal.cs_nbfree--; 7435322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 7445375Smckusic cylno = cbtocylno(fs, bno); 74534143Smckusick cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 74634143Smckusick cg_blktot(cgp)[cylno]--; 7474359Smckusick fs->fs_fmod++; 7484651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 7494359Smckusick } 75034143Smckusick 7515375Smckusic /* 7525375Smckusic * Determine whether an inode can be allocated. 7535375Smckusic * 7545375Smckusic * Check to see if an inode is available, and if it is, 7555375Smckusic * allocate it using the following policy: 7565375Smckusic * 1) allocate the requested inode. 7575375Smckusic * 2) allocate the next available inode after the requested 7585375Smckusic * inode in the specified cylinder group. 7595375Smckusic */ 7609163Ssam ino_t 7615965Smckusic ialloccg(ip, cg, ipref, mode) 7625965Smckusic struct inode *ip; 7634359Smckusick int cg; 7644359Smckusick daddr_t ipref; 7654359Smckusick int mode; 7664359Smckusick { 7675965Smckusic register struct fs *fs; 7684463Smckusic register struct cg *cgp; 76916784Smckusick struct buf *bp; 77037735Smckusick int error, start, len, loc, map, i; 7714359Smckusick 7725965Smckusic fs = ip->i_fs; 7735322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 7746294Smckusick return (NULL); 77537735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 77638776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 77737735Smckusick if (error) { 77837735Smckusick brelse(bp); 77937735Smckusick return (NULL); 78037735Smckusick } 7816531Smckusick cgp = bp->b_un.b_cg; 78237735Smckusick if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 7835960Smckusic brelse(bp); 7846294Smckusick return (NULL); 7855960Smckusic } 7868105Sroot cgp->cg_time = time.tv_sec; 7874359Smckusick if (ipref) { 7884359Smckusick ipref %= fs->fs_ipg; 78934143Smckusick if (isclr(cg_inosused(cgp), ipref)) 7904359Smckusick goto gotit; 79116784Smckusick } 79216784Smckusick start = cgp->cg_irotor / NBBY; 79316784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 79434143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 79516784Smckusick if (loc == 0) { 79617697Smckusick len = start + 1; 79717697Smckusick start = 0; 79834143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 79917697Smckusick if (loc == 0) { 80017697Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 80117697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 80217697Smckusick panic("ialloccg: map corrupted"); 80317697Smckusick /* NOTREACHED */ 80417697Smckusick } 80516784Smckusick } 80616784Smckusick i = start + len - loc; 80734143Smckusick map = cg_inosused(cgp)[i]; 80816784Smckusick ipref = i * NBBY; 80916784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 81016784Smckusick if ((map & i) == 0) { 8114359Smckusick cgp->cg_irotor = ipref; 8124359Smckusick goto gotit; 8134359Smckusick } 8144359Smckusick } 81516784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 81616784Smckusick panic("ialloccg: block not in map"); 81716784Smckusick /* NOTREACHED */ 8184359Smckusick gotit: 81934143Smckusick setbit(cg_inosused(cgp), ipref); 8204792Smckusic cgp->cg_cs.cs_nifree--; 8214792Smckusic fs->fs_cstotal.cs_nifree--; 8225322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 8234359Smckusick fs->fs_fmod++; 8244359Smckusick if ((mode & IFMT) == IFDIR) { 8254792Smckusic cgp->cg_cs.cs_ndir++; 8264792Smckusic fs->fs_cstotal.cs_ndir++; 8275322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 8284359Smckusick } 8294359Smckusick bdwrite(bp); 8304359Smckusick return (cg * fs->fs_ipg + ipref); 8314359Smckusick } 8324359Smckusick 8335375Smckusic /* 8345375Smckusic * Free a block or fragment. 8355375Smckusic * 8365375Smckusic * The specified block or fragment is placed back in the 8375375Smckusic * free map. If a fragment is deallocated, a possible 8385375Smckusic * block reassembly is checked. 8395375Smckusic */ 84031402Smckusick blkfree(ip, bno, size) 8415965Smckusic register struct inode *ip; 8424359Smckusick daddr_t bno; 8435212Smckusic off_t size; 8444359Smckusick { 8454359Smckusick register struct fs *fs; 8464359Smckusick register struct cg *cgp; 84737735Smckusick struct buf *bp; 84837735Smckusick int error, cg, blk, frags, bbase; 8494463Smckusic register int i; 850*42318Smckusick struct ucred *cred = u.u_cred; /* XXX */ 8514359Smckusick 8525965Smckusic fs = ip->i_fs; 8536716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 8546716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 8556716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 85631402Smckusick panic("blkfree: bad size"); 8576716Smckusick } 8585377Smckusic cg = dtog(fs, bno); 859*42318Smckusick if ((unsigned)bno >= fs->fs_size) { 8606567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 861*42318Smckusick fserr(fs, cred->cr_uid, "bad block"); 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 */ 1063*42318Smckusick fserr(fs, uid, cp) 10644359Smckusick struct fs *fs; 1065*42318Smckusick uid_t uid; 10664359Smckusick char *cp; 10674359Smckusick { 10684359Smckusick 1069*42318Smckusick log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp); 10704359Smckusick } 1071