123394Smckusick /* 229113Smckusick * Copyright (c) 1982, 1986 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*31402Smckusick * @(#)lfs_alloc.c 7.3 (Berkeley) 06/05/87 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" 2026253Skarels #include "cmap.h" 214359Smckusick 225212Smckusic extern u_long hashalloc(); 239163Ssam extern ino_t ialloccg(); 249163Ssam extern daddr_t alloccg(); 254651Smckusic extern daddr_t alloccgblk(); 264651Smckusic extern daddr_t fragextend(); 274651Smckusic extern daddr_t blkpref(); 284651Smckusic extern daddr_t mapsearch(); 294607Smckusic extern int inside[], around[]; 305322Smckusic extern unsigned char *fragtbl[]; 314359Smckusick 325375Smckusic /* 335375Smckusic * Allocate a block in the file system. 345375Smckusic * 355375Smckusic * The size of the requested block is given, which must be some 365375Smckusic * multiple of fs_fsize and <= fs_bsize. 375375Smckusic * A preference may be optionally specified. If a preference is given 385375Smckusic * the following hierarchy is used to allocate a block: 395375Smckusic * 1) allocate the requested block. 405375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 415375Smckusic * 3) allocate a block in the same cylinder group. 425375Smckusic * 4) quadradically rehash into other cylinder groups, until an 435375Smckusic * available block is located. 445375Smckusic * If no block preference is given the following heirarchy is used 455375Smckusic * to allocate a block: 465375Smckusic * 1) allocate a block in the cylinder group that contains the 475375Smckusic * inode for the file. 485375Smckusic * 2) quadradically rehash into other cylinder groups, until an 495375Smckusic * available block is located. 505375Smckusic */ 514359Smckusick struct buf * 525965Smckusic alloc(ip, bpref, size) 534463Smckusic register struct inode *ip; 544359Smckusick daddr_t bpref; 554359Smckusick int size; 564359Smckusick { 574359Smckusick daddr_t bno; 584359Smckusick register struct fs *fs; 594463Smckusic register struct buf *bp; 604359Smckusick int cg; 614359Smckusick 625965Smckusic fs = ip->i_fs; 636716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 646716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 656716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 664463Smckusic panic("alloc: bad size"); 676716Smckusick } 685322Smckusic if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 694359Smckusick goto nospace; 7011638Ssam if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 714792Smckusic goto nospace; 727650Ssam #ifdef QUOTA 7312643Ssam u.u_error = chkdq(ip, (long)btodb(size), 0); 7412643Ssam if (u.u_error) 7512643Ssam return (NULL); 767483Skre #endif 774948Smckusic if (bpref >= fs->fs_size) 784948Smckusic bpref = 0; 794359Smckusick if (bpref == 0) 805377Smckusic cg = itog(fs, ip->i_number); 814359Smckusick else 825377Smckusic cg = dtog(fs, bpref); 839163Ssam bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, 849163Ssam (u_long (*)())alloccg); 856567Smckusic if (bno <= 0) 864359Smckusick goto nospace; 8712643Ssam ip->i_blocks += btodb(size); 8812643Ssam ip->i_flag |= IUPD|ICHG; 895965Smckusic bp = getblk(ip->i_dev, fsbtodb(fs, bno), size); 904359Smckusick clrbuf(bp); 914359Smckusick return (bp); 924359Smckusick nospace: 934359Smckusick fserr(fs, "file system full"); 944359Smckusick uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 954359Smckusick u.u_error = ENOSPC; 964359Smckusick return (NULL); 974359Smckusick } 984359Smckusick 995375Smckusic /* 1005375Smckusic * Reallocate a fragment to a bigger size 1015375Smckusic * 1025375Smckusic * The number and size of the old block is given, and a preference 1035375Smckusic * and new size is also specified. The allocator attempts to extend 1045375Smckusic * the original block. Failing that, the regular block allocator is 1055375Smckusic * invoked to get an appropriate block. 1065375Smckusic */ 1074426Smckusic struct buf * 1085965Smckusic realloccg(ip, bprev, bpref, osize, nsize) 1095965Smckusic register struct inode *ip; 1104651Smckusic daddr_t bprev, bpref; 1114426Smckusic int osize, nsize; 1124426Smckusic { 1134426Smckusic register struct fs *fs; 1144463Smckusic register struct buf *bp, *obp; 11524698Smckusick int cg, request; 11625471Smckusick daddr_t bno, bn; 11725471Smckusick int i, count, s; 11825471Smckusick extern struct cmap *mfind(); 1194426Smckusic 1205965Smckusic fs = ip->i_fs; 1215960Smckusic if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 1226716Smckusick (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 1236716Smckusick printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 1246716Smckusick ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 1254463Smckusic panic("realloccg: bad size"); 1266716Smckusick } 12711638Ssam if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 1284792Smckusic goto nospace; 1296716Smckusick if (bprev == 0) { 1306716Smckusick printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 1316716Smckusick ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 1324463Smckusic panic("realloccg: bad bprev"); 1336716Smckusick } 1347650Ssam #ifdef QUOTA 13512643Ssam u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0); 13612643Ssam if (u.u_error) 13712643Ssam return (NULL); 1387483Skre #endif 1396294Smckusick cg = dtog(fs, bprev); 1405965Smckusic bno = fragextend(ip, cg, (long)bprev, osize, nsize); 1414463Smckusic if (bno != 0) { 1427187Sroot do { 1437187Sroot bp = bread(ip->i_dev, fsbtodb(fs, bno), osize); 1447187Sroot if (bp->b_flags & B_ERROR) { 1457187Sroot brelse(bp); 1467187Sroot return (NULL); 1477187Sroot } 1487187Sroot } while (brealloc(bp, nsize) == 0); 1497187Sroot bp->b_flags |= B_DONE; 1509163Ssam bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 15112643Ssam ip->i_blocks += btodb(nsize - osize); 15212643Ssam ip->i_flag |= IUPD|ICHG; 1534463Smckusic return (bp); 1544463Smckusic } 1554948Smckusic if (bpref >= fs->fs_size) 1564948Smckusic bpref = 0; 15726253Skarels switch ((int)fs->fs_optim) { 15825256Smckusick case FS_OPTSPACE: 15925256Smckusick /* 16025256Smckusick * Allocate an exact sized fragment. Although this makes 16125256Smckusick * best use of space, we will waste time relocating it if 16225256Smckusick * the file continues to grow. If the fragmentation is 16325256Smckusick * less than half of the minimum free reserve, we choose 16425256Smckusick * to begin optimizing for time. 16525256Smckusick */ 16624698Smckusick request = nsize; 16725256Smckusick if (fs->fs_minfree < 5 || 16825256Smckusick fs->fs_cstotal.cs_nffree > 16925256Smckusick fs->fs_dsize * fs->fs_minfree / (2 * 100)) 17025256Smckusick break; 17125256Smckusick log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 17225256Smckusick fs->fs_fsmnt); 17325256Smckusick fs->fs_optim = FS_OPTTIME; 17425256Smckusick break; 17525256Smckusick case FS_OPTTIME: 17625256Smckusick /* 17725256Smckusick * At this point we have discovered a file that is trying 17825256Smckusick * to grow a small fragment to a larger fragment. To save 17925256Smckusick * time, we allocate a full sized block, then free the 18025256Smckusick * unused portion. If the file continues to grow, the 18125256Smckusick * `fragextend' call above will be able to grow it in place 18225256Smckusick * without further copying. If aberrant programs cause 18325256Smckusick * disk fragmentation to grow within 2% of the free reserve, 18425256Smckusick * we choose to begin optimizing for space. 18525256Smckusick */ 18624698Smckusick request = fs->fs_bsize; 18725256Smckusick if (fs->fs_cstotal.cs_nffree < 18825256Smckusick fs->fs_dsize * (fs->fs_minfree - 2) / 100) 18925256Smckusick break; 19025256Smckusick log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 19125256Smckusick fs->fs_fsmnt); 19225256Smckusick fs->fs_optim = FS_OPTSPACE; 19325256Smckusick break; 19425256Smckusick default: 19525256Smckusick printf("dev = 0x%x, optim = %d, fs = %s\n", 19625256Smckusick ip->i_dev, fs->fs_optim, fs->fs_fsmnt); 19725256Smckusick panic("realloccg: bad optim"); 19825256Smckusick /* NOTREACHED */ 19925256Smckusick } 20024698Smckusick bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request, 2019163Ssam (u_long (*)())alloccg); 2026567Smckusic if (bno > 0) { 2035965Smckusic obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize); 2045960Smckusic if (obp->b_flags & B_ERROR) { 2055960Smckusic brelse(obp); 2066294Smckusick return (NULL); 2075960Smckusic } 20825471Smckusick bn = fsbtodb(fs, bno); 20925471Smckusick bp = getblk(ip->i_dev, bn, nsize); 21017658Smckusick bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize); 21130749Skarels count = howmany(osize, CLBYTES); 21230749Skarels for (i = 0; i < count; i++) 21330749Skarels munhash(ip->i_dev, bn + i * CLBYTES / DEV_BSIZE); 21417658Smckusick bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 21517658Smckusick if (obp->b_flags & B_DELWRI) { 21617658Smckusick obp->b_flags &= ~B_DELWRI; 21717658Smckusick u.u_ru.ru_oublock--; /* delete charge */ 21817658Smckusick } 2194463Smckusic brelse(obp); 220*31402Smckusick blkfree(ip, bprev, (off_t)osize); 22124698Smckusick if (nsize < request) 222*31402Smckusick blkfree(ip, bno + numfrags(fs, nsize), 22324698Smckusick (off_t)(request - nsize)); 22412643Ssam ip->i_blocks += btodb(nsize - osize); 22512643Ssam ip->i_flag |= IUPD|ICHG; 2266294Smckusick return (bp); 2274463Smckusic } 2284792Smckusic nospace: 2294463Smckusic /* 2304463Smckusic * no space available 2314463Smckusic */ 2324426Smckusic fserr(fs, "file system full"); 2334426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 2344426Smckusic u.u_error = ENOSPC; 2354426Smckusic return (NULL); 2364426Smckusic } 2374426Smckusic 2385375Smckusic /* 2395375Smckusic * Allocate an inode in the file system. 2405375Smckusic * 2415375Smckusic * A preference may be optionally specified. If a preference is given 2425375Smckusic * the following hierarchy is used to allocate an inode: 2435375Smckusic * 1) allocate the requested inode. 2445375Smckusic * 2) allocate an inode in the same cylinder group. 2455375Smckusic * 3) quadradically rehash into other cylinder groups, until an 2465375Smckusic * available inode is located. 2475375Smckusic * If no inode preference is given the following heirarchy is used 2485375Smckusic * to allocate an inode: 2495375Smckusic * 1) allocate an inode in cylinder group 0. 2505375Smckusic * 2) quadradically rehash into other cylinder groups, until an 2515375Smckusic * available inode is located. 2525375Smckusic */ 2534359Smckusick struct inode * 2545965Smckusic ialloc(pip, ipref, mode) 2555965Smckusic register struct inode *pip; 2564359Smckusick ino_t ipref; 2574359Smckusick int mode; 2584359Smckusick { 2595212Smckusic ino_t ino; 2604359Smckusick register struct fs *fs; 2614359Smckusick register struct inode *ip; 2624359Smckusick int cg; 2634359Smckusick 2645965Smckusic fs = pip->i_fs; 2654792Smckusic if (fs->fs_cstotal.cs_nifree == 0) 2664359Smckusick goto noinodes; 2677650Ssam #ifdef QUOTA 26812643Ssam u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0); 26912643Ssam if (u.u_error) 27012643Ssam return (NULL); 2717483Skre #endif 2724948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg) 2734948Smckusic ipref = 0; 2745377Smckusic cg = itog(fs, ipref); 2755965Smckusic ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); 2764359Smckusick if (ino == 0) 2774359Smckusick goto noinodes; 2785965Smckusic ip = iget(pip->i_dev, pip->i_fs, ino); 2794359Smckusick if (ip == NULL) { 28015120Skarels ifree(pip, ino, 0); 2814359Smckusick return (NULL); 2824359Smckusick } 2836716Smckusick if (ip->i_mode) { 2846716Smckusick printf("mode = 0%o, inum = %d, fs = %s\n", 2856716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt); 2864359Smckusick panic("ialloc: dup alloc"); 2876716Smckusick } 28812643Ssam if (ip->i_blocks) { /* XXX */ 28912643Ssam printf("free inode %s/%d had %d blocks\n", 29012643Ssam fs->fs_fsmnt, ino, ip->i_blocks); 29112643Ssam ip->i_blocks = 0; 29212643Ssam } 2934359Smckusick return (ip); 2944359Smckusick noinodes: 2954359Smckusick fserr(fs, "out of inodes"); 2966294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 2974359Smckusick u.u_error = ENOSPC; 2984359Smckusick return (NULL); 2994359Smckusick } 3004359Smckusick 3014651Smckusic /* 3025375Smckusic * Find a cylinder to place a directory. 3035375Smckusic * 3045375Smckusic * The policy implemented by this algorithm is to select from 3055375Smckusic * among those cylinder groups with above the average number of 3065375Smckusic * free inodes, the one with the smallest number of directories. 3074651Smckusic */ 3089163Ssam ino_t 3095965Smckusic dirpref(fs) 3105965Smckusic register struct fs *fs; 3114359Smckusick { 3124651Smckusic int cg, minndir, mincg, avgifree; 3134359Smckusick 3144792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 3154651Smckusic minndir = fs->fs_ipg; 3164359Smckusick mincg = 0; 3174651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++) 3185322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir && 3195322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 3204359Smckusick mincg = cg; 3215322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir; 3224359Smckusick } 3239163Ssam return ((ino_t)(fs->fs_ipg * mincg)); 3244359Smckusick } 3254359Smckusick 3264651Smckusic /* 3279163Ssam * Select the desired position for the next block in a file. The file is 3289163Ssam * logically divided into sections. The first section is composed of the 3299163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks. 3309163Ssam * 3319163Ssam * If no blocks have been allocated in the first section, the policy is to 3329163Ssam * request a block in the same cylinder group as the inode that describes 3339163Ssam * the file. If no blocks have been allocated in any other section, the 3349163Ssam * policy is to place the section in a cylinder group with a greater than 3359163Ssam * average number of free blocks. An appropriate cylinder group is found 33617696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of 33717696Smckusick * blocks is needed, the sweep begins in the cylinder group following the 33817696Smckusick * cylinder group from which the previous allocation was made. The sweep 33917696Smckusick * continues until a cylinder group with greater than the average number 34017696Smckusick * of free blocks is found. If the allocation is for the first block in an 34117696Smckusick * indirect block, the information on the previous allocation is unavailable; 34217696Smckusick * here a best guess is made based upon the logical block number being 34317696Smckusick * allocated. 3449163Ssam * 3459163Ssam * If a section is already partially allocated, the policy is to 3469163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these 3479163Ssam * contiguous blocks and the beginning of the next is physically separated 3489163Ssam * so that the disk head will be in transit between them for at least 3499163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to 3509163Ssam * schedule another I/O transfer. 3514651Smckusic */ 3525212Smckusic daddr_t 3539163Ssam blkpref(ip, lbn, indx, bap) 3549163Ssam struct inode *ip; 3559163Ssam daddr_t lbn; 3569163Ssam int indx; 3579163Ssam daddr_t *bap; 3589163Ssam { 3595965Smckusic register struct fs *fs; 36017696Smckusick register int cg; 36117696Smckusick int avgbfree, startcg; 3629163Ssam daddr_t nextblk; 3634651Smckusic 3649163Ssam fs = ip->i_fs; 3659163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 3669163Ssam if (lbn < NDADDR) { 3679163Ssam cg = itog(fs, ip->i_number); 3685322Smckusic return (fs->fs_fpg * cg + fs->fs_frag); 3694651Smckusic } 3709163Ssam /* 3719163Ssam * Find a cylinder with greater than average number of 3729163Ssam * unused data blocks. 3739163Ssam */ 37417696Smckusick if (indx == 0 || bap[indx - 1] == 0) 37517696Smckusick startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; 37617696Smckusick else 37717696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1; 37817696Smckusick startcg %= fs->fs_ncg; 3799163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 38017696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++) 3819163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3829163Ssam fs->fs_cgrotor = cg; 3839163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3849163Ssam } 38517696Smckusick for (cg = 0; cg <= startcg; cg++) 3869163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 3879163Ssam fs->fs_cgrotor = cg; 3889163Ssam return (fs->fs_fpg * cg + fs->fs_frag); 3899163Ssam } 3909163Ssam return (NULL); 3919163Ssam } 3929163Ssam /* 3939163Ssam * One or more previous blocks have been laid out. If less 3949163Ssam * than fs_maxcontig previous blocks are contiguous, the 3959163Ssam * next block is requested contiguously, otherwise it is 3969163Ssam * requested rotationally delayed by fs_rotdelay milliseconds. 3979163Ssam */ 3989163Ssam nextblk = bap[indx - 1] + fs->fs_frag; 3999163Ssam if (indx > fs->fs_maxcontig && 40011638Ssam bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 4019163Ssam != nextblk) 4029163Ssam return (nextblk); 4039163Ssam if (fs->fs_rotdelay != 0) 4049163Ssam /* 4059163Ssam * Here we convert ms of delay to frags as: 4069163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) / 4079163Ssam * ((sect/frag) * (ms/sec)) 4089163Ssam * then round up to the next block. 4099163Ssam */ 4109163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 4119163Ssam (NSPF(fs) * 1000), fs->fs_frag); 4129163Ssam return (nextblk); 4134651Smckusic } 4144651Smckusic 4155375Smckusic /* 4165375Smckusic * Implement the cylinder overflow algorithm. 4175375Smckusic * 4185375Smckusic * The policy implemented by this algorithm is: 4195375Smckusic * 1) allocate the block in its requested cylinder group. 4205375Smckusic * 2) quadradically rehash on the cylinder group number. 4215375Smckusic * 3) brute force search for a free block. 4225375Smckusic */ 4235212Smckusic /*VARARGS5*/ 4245212Smckusic u_long 4255965Smckusic hashalloc(ip, cg, pref, size, allocator) 4265965Smckusic struct inode *ip; 4274359Smckusick int cg; 4284359Smckusick long pref; 4294359Smckusick int size; /* size for data blocks, mode for inodes */ 4305212Smckusic u_long (*allocator)(); 4314359Smckusick { 4325965Smckusic register struct fs *fs; 4334359Smckusick long result; 4344359Smckusick int i, icg = cg; 4354359Smckusick 4365965Smckusic fs = ip->i_fs; 4374359Smckusick /* 4384359Smckusick * 1: preferred cylinder group 4394359Smckusick */ 4405965Smckusic result = (*allocator)(ip, cg, pref, size); 4414359Smckusick if (result) 4424359Smckusick return (result); 4434359Smckusick /* 4444359Smckusick * 2: quadratic rehash 4454359Smckusick */ 4464359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) { 4474359Smckusick cg += i; 4484359Smckusick if (cg >= fs->fs_ncg) 4494359Smckusick cg -= fs->fs_ncg; 4505965Smckusic result = (*allocator)(ip, cg, 0, size); 4514359Smckusick if (result) 4524359Smckusick return (result); 4534359Smckusick } 4544359Smckusick /* 4554359Smckusick * 3: brute force search 45610847Ssam * Note that we start at i == 2, since 0 was checked initially, 45710847Ssam * and 1 is always checked in the quadratic rehash. 4584359Smckusick */ 45910848Smckusick cg = (icg + 2) % fs->fs_ncg; 46010847Ssam for (i = 2; i < fs->fs_ncg; i++) { 4615965Smckusic result = (*allocator)(ip, cg, 0, size); 4624359Smckusick if (result) 4634359Smckusick return (result); 4644359Smckusick cg++; 4654359Smckusick if (cg == fs->fs_ncg) 4664359Smckusick cg = 0; 4674359Smckusick } 4686294Smckusick return (NULL); 4694359Smckusick } 4704359Smckusick 4715375Smckusic /* 4725375Smckusic * Determine whether a fragment can be extended. 4735375Smckusic * 4745375Smckusic * Check to see if the necessary fragments are available, and 4755375Smckusic * if they are, allocate them. 4765375Smckusic */ 4774359Smckusick daddr_t 4785965Smckusic fragextend(ip, cg, bprev, osize, nsize) 4795965Smckusic struct inode *ip; 4804426Smckusic int cg; 4814463Smckusic long bprev; 4824426Smckusic int osize, nsize; 4834426Smckusic { 4845965Smckusic register struct fs *fs; 4854463Smckusic register struct buf *bp; 4864463Smckusic register struct cg *cgp; 4874463Smckusic long bno; 4884463Smckusic int frags, bbase; 4894426Smckusic int i; 4904426Smckusic 4915965Smckusic fs = ip->i_fs; 49217224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 4936531Smckusick return (NULL); 4945960Smckusic frags = numfrags(fs, nsize); 49517224Smckusick bbase = fragnum(fs, bprev); 49617224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) { 49730749Skarels /* cannot extend across a block boundary */ 4986294Smckusick return (NULL); 4994463Smckusic } 50010278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 5016531Smckusick cgp = bp->b_un.b_cg; 5026531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 5035960Smckusic brelse(bp); 5046294Smckusick return (NULL); 5055960Smckusic } 5068105Sroot cgp->cg_time = time.tv_sec; 5075377Smckusic bno = dtogd(fs, bprev); 5085960Smckusic for (i = numfrags(fs, osize); i < frags; i++) 5095361Smckusic if (isclr(cgp->cg_free, bno + i)) { 5105361Smckusic brelse(bp); 5116294Smckusick return (NULL); 5125361Smckusic } 5135361Smckusic /* 5145361Smckusic * the current fragment can be extended 5155361Smckusic * deduct the count on fragment being extended into 5165361Smckusic * increase the count on the remaining fragment (if any) 5175361Smckusic * allocate the extended piece 5185361Smckusic */ 5195361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++) 5204463Smckusic if (isclr(cgp->cg_free, bno + i)) 5214463Smckusic break; 5225960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--; 5235361Smckusic if (i != frags) 5245361Smckusic cgp->cg_frsum[i - frags]++; 5255960Smckusic for (i = numfrags(fs, osize); i < frags; i++) { 5265361Smckusic clrbit(cgp->cg_free, bno + i); 5275361Smckusic cgp->cg_cs.cs_nffree--; 5285361Smckusic fs->fs_cstotal.cs_nffree--; 5295361Smckusic fs->fs_cs(fs, cg).cs_nffree--; 5304463Smckusic } 5315361Smckusic fs->fs_fmod++; 5325361Smckusic bdwrite(bp); 5335361Smckusic return (bprev); 5344426Smckusic } 5354426Smckusic 5365375Smckusic /* 5375375Smckusic * Determine whether a block can be allocated. 5385375Smckusic * 5395375Smckusic * Check to see if a block of the apprpriate size is available, 5405375Smckusic * and if it is, allocate it. 5415375Smckusic */ 5429163Ssam daddr_t 5435965Smckusic alloccg(ip, cg, bpref, size) 5445965Smckusic struct inode *ip; 5454359Smckusick int cg; 5464359Smckusick daddr_t bpref; 5474359Smckusick int size; 5484359Smckusick { 5495965Smckusic register struct fs *fs; 5504463Smckusic register struct buf *bp; 5514463Smckusic register struct cg *cgp; 5524463Smckusic int bno, frags; 5534463Smckusic int allocsiz; 5544463Smckusic register int i; 5554359Smckusick 5565965Smckusic fs = ip->i_fs; 5575322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 5586294Smckusick return (NULL); 55910278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 5606531Smckusick cgp = bp->b_un.b_cg; 56115950Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC || 56215950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 5635960Smckusic brelse(bp); 5646294Smckusick return (NULL); 5655960Smckusic } 5668105Sroot cgp->cg_time = time.tv_sec; 5675322Smckusic if (size == fs->fs_bsize) { 5685212Smckusic bno = alloccgblk(fs, cgp, bpref); 5694463Smckusic bdwrite(bp); 5704463Smckusic return (bno); 5714463Smckusic } 5724463Smckusic /* 5734463Smckusic * check to see if any fragments are already available 5744463Smckusic * allocsiz is the size which will be allocated, hacking 5754463Smckusic * it down to a smaller size if necessary 5764463Smckusic */ 5775960Smckusic frags = numfrags(fs, size); 5785322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 5794463Smckusic if (cgp->cg_frsum[allocsiz] != 0) 5804463Smckusic break; 5815322Smckusic if (allocsiz == fs->fs_frag) { 5824463Smckusic /* 5834463Smckusic * no fragments were available, so a block will be 5844463Smckusic * allocated, and hacked up 5854463Smckusic */ 5864792Smckusic if (cgp->cg_cs.cs_nbfree == 0) { 5874463Smckusic brelse(bp); 5886294Smckusick return (NULL); 5894463Smckusic } 5905212Smckusic bno = alloccgblk(fs, cgp, bpref); 5915377Smckusic bpref = dtogd(fs, bno); 5925322Smckusic for (i = frags; i < fs->fs_frag; i++) 5934463Smckusic setbit(cgp->cg_free, bpref + i); 5945322Smckusic i = fs->fs_frag - frags; 5954792Smckusic cgp->cg_cs.cs_nffree += i; 5964792Smckusic fs->fs_cstotal.cs_nffree += i; 5975322Smckusic fs->fs_cs(fs, cg).cs_nffree += i; 5989762Ssam fs->fs_fmod++; 5994463Smckusic cgp->cg_frsum[i]++; 6004463Smckusic bdwrite(bp); 6014463Smckusic return (bno); 6024463Smckusic } 6034651Smckusic bno = mapsearch(fs, cgp, bpref, allocsiz); 60415950Smckusick if (bno < 0) { 60515950Smckusick brelse(bp); 6066294Smckusick return (NULL); 60715950Smckusick } 6084463Smckusic for (i = 0; i < frags; i++) 6094463Smckusic clrbit(cgp->cg_free, bno + i); 6104792Smckusic cgp->cg_cs.cs_nffree -= frags; 6114792Smckusic fs->fs_cstotal.cs_nffree -= frags; 6125322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags; 6139762Ssam fs->fs_fmod++; 6144463Smckusic cgp->cg_frsum[allocsiz]--; 6154463Smckusic if (frags != allocsiz) 6164463Smckusic cgp->cg_frsum[allocsiz - frags]++; 6174463Smckusic bdwrite(bp); 6184463Smckusic return (cg * fs->fs_fpg + bno); 6194463Smckusic } 6204463Smckusic 6215375Smckusic /* 6225375Smckusic * Allocate a block in a cylinder group. 6235375Smckusic * 6245375Smckusic * This algorithm implements the following policy: 6255375Smckusic * 1) allocate the requested block. 6265375Smckusic * 2) allocate a rotationally optimal block in the same cylinder. 6275375Smckusic * 3) allocate the next available block on the block rotor for the 6285375Smckusic * specified cylinder group. 6295375Smckusic * Note that this routine only allocates fs_bsize blocks; these 6305375Smckusic * blocks may be fragmented by the routine that allocates them. 6315375Smckusic */ 6324463Smckusic daddr_t 6335212Smckusic alloccgblk(fs, cgp, bpref) 6345965Smckusic register struct fs *fs; 6354463Smckusic register struct cg *cgp; 6364463Smckusic daddr_t bpref; 6374463Smckusic { 6384651Smckusic daddr_t bno; 6396294Smckusick int cylno, pos, delta; 6404651Smckusic short *cylbp; 6415361Smckusic register int i; 6424463Smckusic 6434651Smckusic if (bpref == 0) { 6444651Smckusic bpref = cgp->cg_rotor; 6455361Smckusic goto norot; 6465361Smckusic } 64717224Smckusick bpref = blknum(fs, bpref); 6485377Smckusic bpref = dtogd(fs, bpref); 6495361Smckusic /* 6505361Smckusic * if the requested block is available, use it 6515361Smckusic */ 65211638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) { 6535361Smckusic bno = bpref; 6545361Smckusic goto gotit; 6555361Smckusic } 6565361Smckusic /* 6575361Smckusic * check for a block available on the same cylinder 6585361Smckusic */ 6595361Smckusic cylno = cbtocylno(fs, bpref); 6605375Smckusic if (cgp->cg_btot[cylno] == 0) 6615375Smckusic goto norot; 6625375Smckusic if (fs->fs_cpc == 0) { 6635375Smckusic /* 6645375Smckusic * block layout info is not available, so just have 6655375Smckusic * to take any block in this cylinder. 6665375Smckusic */ 6675375Smckusic bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 6685375Smckusic goto norot; 6695375Smckusic } 6705375Smckusic /* 6715361Smckusic * check the summary information to see if a block is 6725361Smckusic * available in the requested cylinder starting at the 6739163Ssam * requested rotational position and proceeding around. 6745361Smckusic */ 6759163Ssam cylbp = cgp->cg_b[cylno]; 6769163Ssam pos = cbtorpos(fs, bpref); 6775361Smckusic for (i = pos; i < NRPOS; i++) 6785361Smckusic if (cylbp[i] > 0) 6795361Smckusic break; 6805361Smckusic if (i == NRPOS) 6815361Smckusic for (i = 0; i < pos; i++) 6825361Smckusic if (cylbp[i] > 0) 6835361Smckusic break; 6845361Smckusic if (cylbp[i] > 0) { 6854651Smckusic /* 6865361Smckusic * found a rotational position, now find the actual 6875361Smckusic * block. A panic if none is actually there. 6884651Smckusic */ 6895361Smckusic pos = cylno % fs->fs_cpc; 6905361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 6916716Smckusick if (fs->fs_postbl[pos][i] == -1) { 6926716Smckusick printf("pos = %d, i = %d, fs = %s\n", 6936716Smckusick pos, i, fs->fs_fsmnt); 6945361Smckusic panic("alloccgblk: cyl groups corrupted"); 6956716Smckusick } 6966294Smckusick for (i = fs->fs_postbl[pos][i];; ) { 6975361Smckusic if (isblock(fs, cgp->cg_free, bno + i)) { 69811638Ssam bno = blkstofrags(fs, (bno + i)); 6995361Smckusic goto gotit; 7005361Smckusic } 7016294Smckusick delta = fs->fs_rotbl[i]; 7026294Smckusick if (delta <= 0 || delta > MAXBPC - i) 7034651Smckusic break; 7046294Smckusick i += delta; 7054651Smckusic } 7066716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 7075361Smckusic panic("alloccgblk: can't find blk in cyl"); 7084359Smckusick } 7095361Smckusic norot: 7105361Smckusic /* 7115361Smckusic * no blocks in the requested cylinder, so take next 7125361Smckusic * available one in this cylinder group. 7135361Smckusic */ 7148628Sroot bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 7156567Smckusic if (bno < 0) 7166294Smckusick return (NULL); 7174651Smckusic cgp->cg_rotor = bno; 7184359Smckusick gotit: 71911638Ssam clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno)); 7204792Smckusic cgp->cg_cs.cs_nbfree--; 7214792Smckusic fs->fs_cstotal.cs_nbfree--; 7225322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 7235375Smckusic cylno = cbtocylno(fs, bno); 7245375Smckusic cgp->cg_b[cylno][cbtorpos(fs, bno)]--; 7255375Smckusic cgp->cg_btot[cylno]--; 7264359Smckusick fs->fs_fmod++; 7274651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno); 7284359Smckusick } 7294359Smckusick 7305375Smckusic /* 7315375Smckusic * Determine whether an inode can be allocated. 7325375Smckusic * 7335375Smckusic * Check to see if an inode is available, and if it is, 7345375Smckusic * allocate it using the following policy: 7355375Smckusic * 1) allocate the requested inode. 7365375Smckusic * 2) allocate the next available inode after the requested 7375375Smckusic * inode in the specified cylinder group. 7385375Smckusic */ 7399163Ssam ino_t 7405965Smckusic ialloccg(ip, cg, ipref, mode) 7415965Smckusic struct inode *ip; 7424359Smckusick int cg; 7434359Smckusick daddr_t ipref; 7444359Smckusick int mode; 7454359Smckusick { 7465965Smckusic register struct fs *fs; 7474463Smckusic register struct cg *cgp; 74816784Smckusick struct buf *bp; 74916784Smckusick int start, len, loc, map, i; 7504359Smckusick 7515965Smckusic fs = ip->i_fs; 7525322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0) 7536294Smckusick return (NULL); 75410278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 7556531Smckusick cgp = bp->b_un.b_cg; 75615950Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC || 75715950Smckusick cgp->cg_cs.cs_nifree == 0) { 7585960Smckusic brelse(bp); 7596294Smckusick return (NULL); 7605960Smckusic } 7618105Sroot cgp->cg_time = time.tv_sec; 7624359Smckusick if (ipref) { 7634359Smckusick ipref %= fs->fs_ipg; 7644359Smckusick if (isclr(cgp->cg_iused, ipref)) 7654359Smckusick goto gotit; 76616784Smckusick } 76716784Smckusick start = cgp->cg_irotor / NBBY; 76816784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 76916784Smckusick loc = skpc(0xff, len, &cgp->cg_iused[start]); 77016784Smckusick if (loc == 0) { 77117697Smckusick len = start + 1; 77217697Smckusick start = 0; 77317697Smckusick loc = skpc(0xff, len, &cgp->cg_iused[0]); 77417697Smckusick if (loc == 0) { 77517697Smckusick printf("cg = %s, irotor = %d, fs = %s\n", 77617697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt); 77717697Smckusick panic("ialloccg: map corrupted"); 77817697Smckusick /* NOTREACHED */ 77917697Smckusick } 78016784Smckusick } 78116784Smckusick i = start + len - loc; 78216784Smckusick map = cgp->cg_iused[i]; 78316784Smckusick ipref = i * NBBY; 78416784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 78516784Smckusick if ((map & i) == 0) { 7864359Smckusick cgp->cg_irotor = ipref; 7874359Smckusick goto gotit; 7884359Smckusick } 7894359Smckusick } 79016784Smckusick printf("fs = %s\n", fs->fs_fsmnt); 79116784Smckusick panic("ialloccg: block not in map"); 79216784Smckusick /* NOTREACHED */ 7934359Smckusick gotit: 7944359Smckusick setbit(cgp->cg_iused, ipref); 7954792Smckusic cgp->cg_cs.cs_nifree--; 7964792Smckusic fs->fs_cstotal.cs_nifree--; 7975322Smckusic fs->fs_cs(fs, cg).cs_nifree--; 7984359Smckusick fs->fs_fmod++; 7994359Smckusick if ((mode & IFMT) == IFDIR) { 8004792Smckusic cgp->cg_cs.cs_ndir++; 8014792Smckusic fs->fs_cstotal.cs_ndir++; 8025322Smckusic fs->fs_cs(fs, cg).cs_ndir++; 8034359Smckusick } 8044359Smckusick bdwrite(bp); 8054359Smckusick return (cg * fs->fs_ipg + ipref); 8064359Smckusick } 8074359Smckusick 8085375Smckusic /* 8095375Smckusic * Free a block or fragment. 8105375Smckusic * 8115375Smckusic * The specified block or fragment is placed back in the 8125375Smckusic * free map. If a fragment is deallocated, a possible 8135375Smckusic * block reassembly is checked. 8145375Smckusic */ 815*31402Smckusick blkfree(ip, bno, size) 8165965Smckusic register struct inode *ip; 8174359Smckusick daddr_t bno; 8185212Smckusic off_t size; 8194359Smckusick { 8204359Smckusick register struct fs *fs; 8214359Smckusick register struct cg *cgp; 8224359Smckusick register struct buf *bp; 8234463Smckusic int cg, blk, frags, bbase; 8244463Smckusic register int i; 8254359Smckusick 8265965Smckusic fs = ip->i_fs; 8276716Smckusick if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 8286716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 8296716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 830*31402Smckusick panic("blkfree: bad size"); 8316716Smckusick } 8325377Smckusic cg = dtog(fs, bno); 8336567Smckusic if (badblock(fs, bno)) { 8346567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number); 8354359Smckusick return; 8366567Smckusic } 83710278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 8386531Smckusick cgp = bp->b_un.b_cg; 8396531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 8405960Smckusic brelse(bp); 8414359Smckusick return; 8425960Smckusic } 8438105Sroot cgp->cg_time = time.tv_sec; 8445377Smckusic bno = dtogd(fs, bno); 8455322Smckusic if (size == fs->fs_bsize) { 84611638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) { 8476716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 8486716Smckusick ip->i_dev, bno, fs->fs_fsmnt); 849*31402Smckusick panic("blkfree: freeing free block"); 8506567Smckusic } 85111638Ssam setblock(fs, cgp->cg_free, fragstoblks(fs, bno)); 8524792Smckusic cgp->cg_cs.cs_nbfree++; 8534792Smckusic fs->fs_cstotal.cs_nbfree++; 8545322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 8555375Smckusic i = cbtocylno(fs, bno); 8565375Smckusic cgp->cg_b[i][cbtorpos(fs, bno)]++; 8575375Smckusic cgp->cg_btot[i]++; 8584426Smckusic } else { 85917224Smckusick bbase = bno - fragnum(fs, bno); 8604463Smckusic /* 8614463Smckusic * decrement the counts associated with the old frags 8624463Smckusic */ 8636294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 8645322Smckusic fragacct(fs, blk, cgp->cg_frsum, -1); 8654463Smckusic /* 8664463Smckusic * deallocate the fragment 8674463Smckusic */ 8685960Smckusic frags = numfrags(fs, size); 8694463Smckusic for (i = 0; i < frags; i++) { 8706716Smckusick if (isset(cgp->cg_free, bno + i)) { 8716716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n", 8726716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt); 873*31402Smckusick panic("blkfree: freeing free frag"); 8746716Smckusick } 8754426Smckusic setbit(cgp->cg_free, bno + i); 8764426Smckusic } 8776294Smckusick cgp->cg_cs.cs_nffree += i; 8786294Smckusick fs->fs_cstotal.cs_nffree += i; 8796294Smckusick fs->fs_cs(fs, cg).cs_nffree += i; 8804463Smckusic /* 8814463Smckusic * add back in counts associated with the new frags 8824463Smckusic */ 8836294Smckusick blk = blkmap(fs, cgp->cg_free, bbase); 8845322Smckusic fragacct(fs, blk, cgp->cg_frsum, 1); 8854463Smckusic /* 8864463Smckusic * if a complete block has been reassembled, account for it 8874463Smckusic */ 88811638Ssam if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) { 8895322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag; 8905322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag; 8915322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 8924792Smckusic cgp->cg_cs.cs_nbfree++; 8934792Smckusic fs->fs_cstotal.cs_nbfree++; 8945322Smckusic fs->fs_cs(fs, cg).cs_nbfree++; 8955375Smckusic i = cbtocylno(fs, bbase); 8965375Smckusic cgp->cg_b[i][cbtorpos(fs, bbase)]++; 8975375Smckusic cgp->cg_btot[i]++; 8984426Smckusic } 8994426Smckusic } 9004359Smckusick fs->fs_fmod++; 9014359Smckusick bdwrite(bp); 9024359Smckusick } 9034359Smckusick 9045375Smckusic /* 9055375Smckusic * Free an inode. 9065375Smckusic * 9075375Smckusic * The specified inode is placed back in the free map. 9085375Smckusic */ 9095965Smckusic ifree(ip, ino, mode) 9105965Smckusic struct inode *ip; 9114359Smckusick ino_t ino; 9124359Smckusick int mode; 9134359Smckusick { 9144359Smckusick register struct fs *fs; 9154359Smckusick register struct cg *cgp; 9164359Smckusick register struct buf *bp; 9174359Smckusick int cg; 9184359Smckusick 9195965Smckusic fs = ip->i_fs; 9206716Smckusick if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 9216716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9226716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9234359Smckusick panic("ifree: range"); 9246716Smckusick } 9255377Smckusic cg = itog(fs, ino); 92610278Smckusick bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 9276531Smckusick cgp = bp->b_un.b_cg; 9286531Smckusick if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 9295960Smckusic brelse(bp); 9304359Smckusick return; 9315960Smckusic } 9328105Sroot cgp->cg_time = time.tv_sec; 9334359Smckusick ino %= fs->fs_ipg; 9346716Smckusick if (isclr(cgp->cg_iused, ino)) { 9356716Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n", 9366716Smckusick ip->i_dev, ino, fs->fs_fsmnt); 9374359Smckusick panic("ifree: freeing free inode"); 9386716Smckusick } 9394359Smckusick clrbit(cgp->cg_iused, ino); 94016784Smckusick if (ino < cgp->cg_irotor) 94116784Smckusick cgp->cg_irotor = ino; 9424792Smckusic cgp->cg_cs.cs_nifree++; 9434792Smckusic fs->fs_cstotal.cs_nifree++; 9445322Smckusic fs->fs_cs(fs, cg).cs_nifree++; 9454359Smckusick if ((mode & IFMT) == IFDIR) { 9464792Smckusic cgp->cg_cs.cs_ndir--; 9474792Smckusic fs->fs_cstotal.cs_ndir--; 9485322Smckusic fs->fs_cs(fs, cg).cs_ndir--; 9494359Smckusick } 9504359Smckusick fs->fs_fmod++; 9514359Smckusick bdwrite(bp); 9524359Smckusick } 9534359Smckusick 9544463Smckusic /* 9555375Smckusic * Find a block of the specified size in the specified cylinder group. 9565375Smckusic * 9574651Smckusic * It is a panic if a request is made to find a block if none are 9584651Smckusic * available. 9594651Smckusic */ 9604651Smckusic daddr_t 9614651Smckusic mapsearch(fs, cgp, bpref, allocsiz) 9624651Smckusic register struct fs *fs; 9634651Smckusic register struct cg *cgp; 9644651Smckusic daddr_t bpref; 9654651Smckusic int allocsiz; 9664651Smckusic { 9674651Smckusic daddr_t bno; 9684651Smckusic int start, len, loc, i; 9694651Smckusic int blk, field, subfield, pos; 9704651Smckusic 9714651Smckusic /* 9724651Smckusic * find the fragment by searching through the free block 9734651Smckusic * map for an appropriate bit pattern 9744651Smckusic */ 9754651Smckusic if (bpref) 9765377Smckusic start = dtogd(fs, bpref) / NBBY; 9774651Smckusic else 9784651Smckusic start = cgp->cg_frotor / NBBY; 9795398Smckusic len = howmany(fs->fs_fpg, NBBY) - start; 98012755Ssam loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start], 98112755Ssam (caddr_t)fragtbl[fs->fs_frag], 98212755Ssam (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 9834651Smckusic if (loc == 0) { 9846531Smckusick len = start + 1; 9856531Smckusick start = 0; 98617697Smckusick loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[0], 98712755Ssam (caddr_t)fragtbl[fs->fs_frag], 98812755Ssam (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 98916784Smckusick if (loc == 0) { 99016784Smckusick printf("start = %d, len = %d, fs = %s\n", 99116784Smckusick start, len, fs->fs_fsmnt); 99216784Smckusick panic("alloccg: map corrupted"); 99317697Smckusick /* NOTREACHED */ 99416784Smckusick } 9954651Smckusic } 9964651Smckusic bno = (start + len - loc) * NBBY; 9974651Smckusic cgp->cg_frotor = bno; 9984651Smckusic /* 9994651Smckusic * found the byte in the map 10004651Smckusic * sift through the bits to find the selected frag 10014651Smckusic */ 10026294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 10036294Smckusick blk = blkmap(fs, cgp->cg_free, bno); 10044651Smckusic blk <<= 1; 10054651Smckusic field = around[allocsiz]; 10064651Smckusic subfield = inside[allocsiz]; 10075322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 10086294Smckusick if ((blk & field) == subfield) 10096294Smckusick return (bno + pos); 10104651Smckusic field <<= 1; 10114651Smckusic subfield <<= 1; 10124651Smckusic } 10134651Smckusic } 10146716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 10154651Smckusic panic("alloccg: block not in map"); 10166531Smckusick return (-1); 10174651Smckusic } 10184651Smckusic 10194651Smckusic /* 10205375Smckusic * Fserr prints the name of a file system with an error diagnostic. 10215375Smckusic * 10225375Smckusic * The form of the error message is: 10234359Smckusick * fs: error message 10244359Smckusick */ 10254359Smckusick fserr(fs, cp) 10264359Smckusick struct fs *fs; 10274359Smckusick char *cp; 10284359Smckusick { 10294359Smckusick 103024839Seric log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp); 10314359Smckusick } 1032