1*8770Sroot /* ffs_subr.c 4.2 82/10/21 */ 28719Sroot 38719Sroot #ifdef KERNEL 48719Sroot #include "../h/param.h" 58719Sroot #include "../h/fs.h" 68719Sroot #else 78719Sroot #include <sys/param.h> 88719Sroot #include <sys/fs.h> 98719Sroot #endif 108719Sroot 118719Sroot extern int around[9]; 128719Sroot extern int inside[9]; 138719Sroot extern u_char *fragtbl[]; 148719Sroot 158719Sroot /* 168719Sroot * Update the frsum fields to reflect addition or deletion 178719Sroot * of some frags. 188719Sroot */ 198719Sroot fragacct(fs, fragmap, fraglist, cnt) 208719Sroot struct fs *fs; 218719Sroot int fragmap; 228719Sroot long fraglist[]; 238719Sroot int cnt; 248719Sroot { 258719Sroot int inblk; 268719Sroot register int field, subfield; 278719Sroot register int siz, pos; 288719Sroot 298719Sroot inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 308719Sroot fragmap <<= 1; 318719Sroot for (siz = 1; siz < fs->fs_frag; siz++) { 328719Sroot if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 338719Sroot continue; 348719Sroot field = around[siz]; 358719Sroot subfield = inside[siz]; 368719Sroot for (pos = siz; pos <= fs->fs_frag; pos++) { 378719Sroot if ((fragmap & field) == subfield) { 388719Sroot fraglist[siz] += cnt; 398719Sroot pos += siz; 408719Sroot field <<= siz; 418719Sroot subfield <<= siz; 428719Sroot } 438719Sroot field <<= 1; 448719Sroot subfield <<= 1; 458719Sroot } 468719Sroot } 478719Sroot } 488719Sroot 498719Sroot #ifdef KERNEL 508719Sroot /* 518719Sroot * Check that a specified block number is in range. 528719Sroot */ 538719Sroot badblock(fs, bn) 548719Sroot register struct fs *fs; 558719Sroot daddr_t bn; 568719Sroot { 578719Sroot 588719Sroot if ((unsigned)bn >= fs->fs_size) { 598719Sroot printf("bad block %d, ", bn); 608719Sroot fserr(fs, "bad block"); 618719Sroot return (1); 628719Sroot } 638719Sroot return (0); 648719Sroot } 658719Sroot #endif 668719Sroot 678719Sroot /* 688719Sroot * block operations 698719Sroot * 708719Sroot * check if a block is available 718719Sroot */ 728719Sroot isblock(fs, cp, h) 738719Sroot struct fs *fs; 748719Sroot unsigned char *cp; 758719Sroot daddr_t h; 768719Sroot { 778719Sroot unsigned char mask; 788719Sroot 798719Sroot switch (fs->fs_frag) { 808719Sroot case 8: 818719Sroot return (cp[h] == 0xff); 828719Sroot case 4: 838719Sroot mask = 0x0f << ((h & 0x1) << 2); 848719Sroot return ((cp[h >> 1] & mask) == mask); 858719Sroot case 2: 868719Sroot mask = 0x03 << ((h & 0x3) << 1); 878719Sroot return ((cp[h >> 2] & mask) == mask); 888719Sroot case 1: 898719Sroot mask = 0x01 << (h & 0x7); 908719Sroot return ((cp[h >> 3] & mask) == mask); 918719Sroot default: 928719Sroot panic("isblock"); 938719Sroot return (NULL); 948719Sroot } 958719Sroot } 968719Sroot 978719Sroot /* 988719Sroot * take a block out of the map 998719Sroot */ 1008719Sroot clrblock(fs, cp, h) 1018719Sroot struct fs *fs; 102*8770Sroot u_char *cp; 1038719Sroot daddr_t h; 1048719Sroot { 1058719Sroot 1068719Sroot switch ((fs)->fs_frag) { 1078719Sroot case 8: 1088719Sroot cp[h] = 0; 1098719Sroot return; 1108719Sroot case 4: 1118719Sroot cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1128719Sroot return; 1138719Sroot case 2: 1148719Sroot cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1158719Sroot return; 1168719Sroot case 1: 1178719Sroot cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1188719Sroot return; 1198719Sroot default: 1208719Sroot panic("clrblock"); 1218719Sroot } 1228719Sroot } 1238719Sroot 1248719Sroot /* 1258719Sroot * put a block into the map 1268719Sroot */ 1278719Sroot setblock(fs, cp, h) 1288719Sroot struct fs *fs; 1298719Sroot unsigned char *cp; 1308719Sroot daddr_t h; 1318719Sroot { 1328719Sroot 1338719Sroot switch (fs->fs_frag) { 1348719Sroot 1358719Sroot case 8: 1368719Sroot cp[h] = 0xff; 1378719Sroot return; 1388719Sroot case 4: 1398719Sroot cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 1408719Sroot return; 1418719Sroot case 2: 1428719Sroot cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 1438719Sroot return; 1448719Sroot case 1: 1458719Sroot cp[h >> 3] |= (0x01 << (h & 0x7)); 1468719Sroot return; 1478719Sroot default: 1488719Sroot panic("setblock"); 1498719Sroot } 1508719Sroot } 151