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*41309Smckusick * @(#)lfs_alloc.c 7.17 (Berkeley) 05/02/90 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 */ 6239678Smckusick alloc(ip, lbn, bpref, size, bnp) 634463Smckusic register struct inode *ip; 6439678Smckusick daddr_t lbn, bpref; 654359Smckusick int size; 6639678Smckusick daddr_t *bnp; 674359Smckusick { 684359Smckusick daddr_t bno; 694359Smckusick register struct fs *fs; 704463Smckusic register struct buf *bp; 7137735Smckusick int cg, error; 72*41309Smckusick struct ucred *cred = u.u_cred; /* XXX */ 734359Smckusick 7439678Smckusick *bnp = 0; 755965Smckusic fs = ip->i_fs; 766716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 776716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 786716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 794463Smckusic panic("alloc: bad size"); 806716Smckusick } 815322Smckusic if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 824359Smckusick goto nospace; 83*41309Smckusick if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 844792Smckusic goto nospace; 857650Ssam #ifdef QUOTA 86*41309Smckusick if (error = chkdq(ip, (long)btodb(size), cred, 0)) 8737735Smckusick return (error); 887483Skre #endif 894948Smckusic if (bpref >= fs->fs_size) 904948Smckusic bpref = 0; 914359Smckusick if (bpref == 0) 925377Smckusic cg = itog(fs, ip->i_number); 934359Smckusick else 945377Smckusic cg = dtog(fs, bpref); 959163Ssam bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, 969163Ssam (u_long (*)())alloccg); 9739678Smckusick if (bno > 0) { 9839678Smckusick ip->i_blocks += btodb(size); 9939678Smckusick ip->i_flag |= IUPD|ICHG; 10039678Smckusick *bnp = bno; 10139678Smckusick return (0); 10239678Smckusick } 1034359Smckusick nospace: 1044359Smckusick fserr(fs, "file system full"); 1054359Smckusick uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 10637735Smckusick return (ENOSPC); 1074359Smckusick } 1084359Smckusick 1095375Smckusic /* 1105375Smckusic * Reallocate a fragment to a bigger size 1115375Smckusic * 1125375Smckusic * The number and size of the old block is given, and a preference 1135375Smckusic * and new size is also specified. The allocator attempts to extend 1145375Smckusic * the original block. Failing that, the regular block allocator is 1155375Smckusic * invoked to get an appropriate block. 1165375Smckusic */ 11739678Smckusick realloccg(ip, lbprev, bpref, osize, nsize, bpp) 1185965Smckusic register struct inode *ip; 11939678Smckusick off_t lbprev; 12039678Smckusick daddr_t bpref; 1214426Smckusic int osize, nsize; 12237735Smckusick struct buf **bpp; 1234426Smckusic { 1244426Smckusic register struct fs *fs; 12537735Smckusick struct buf *bp, *obp; 12624698Smckusick int cg, request; 12739678Smckusick daddr_t bprev, bno, bn; 12837735Smckusick int i, error, count; 129*41309Smckusick struct ucred *cred = u.u_cred; /* XXX */ 1304426Smckusic 13137735Smckusick *bpp = 0; 1325965Smckusic fs = ip->i_fs; 1335960Smckusic if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 1346716Smckusick (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 1356716Smckusick printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 1366716Smckusick ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 1374463Smckusic panic("realloccg: bad size"); 1386716Smckusick } 139*41309Smckusick if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 1404792Smckusic goto nospace; 14139678Smckusick if ((bprev = ip->i_db[lbprev]) == 0) { 1426716Smckusick printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 1436716Smckusick ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 1444463Smckusic panic("realloccg: bad bprev"); 1456716Smckusick } 1467650Ssam #ifdef QUOTA 147*41309Smckusick if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) 14837735Smckusick return (error); 1497483Skre #endif 15039678Smckusick /* 15139678Smckusick * Allocate the extra space in the buffer. 15239678Smckusick */ 15339678Smckusick if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) { 15439678Smckusick brelse(bp); 15539678Smckusick return (error); 15639678Smckusick } 15739678Smckusick brealloc(bp, nsize); 15839678Smckusick bp->b_flags |= B_DONE; 15939678Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 16039678Smckusick /* 16139678Smckusick * Check for extension in the existing location. 16239678Smckusick */ 1636294Smckusick cg = dtog(fs, bprev); 16439678Smckusick if (bno = fragextend(ip, cg, (long)bprev, osize, nsize)) { 16539887Smckusick if (bp->b_blkno != fsbtodb(fs, bno)) 16639678Smckusick panic("bad blockno"); 16739762Smckusick ip->i_blocks += btodb(nsize - osize); 16839762Smckusick ip->i_flag |= IUPD|ICHG; 16937735Smckusick *bpp = bp; 17037735Smckusick return (0); 1714463Smckusic } 17239678Smckusick /* 17339678Smckusick * Allocate a new disk location. 17439678Smckusick */ 1754948Smckusic if (bpref >= fs->fs_size) 1764948Smckusic bpref = 0; 17726253Skarels switch ((int)fs->fs_optim) { 17825256Smckusick case FS_OPTSPACE: 17925256Smckusick /* 18025256Smckusick * Allocate an exact sized fragment. Although this makes 18125256Smckusick * best use of space, we will waste time relocating it if 18225256Smckusick * the file continues to grow. If the fragmentation is 18325256Smckusick * less than half of the minimum free reserve, we choose 18425256Smckusick * to begin optimizing for time. 18525256Smckusick */ 18624698Smckusick request = nsize; 18725256Smckusick if (fs->fs_minfree < 5 || 18825256Smckusick fs->fs_cstotal.cs_nffree > 18925256Smckusick fs->fs_dsize * fs->fs_minfree / (2 * 100)) 19025256Smckusick break; 19125256Smckusick log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 19225256Smckusick fs->fs_fsmnt); 19325256Smckusick fs->fs_optim = FS_OPTTIME; 19425256Smckusick break; 19525256Smckusick case FS_OPTTIME: 19625256Smckusick /* 19725256Smckusick * At this point we have discovered a file that is trying 19825256Smckusick * to grow a small fragment to a larger fragment. To save 19925256Smckusick * time, we allocate a full sized block, then free the 20025256Smckusick * unused portion. If the file continues to grow, the 20125256Smckusick * `fragextend' call above will be able to grow it in place 20225256Smckusick * without further copying. If aberrant programs cause 20325256Smckusick * disk fragmentation to grow within 2% of the free reserve, 20425256Smckusick * we choose to begin optimizing for space. 20525256Smckusick */ 20624698Smckusick request = fs->fs_bsize; 20725256Smckusick if (fs->fs_cstotal.cs_nffree < 20825256Smckusick fs->fs_dsize * (fs->fs_minfree - 2) / 100) 20925256Smckusick break; 21025256Smckusick log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 21125256Smckusick fs->fs_fsmnt); 21225256Smckusick fs->fs_optim = FS_OPTSPACE; 21325256Smckusick break; 21425256Smckusick default: 21525256Smckusick printf("dev = 0x%x, optim = %d, fs = %s\n", 21625256Smckusick ip->i_dev, fs->fs_optim, fs->fs_fsmnt); 21725256Smckusick panic("realloccg: bad optim"); 21825256Smckusick /* NOTREACHED */ 21925256Smckusick } 22024698Smckusick bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request, 2219163Ssam (u_long (*)())alloccg); 2226567Smckusic if (bno > 0) { 22339678Smckusick bp->b_blkno = bn = fsbtodb(fs, bno); 22430749Skarels count = howmany(osize, CLBYTES); 22530749Skarels for (i = 0; i < count; i++) 22637735Smckusick munhash(ip->i_devvp, bn + i * CLBYTES / DEV_BSIZE); 22731402Smckusick blkfree(ip, bprev, (off_t)osize); 22824698Smckusick if (nsize < request) 22931402Smckusick blkfree(ip, bno + numfrags(fs, nsize), 23024698Smckusick (off_t)(request - nsize)); 23112643Ssam ip->i_blocks += btodb(nsize - osize); 23212643Ssam ip->i_flag |= IUPD|ICHG; 23337735Smckusick *bpp = bp; 23437735Smckusick return (0); 2354463Smckusic } 23639678Smckusick brelse(bp); 2374792Smckusic nospace: 2384463Smckusic /* 2394463Smckusic * no space available 2404463Smckusic */ 2414426Smckusic fserr(fs, "file system full"); 2424426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 24337735Smckusick return (ENOSPC); 2444426Smckusic } 2454426Smckusic 2465375Smckusic /* 2475375Smckusic * Allocate an inode in the file system. 2485375Smckusic * 2495375Smckusic * A preference may be optionally specified. If a preference is given 2505375Smckusic * the following hierarchy is used to allocate an inode: 2515375Smckusic * 1) allocate the requested inode. 2525375Smckusic * 2) allocate an inode in the same cylinder group. 2535375Smckusic * 3) quadradically rehash into other cylinder groups, until an 2545375Smckusic * available inode is located. 2555375Smckusic * If no inode preference is given the following heirarchy is used 2565375Smckusic * to allocate an inode: 2575375Smckusic * 1) allocate an inode in cylinder group 0. 2585375Smckusic * 2) quadradically rehash into other cylinder groups, until an 2595375Smckusic * available inode is located. 2605375Smckusic */ 261*41309Smckusick ialloc(pip, ipref, mode, cred, ipp) 2625965Smckusic register struct inode *pip; 2634359Smckusick ino_t ipref; 2644359Smckusick int mode; 265*41309Smckusick struct ucred *cred; 26637735Smckusick struct inode **ipp; 2674359Smckusick { 2685212Smckusic ino_t ino; 2694359Smckusick register struct fs *fs; 2704359Smckusick register struct inode *ip; 27137735Smckusick int cg, error; 2724359Smckusick 27337735Smckusick *ipp = 0; 2745965Smckusic fs = pip->i_fs; 2754792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 2764359Smckusick goto noinodes; 2774948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 2784948Smckusic ipref = 0; 2795377Smckusic cg = itog(fs, ipref); 2805965Smckusic ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); 2814359Smckusick if (ino == 0) 2824359Smckusick goto noinodes; 28337735Smckusick error = iget(pip, ino, ipp); 28437735Smckusick if (error) { 285*41309Smckusick ifree(pip, ino, mode); 28637735Smckusick return (error); 2874359Smckusick } 28838255Smckusick ip = *ipp; 2896716Smckusick if (ip->i_mode) { 2906716Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 2916716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 2924359Smckusick panic("ialloc: dup alloc"); 2936716Smckusick } 29412643Ssam if (ip->i_blocks) { /* XXX */ 29512643Ssam printf("free inode %s/%d had %d blocks\n", 29612643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 29712643Ssam ip->i_blocks = 0; 29812643Ssam } 29939516Smckusick ip->i_flags = 0; 30038255Smckusick /* 30138255Smckusick * Set up a new generation number for this inode. 30238255Smckusick */ 30338255Smckusick if (++nextgennumber < (u_long)time.tv_sec) 30438255Smckusick nextgennumber = time.tv_sec; 30538255Smckusick ip->i_gen = nextgennumber; 30637735Smckusick return (0); 3074359Smckusick noinodes: 3084359Smckusick fserr(fs, "out of inodes"); 3096294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 31037735Smckusick return (ENOSPC); 3114359Smckusick } 3124359Smckusick 3134651Smckusic /* 3145375Smckusic * Find a cylinder to place a directory. 3155375Smckusic * 3165375Smckusic * The policy implemented by this algorithm is to select from 3175375Smckusic * among those cylinder groups with above the average number of 3185375Smckusic * free inodes, the one with the smallest number of directories. 3194651Smckusic */ 3209163Ssam ino_t 3215965Smckusic dirpref(fs) 3225965Smckusic register struct fs *fs; 3234359Smckusick { 3244651Smckusic int cg, minndir, mincg, avgifree; 3254359Smckusick 3264792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 3274651Smckusic minndir = fs->fs_ipg; 3284359Smckusick mincg = 0; 3294651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 3305322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 3315322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 3324359Smckusick mincg = cg; 3335322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 3344359Smckusick } 3359163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 3364359Smckusick } 3374359Smckusick 3384651Smckusic /* 3399163Ssam * Select the desired position for the next block in a file. The file is 3409163Ssam * logically divided into sections. The first section is composed of the 3419163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 3429163Ssam * 3439163Ssam * If no blocks have been allocated in the first section, the policy is to 3449163Ssam * request a block in the same cylinder group as the inode that describes 3459163Ssam * the file. If no blocks have been allocated in any other section, the 3469163Ssam * policy is to place the section in a cylinder group with a greater than 3479163Ssam * average number of free blocks. An appropriate cylinder group is found 34817696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of 34917696Smckusick * blocks is needed, the sweep begins in the cylinder group following the 35017696Smckusick * cylinder group from which the previous allocation was made. The sweep 35117696Smckusick * continues until a cylinder group with greater than the average number 35217696Smckusick * of free blocks is found. If the allocation is for the first block in an 35317696Smckusick * indirect block, the information on the previous allocation is unavailable; 35417696Smckusick * here a best guess is made based upon the logical block number being 35517696Smckusick * allocated. 3569163Ssam * 3579163Ssam * If a section is already partially allocated, the policy is to 3589163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 3599163Ssam * contiguous blocks and the beginning of the next is physically separated 3609163Ssam * so that the disk head will be in transit between them for at least 3619163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 3629163Ssam * schedule another I/O transfer. 3634651Smckusic */ 3645212Smckusic daddr_t 3659163Ssam blkpref(ip, lbn, indx, bap) 3669163Ssam struct inode *ip; 3679163Ssam daddr_t lbn; 3689163Ssam int indx; 3699163Ssam daddr_t *bap; 3709163Ssam { 3715965Smckusic register struct fs *fs; 37217696Smckusick register int cg; 37317696Smckusick int avgbfree, startcg; 3749163Ssam daddr_t nextblk; 3754651Smckusic 3769163Ssam fs = ip->i_fs; 3779163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 3789163Ssam if (lbn < NDADDR) { 3799163Ssam cg = itog(fs, ip->i_number); 3805322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 3814651Smckusic } 3829163Ssam /* 3839163Ssam * Find a cylinder with greater than average number of 3849163Ssam * unused data blocks. 3859163Ssam */ 38617696Smckusick if (indx == 0 || bap[indx - 1] == 0) 38717696Smckusick startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; 38817696Smckusick else 38917696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1; 39017696Smckusick startcg %= fs->fs_ncg; 3919163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 39217696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++) 3939163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3949163Ssam fs->fs_cgrotor = cg; 3959163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3969163Ssam } 39717696Smckusick for (cg = 0; cg <= startcg; cg++) 3989163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3999163Ssam fs->fs_cgrotor = cg; 4009163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 4019163Ssam } 4029163Ssam return (NULL); 4039163Ssam } 4049163Ssam /* 4059163Ssam * One or more previous blocks have been laid out. If less 4069163Ssam * than fs_maxcontig previous blocks are contiguous, the 4079163Ssam * next block is requested contiguously, otherwise it is 4089163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 4099163Ssam */ 4109163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 4119163Ssam if (indx > fs->fs_maxcontig && 41211638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 4139163Ssam != nextblk) 4149163Ssam return (nextblk); 4159163Ssam if (fs->fs_rotdelay != 0) 4169163Ssam /* 4179163Ssam * Here we convert ms of delay to frags as: 4189163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 4199163Ssam * ((sect/frag) * (ms/sec)) 4209163Ssam * then round up to the next block. 4219163Ssam */ 4229163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 4239163Ssam (NSPF(fs) * 1000), fs->fs_frag); 4249163Ssam return (nextblk); 4254651Smckusic } 4264651Smckusic 4275375Smckusic /* 4285375Smckusic * Implement the cylinder overflow algorithm. 4295375Smckusic * 4305375Smckusic * The policy implemented by this algorithm is: 4315375Smckusic * 1) allocate the block in its requested cylinder group. 4325375Smckusic * 2) quadradically rehash on the cylinder group number. 4335375Smckusic * 3) brute force search for a free block. 4345375Smckusic */ 4355212Smckusic /*VARARGS5*/ 4365212Smckusic u_long 4375965Smckusic hashalloc(ip, cg, pref, size, allocator) 4385965Smckusic struct inode *ip; 4394359Smckusick int cg; 4404359Smckusick long pref; 4414359Smckusick int size; /* size for data blocks, mode for inodes */ 4425212Smckusic u_long (*allocator)(); 4434359Smckusick { 4445965Smckusic register struct fs *fs; 4454359Smckusick long result; 4464359Smckusick int i, icg = cg; 4474359Smckusick 4485965Smckusic fs = ip->i_fs; 4494359Smckusick /* 4504359Smckusick * 1: preferred cylinder group 4514359Smckusick */ 4525965Smckusic result = (*allocator)(ip, cg, pref, size); 4534359Smckusick if (result) 4544359Smckusick return (result); 4554359Smckusick /* 4564359Smckusick * 2: quadratic rehash 4574359Smckusick */ 4584359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 4594359Smckusick cg += i; 4604359Smckusick if (cg >= fs->fs_ncg) 4614359Smckusick cg -= fs->fs_ncg; 4625965Smckusic result = (*allocator)(ip, cg, 0, size); 4634359Smckusick if (result) 4644359Smckusick return (result); 4654359Smckusick } 4664359Smckusick /* 4674359Smckusick * 3: brute force search 46810847Ssam * Note that we start at i == 2, since 0 was checked initially, 46910847Ssam * and 1 is always checked in the quadratic rehash. 4704359Smckusick */ 47110848Smckusick cg = (icg + 2) % fs->fs_ncg; 47210847Ssam for (i = 2; i < fs->fs_ncg; i++) { 4735965Smckusic result = (*allocator)(ip, cg, 0, size); 4744359Smckusick if (result) 4754359Smckusick return (result); 4764359Smckusick cg++; 4774359Smckusick if (cg == fs->fs_ncg) 4784359Smckusick cg = 0; 4794359Smckusick } 4806294Smckusick return (NULL); 4814359Smckusick } 4824359Smckusick 4835375Smckusic /* 4845375Smckusic * Determine whether a fragment can be extended. 4855375Smckusic * 4865375Smckusic * Check to see if the necessary fragments are available, and 4875375Smckusic * if they are, allocate them. 4885375Smckusic */ 4894359Smckusick daddr_t 4905965Smckusic fragextend(ip, cg, bprev, osize, nsize) 4915965Smckusic struct inode *ip; 4924426Smckusic int cg; 4934463Smckusic long bprev; 4944426Smckusic int osize, nsize; 4954426Smckusic { 4965965Smckusic register struct fs *fs; 4974463Smckusic register struct cg *cgp; 49837735Smckusick struct buf *bp; 4994463Smckusic long bno; 5004463Smckusic int frags, bbase; 50137735Smckusick int i, error; 5024426Smckusic 5035965Smckusic fs = ip->i_fs; 50417224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 5056531Smckusick return (NULL); 5065960Smckusic frags = numfrags(fs, nsize); 50717224Smckusick bbase = fragnum(fs, bprev); 50817224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 50930749Skarels /* cannot extend across a block boundary */ 5106294Smckusick return (NULL); 5114463Smckusic } 51237735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 51338776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 51437735Smckusick if (error) { 51537735Smckusick brelse(bp); 51637735Smckusick return (NULL); 51737735Smckusick } 5186531Smckusick cgp = bp->b_un.b_cg; 51937735Smckusick if (!cg_chkmagic(cgp)) { 5205960Smckusic brelse(bp); 5216294Smckusick return (NULL); 5225960Smckusic } 5238105Sroot cgp->cg_time = time.tv_sec; 5245377Smckusic bno = dtogd(fs, bprev); 5255960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 52634143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) { 5275361Smckusic brelse(bp); 5286294Smckusick return (NULL); 5295361Smckusic } 5305361Smckusic /* 5315361Smckusic * the current fragment can be extended 5325361Smckusic * deduct the count on fragment being extended into 5335361Smckusic * increase the count on the remaining fragment (if any) 5345361Smckusic * allocate the extended piece 5355361Smckusic */ 5365361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 53734143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) 5384463Smckusic break; 5395960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 5405361Smckusic if (i != frags) 5415361Smckusic cgp->cg_frsum[i - frags]++; 5425960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 54334143Smckusick clrbit(cg_blksfree(cgp), bno + i); 5445361Smckusic cgp->cg_cs.cs_nffree--; 5455361Smckusic fs->fs_cstotal.cs_nffree--; 5465361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 5474463Smckusic } 5485361Smckusic fs->fs_fmod++; 5495361Smckusic bdwrite(bp); 5505361Smckusic return (bprev); 5514426Smckusic } 5524426Smckusic 5535375Smckusic /* 5545375Smckusic * Determine whether a block can be allocated. 5555375Smckusic * 5565375Smckusic * Check to see if a block of the apprpriate size is available, 5575375Smckusic * and if it is, allocate it. 5585375Smckusic */ 5599163Ssam daddr_t 5605965Smckusic alloccg(ip, cg, bpref, size) 5615965Smckusic struct inode *ip; 5624359Smckusick int cg; 5634359Smckusick daddr_t bpref; 5644359Smckusick int size; 5654359Smckusick { 5665965Smckusic register struct fs *fs; 5674463Smckusic register struct cg *cgp; 56837735Smckusick struct buf *bp; 5694463Smckusic register int i; 57037735Smckusick int error, bno, frags, allocsiz; 5714359Smckusick 5725965Smckusic fs = ip->i_fs; 5735322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 5746294Smckusick return (NULL); 57537735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 57638776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 57737735Smckusick if (error) { 57837735Smckusick brelse(bp); 57937735Smckusick return (NULL); 58037735Smckusick } 5816531Smckusick cgp = bp->b_un.b_cg; 58237735Smckusick if (!cg_chkmagic(cgp) || 58315950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 5845960Smckusic brelse(bp); 5856294Smckusick return (NULL); 5865960Smckusic } 5878105Sroot cgp->cg_time = time.tv_sec; 5885322Smckusic if (size == fs->fs_bsize) { 5895212Smckusic bno = alloccgblk(fs, cgp, bpref); 5904463Smckusic bdwrite(bp); 5914463Smckusic return (bno); 5924463Smckusic } 5934463Smckusic /* 5944463Smckusic * check to see if any fragments are already available 5954463Smckusic * allocsiz is the size which will be allocated, hacking 5964463Smckusic * it down to a smaller size if necessary 5974463Smckusic */ 5985960Smckusic frags = numfrags(fs, size); 5995322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 6004463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 6014463Smckusic break; 6025322Smckusic if (allocsiz == fs->fs_frag) { 6034463Smckusic /* 6044463Smckusic * no fragments were available, so a block will be 6054463Smckusic * allocated, and hacked up 6064463Smckusic */ 6074792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 6084463Smckusic brelse(bp); 6096294Smckusick return (NULL); 6104463Smckusic } 6115212Smckusic bno = alloccgblk(fs, cgp, bpref); 6125377Smckusic bpref = dtogd(fs, bno); 6135322Smckusic for (i = frags; i < fs->fs_frag; i++) 61434143Smckusick setbit(cg_blksfree(cgp), bpref + i); 6155322Smckusic i = fs->fs_frag - frags; 6164792Smckusic cgp->cg_cs.cs_nffree += i; 6174792Smckusic fs->fs_cstotal.cs_nffree += i; 6185322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 6199762Ssam fs->fs_fmod++; 6204463Smckusic cgp->cg_frsum[i]++; 6214463Smckusic bdwrite(bp); 6224463Smckusic return (bno); 6234463Smckusic } 6244651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 62515950Smckusick if (bno < 0) { 62615950Smckusick brelse(bp); 6276294Smckusick return (NULL); 62815950Smckusick } 6294463Smckusic for (i = 0; i < frags; i++) 63034143Smckusick clrbit(cg_blksfree(cgp), bno + i); 6314792Smckusic cgp->cg_cs.cs_nffree -= frags; 6324792Smckusic fs->fs_cstotal.cs_nffree -= frags; 6335322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 6349762Ssam fs->fs_fmod++; 6354463Smckusic cgp->cg_frsum[allocsiz]--; 6364463Smckusic if (frags != allocsiz) 6374463Smckusic cgp->cg_frsum[allocsiz - frags]++; 6384463Smckusic bdwrite(bp); 6394463Smckusic return (cg * fs->fs_fpg + bno); 6404463Smckusic } 6414463Smckusic 6425375Smckusic /* 6435375Smckusic * Allocate a block in a cylinder group. 6445375Smckusic * 6455375Smckusic * This algorithm implements the following policy: 6465375Smckusic * 1) allocate the requested block. 6475375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 6485375Smckusic * 3) allocate the next available block on the block rotor for the 6495375Smckusic * specified cylinder group. 6505375Smckusic * Note that this routine only allocates fs_bsize blocks; these 6515375Smckusic * blocks may be fragmented by the routine that allocates them. 6525375Smckusic */ 6534463Smckusic daddr_t 6545212Smckusic alloccgblk(fs, cgp, bpref) 6555965Smckusic register struct fs *fs; 6564463Smckusic register struct cg *cgp; 6574463Smckusic daddr_t bpref; 6584463Smckusic { 6594651Smckusic daddr_t bno; 6606294Smckusick int cylno, pos, delta; 6614651Smckusic short *cylbp; 6625361Smckusic register int i; 6634463Smckusic 6644651Smckusic if (bpref == 0) { 6654651Smckusic bpref = cgp->cg_rotor; 6665361Smckusic goto norot; 6675361Smckusic } 66817224Smckusick bpref = blknum(fs, bpref); 6695377Smckusic bpref = dtogd(fs, bpref); 6705361Smckusic /* 6715361Smckusic * if the requested block is available, use it 6725361Smckusic */ 67334143Smckusick if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 6745361Smckusic bno = bpref; 6755361Smckusic goto gotit; 6765361Smckusic } 6775361Smckusic /* 6785361Smckusic * check for a block available on the same cylinder 6795361Smckusic */ 6805361Smckusic cylno = cbtocylno(fs, bpref); 68134143Smckusick if (cg_blktot(cgp)[cylno] == 0) 6825375Smckusic goto norot; 6835375Smckusic if (fs->fs_cpc == 0) { 6845375Smckusic /* 6855375Smckusic * block layout info is not available, so just have 6865375Smckusic * to take any block in this cylinder. 6875375Smckusic */ 6885375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 6895375Smckusic goto norot; 6905375Smckusic } 6915375Smckusic /* 6925361Smckusic * check the summary information to see if a block is 6935361Smckusic * available in the requested cylinder starting at the 6949163Ssam * requested rotational position and proceeding around. 6955361Smckusic */ 69634143Smckusick cylbp = cg_blks(fs, cgp, cylno); 6979163Ssam pos = cbtorpos(fs, bpref); 69834143Smckusick for (i = pos; i < fs->fs_nrpos; i++) 6995361Smckusic if (cylbp[i] > 0) 7005361Smckusic break; 70134143Smckusick if (i == fs->fs_nrpos) 7025361Smckusic for (i = 0; i < pos; i++) 7035361Smckusic if (cylbp[i] > 0) 7045361Smckusic break; 7055361Smckusic if (cylbp[i] > 0) { 7064651Smckusic /* 7075361Smckusic * found a rotational position, now find the actual 7085361Smckusic * block. A panic if none is actually there. 7094651Smckusic */ 7105361Smckusic pos = cylno % fs->fs_cpc; 7115361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 71234143Smckusick if (fs_postbl(fs, pos)[i] == -1) { 7136716Smckusick printf("pos = %d, i = %d, fs = %s\n", 7146716Smckusick pos, i, fs->fs_fsmnt); 7155361Smckusic panic("alloccgblk: cyl groups corrupted"); 7166716Smckusick } 71734143Smckusick for (i = fs_postbl(fs, pos)[i];; ) { 71834143Smckusick if (isblock(fs, cg_blksfree(cgp), bno + i)) { 71911638Ssam bno = blkstofrags(fs, (bno + i)); 7205361Smckusic goto gotit; 7215361Smckusic } 72234143Smckusick delta = fs_rotbl(fs)[i]; 72334143Smckusick if (delta <= 0 || 72434143Smckusick delta + i > fragstoblks(fs, fs->fs_fpg)) 7254651Smckusic break; 7266294Smckusick i += delta; 7274651Smckusic } 7286716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 7295361Smckusic panic("alloccgblk: can't find blk in cyl"); 7304359Smckusick } 7315361Smckusic norot: 7325361Smckusic /* 7335361Smckusic * no blocks in the requested cylinder, so take next 7345361Smckusic * available one in this cylinder group. 7355361Smckusic */ 7368628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 7376567Smckusic if (bno < 0) 7386294Smckusick return (NULL); 7394651Smckusic cgp->cg_rotor = bno; 7404359Smckusick gotit: 74134143Smckusick clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno)); 7424792Smckusic cgp->cg_cs.cs_nbfree--; 7434792Smckusic fs->fs_cstotal.cs_nbfree--; 7445322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 7455375Smckusic cylno = cbtocylno(fs, bno); 74634143Smckusick cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 74734143Smckusick cg_blktot(cgp)[cylno]--; 7484359Smckusick fs->fs_fmod++; 7494651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 7504359Smckusick } 75134143Smckusick 7525375Smckusic /* 7535375Smckusic * Determine whether an inode can be allocated. 7545375Smckusic * 7555375Smckusic * Check to see if an inode is available, and if it is, 7565375Smckusic * allocate it using the following policy: 7575375Smckusic * 1) allocate the requested inode. 7585375Smckusic * 2) allocate the next available inode after the requested 7595375Smckusic * inode in the specified cylinder group. 7605375Smckusic */ 7619163Ssam ino_t 7625965Smckusic ialloccg(ip, cg, ipref, mode) 7635965Smckusic struct inode *ip; 7644359Smckusick int cg; 7654359Smckusick daddr_t ipref; 7664359Smckusick int mode; 7674359Smckusick { 7685965Smckusic register struct fs *fs; 7694463Smckusic register struct cg *cgp; 77016784Smckusick struct buf *bp; 77137735Smckusick int error, start, len, loc, map, i; 7724359Smckusick 7735965Smckusic fs = ip->i_fs; 7745322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 7756294Smckusick return (NULL); 77637735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 77738776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 77837735Smckusick if (error) { 77937735Smckusick brelse(bp); 78037735Smckusick return (NULL); 78137735Smckusick } 7826531Smckusick cgp = bp->b_un.b_cg; 78337735Smckusick if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 7845960Smckusic brelse(bp); 7856294Smckusick return (NULL); 7865960Smckusic } 7878105Sroot cgp->cg_time = time.tv_sec; 7884359Smckusick if (ipref) { 7894359Smckusick ipref %= fs->fs_ipg; 79034143Smckusick if (isclr(cg_inosused(cgp), ipref)) 7914359Smckusick goto gotit; 79216784Smckusick } 79316784Smckusick start = cgp->cg_irotor / NBBY; 79416784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 79534143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 79616784Smckusick if (loc == 0) { 79717697Smckusick len = start + 1; 79817697Smckusick start = 0; 79934143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 80017697Smckusick if (loc == 0) { 80117697Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 80217697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 80317697Smckusick panic("ialloccg: map corrupted"); 80417697Smckusick /* NOTREACHED */ 80517697Smckusick } 80616784Smckusick } 80716784Smckusick i = start + len - loc; 80834143Smckusick map = cg_inosused(cgp)[i]; 80916784Smckusick ipref = i * NBBY; 81016784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 81116784Smckusick if ((map & i) == 0) { 8124359Smckusick cgp->cg_irotor = ipref; 8134359Smckusick goto gotit; 8144359Smckusick } 8154359Smckusick } 81616784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 81716784Smckusick panic("ialloccg: block not in map"); 81816784Smckusick /* NOTREACHED */ 8194359Smckusick gotit: 82034143Smckusick setbit(cg_inosused(cgp), ipref); 8214792Smckusic cgp->cg_cs.cs_nifree--; 8224792Smckusic fs->fs_cstotal.cs_nifree--; 8235322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 8244359Smckusick fs->fs_fmod++; 8254359Smckusick if ((mode & IFMT) == IFDIR) { 8264792Smckusic cgp->cg_cs.cs_ndir++; 8274792Smckusic fs->fs_cstotal.cs_ndir++; 8285322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 8294359Smckusick } 8304359Smckusick bdwrite(bp); 8314359Smckusick return (cg * fs->fs_ipg + ipref); 8324359Smckusick } 8334359Smckusick 8345375Smckusic /* 8355375Smckusic * Free a block or fragment. 8365375Smckusic * 8375375Smckusic * The specified block or fragment is placed back in the 8385375Smckusic * free map. If a fragment is deallocated, a possible 8395375Smckusic * block reassembly is checked. 8405375Smckusic */ 84131402Smckusick blkfree(ip, bno, size) 8425965Smckusic register struct inode *ip; 8434359Smckusick daddr_t bno; 8445212Smckusic off_t size; 8454359Smckusick { 8464359Smckusick register struct fs *fs; 8474359Smckusick register struct cg *cgp; 84837735Smckusick struct buf *bp; 84937735Smckusick int error, cg, blk, frags, bbase; 8504463Smckusic register int i; 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); 8596567Smckusic if (badblock(fs, bno)) { 8606567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 8614359Smckusick return; 8626567Smckusic } 86337735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 86438776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 86537735Smckusick if (error) { 86637735Smckusick brelse(bp); 86737735Smckusick return; 86837735Smckusick } 8696531Smckusick cgp = bp->b_un.b_cg; 87037735Smckusick if (!cg_chkmagic(cgp)) { 8715960Smckusic brelse(bp); 8724359Smckusick return; 8735960Smckusic } 8748105Sroot cgp->cg_time = time.tv_sec; 8755377Smckusic bno = dtogd(fs, bno); 8765322Smckusic if (size == fs->fs_bsize) { 87734143Smckusick if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) { 8786716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 8796716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 88031402Smckusick panic("blkfree: freeing free block"); 8816567Smckusic } 88234143Smckusick setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno)); 8834792Smckusic cgp->cg_cs.cs_nbfree++; 8844792Smckusic fs->fs_cstotal.cs_nbfree++; 8855322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 8865375Smckusic i = cbtocylno(fs, bno); 88734143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; 88834143Smckusick cg_blktot(cgp)[i]++; 8894426Smckusic } else { 89017224Smckusick bbase = bno - fragnum(fs, bno); 8914463Smckusic /* 8924463Smckusic * decrement the counts associated with the old frags 8934463Smckusic */ 89434143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 8955322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 8964463Smckusic /* 8974463Smckusic * deallocate the fragment 8984463Smckusic */ 8995960Smckusic frags = numfrags(fs, size); 9004463Smckusic for (i = 0; i < frags; i++) { 90134143Smckusick if (isset(cg_blksfree(cgp), bno + i)) { 9026716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 9036716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 90431402Smckusick panic("blkfree: freeing free frag"); 9056716Smckusick } 90634143Smckusick setbit(cg_blksfree(cgp), bno + i); 9074426Smckusic } 9086294Smckusick cgp->cg_cs.cs_nffree += i; 9096294Smckusick fs->fs_cstotal.cs_nffree += i; 9106294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 9114463Smckusic /* 9124463Smckusic * add back in counts associated with the new frags 9134463Smckusic */ 91434143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase); 9155322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 9164463Smckusic /* 9174463Smckusic * if a complete block has been reassembled, account for it 9184463Smckusic */ 91934476Smckusick if (isblock(fs, cg_blksfree(cgp), 92034476Smckusick (daddr_t)fragstoblks(fs, bbase))) { 9215322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 9225322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 9235322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 9244792Smckusic cgp->cg_cs.cs_nbfree++; 9254792Smckusic fs->fs_cstotal.cs_nbfree++; 9265322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 9275375Smckusic i = cbtocylno(fs, bbase); 92834143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 92934143Smckusick cg_blktot(cgp)[i]++; 9304426Smckusic } 9314426Smckusic } 9324359Smckusick fs->fs_fmod++; 9334359Smckusick bdwrite(bp); 9344359Smckusick } 9354359Smckusick 9365375Smckusic /* 9375375Smckusic * Free an inode. 9385375Smckusic * 9395375Smckusic * The specified inode is placed back in the free map. 9405375Smckusic */ 9415965Smckusic ifree(ip, ino, mode) 9425965Smckusic struct inode *ip; 9434359Smckusick ino_t ino; 9444359Smckusick int mode; 9454359Smckusick { 9464359Smckusick register struct fs *fs; 9474359Smckusick register struct cg *cgp; 94837735Smckusick struct buf *bp; 94937735Smckusick int error, cg; 9504359Smckusick 9515965Smckusic fs = ip->i_fs; 9526716Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 9536716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9546716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9554359Smckusick panic("ifree: range"); 9566716Smckusick } 9575377Smckusic cg = itog(fs, ino); 95837735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 95938776Smckusick (int)fs->fs_cgsize, NOCRED, &bp); 96037735Smckusick if (error) { 96137735Smckusick brelse(bp); 96237735Smckusick return; 96337735Smckusick } 9646531Smckusick cgp = bp->b_un.b_cg; 96537735Smckusick if (!cg_chkmagic(cgp)) { 9665960Smckusic brelse(bp); 9674359Smckusick return; 9685960Smckusic } 9698105Sroot cgp->cg_time = time.tv_sec; 9704359Smckusick ino %= fs->fs_ipg; 97134143Smckusick if (isclr(cg_inosused(cgp), ino)) { 9726716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9736716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9744359Smckusick panic("ifree: freeing free inode"); 9756716Smckusick } 97634143Smckusick clrbit(cg_inosused(cgp), ino); 97716784Smckusick if (ino < cgp->cg_irotor) 97816784Smckusick cgp->cg_irotor = ino; 9794792Smckusic cgp->cg_cs.cs_nifree++; 9804792Smckusic fs->fs_cstotal.cs_nifree++; 9815322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 9824359Smckusick if ((mode & IFMT) == IFDIR) { 9834792Smckusic cgp->cg_cs.cs_ndir--; 9844792Smckusic fs->fs_cstotal.cs_ndir--; 9855322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 9864359Smckusick } 9874359Smckusick fs->fs_fmod++; 9884359Smckusick bdwrite(bp); 9894359Smckusick } 9904359Smckusick 9914463Smckusic /* 9925375Smckusic * Find a block of the specified size in the specified cylinder group. 9935375Smckusic * 9944651Smckusic * It is a panic if a request is made to find a block if none are 9954651Smckusic * available. 9964651Smckusic */ 9974651Smckusic daddr_t 9984651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 9994651Smckusic register struct fs *fs; 10004651Smckusic register struct cg *cgp; 10014651Smckusic daddr_t bpref; 10024651Smckusic int allocsiz; 10034651Smckusic { 10044651Smckusic daddr_t bno; 10054651Smckusic int start, len, loc, i; 10064651Smckusic int blk, field, subfield, pos; 10074651Smckusic 10084651Smckusic /* 10094651Smckusic * find the fragment by searching through the free block 10104651Smckusic * map for an appropriate bit pattern 10114651Smckusic */ 10124651Smckusic if (bpref) 10135377Smckusic start = dtogd(fs, bpref) / NBBY; 10144651Smckusic else 10154651Smckusic start = cgp->cg_frotor / NBBY; 10165398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 101734476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start], 101834476Smckusick (u_char *)fragtbl[fs->fs_frag], 101934476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 10204651Smckusic if (loc == 0) { 10216531Smckusick len = start + 1; 10226531Smckusick start = 0; 102334476Smckusick loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0], 102434476Smckusick (u_char *)fragtbl[fs->fs_frag], 102534476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 102616784Smckusick if (loc == 0) { 102716784Smckusick printf("start = %d, len = %d, fs = %s\n", 102816784Smckusick start, len, fs->fs_fsmnt); 102916784Smckusick panic("alloccg: map corrupted"); 103017697Smckusick /* NOTREACHED */ 103116784Smckusick } 10324651Smckusic } 10334651Smckusic bno = (start + len - loc) * NBBY; 10344651Smckusic cgp->cg_frotor = bno; 10354651Smckusic /* 10364651Smckusic * found the byte in the map 10374651Smckusic * sift through the bits to find the selected frag 10384651Smckusic */ 10396294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 104034143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bno); 10414651Smckusic blk <<= 1; 10424651Smckusic field = around[allocsiz]; 10434651Smckusic subfield = inside[allocsiz]; 10445322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 10456294Smckusick if ((blk & field) == subfield) 10466294Smckusick return (bno + pos); 10474651Smckusic field <<= 1; 10484651Smckusic subfield <<= 1; 10494651Smckusic } 10504651Smckusic } 10516716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 10524651Smckusic panic("alloccg: block not in map"); 10536531Smckusick return (-1); 10544651Smckusic } 10554651Smckusic 10564651Smckusic /* 105739818Smckusick * Check that a specified block number is in range. 105839818Smckusick */ 105939818Smckusick badblock(fs, bn) 106039818Smckusick register struct fs *fs; 106139818Smckusick daddr_t bn; 106239818Smckusick { 106339818Smckusick 106439818Smckusick if ((unsigned)bn >= fs->fs_size) { 106539818Smckusick printf("bad block %d, ", bn); 106639818Smckusick fserr(fs, "bad block"); 106739818Smckusick return (1); 106839818Smckusick } 106939818Smckusick return (0); 107039818Smckusick } 107139818Smckusick 107239818Smckusick /* 10735375Smckusic * Fserr prints the name of a file system with an error diagnostic. 10745375Smckusic * 10755375Smckusic * The form of the error message is: 10764359Smckusick * fs: error message 10774359Smckusick */ 10784359Smckusick fserr(fs, cp) 10794359Smckusick struct fs *fs; 10804359Smckusick char *cp; 10814359Smckusick { 10824359Smckusick 108324839Seric log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp); 10844359Smckusick } 1085