123402Smckusick /* 237737Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 337737Smckusick * All rights reserved. 423402Smckusick * 5*44539Sbostic * %sccs.include.redist.c% 637737Smckusick * 7*44539Sbostic * @(#)ffs_subr.c 7.13 (Berkeley) 06/28/90 823402Smckusick */ 98719Sroot 108719Sroot #ifdef KERNEL 1117101Sbloom #include "param.h" 1237737Smckusick #include "../ufs/fs.h" 138719Sroot #else 148719Sroot #include <sys/param.h> 1537737Smckusick #include <ufs/fs.h> 168719Sroot #endif 178719Sroot 188719Sroot extern int around[9]; 198719Sroot extern int inside[9]; 208719Sroot extern u_char *fragtbl[]; 218719Sroot 228719Sroot /* 238719Sroot * Update the frsum fields to reflect addition or deletion 248719Sroot * of some frags. 258719Sroot */ 268719Sroot fragacct(fs, fragmap, fraglist, cnt) 278719Sroot struct fs *fs; 288719Sroot int fragmap; 298719Sroot long fraglist[]; 308719Sroot int cnt; 318719Sroot { 328719Sroot int inblk; 338719Sroot register int field, subfield; 348719Sroot register int siz, pos; 358719Sroot 368719Sroot inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 378719Sroot fragmap <<= 1; 388719Sroot for (siz = 1; siz < fs->fs_frag; siz++) { 398719Sroot if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 408719Sroot continue; 418719Sroot field = around[siz]; 428719Sroot subfield = inside[siz]; 438719Sroot for (pos = siz; pos <= fs->fs_frag; pos++) { 448719Sroot if ((fragmap & field) == subfield) { 458719Sroot fraglist[siz] += cnt; 468719Sroot pos += siz; 478719Sroot field <<= siz; 488719Sroot subfield <<= siz; 498719Sroot } 508719Sroot field <<= 1; 518719Sroot subfield <<= 1; 528719Sroot } 538719Sroot } 548719Sroot } 558719Sroot 568719Sroot /* 578719Sroot * block operations 588719Sroot * 598719Sroot * check if a block is available 608719Sroot */ 618719Sroot isblock(fs, cp, h) 628719Sroot struct fs *fs; 638719Sroot unsigned char *cp; 648719Sroot daddr_t h; 658719Sroot { 668719Sroot unsigned char mask; 678719Sroot 6826309Skarels switch ((int)fs->fs_frag) { 698719Sroot case 8: 708719Sroot return (cp[h] == 0xff); 718719Sroot case 4: 728719Sroot mask = 0x0f << ((h & 0x1) << 2); 738719Sroot return ((cp[h >> 1] & mask) == mask); 748719Sroot case 2: 758719Sroot mask = 0x03 << ((h & 0x3) << 1); 768719Sroot return ((cp[h >> 2] & mask) == mask); 778719Sroot case 1: 788719Sroot mask = 0x01 << (h & 0x7); 798719Sroot return ((cp[h >> 3] & mask) == mask); 808719Sroot default: 818719Sroot panic("isblock"); 828719Sroot return (NULL); 838719Sroot } 848719Sroot } 858719Sroot 868719Sroot /* 878719Sroot * take a block out of the map 888719Sroot */ 898719Sroot clrblock(fs, cp, h) 908719Sroot struct fs *fs; 918770Sroot u_char *cp; 928719Sroot daddr_t h; 938719Sroot { 948719Sroot 9526309Skarels switch ((int)fs->fs_frag) { 968719Sroot case 8: 978719Sroot cp[h] = 0; 988719Sroot return; 998719Sroot case 4: 1008719Sroot cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1018719Sroot return; 1028719Sroot case 2: 1038719Sroot cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1048719Sroot return; 1058719Sroot case 1: 1068719Sroot cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1078719Sroot return; 1088719Sroot default: 1098719Sroot panic("clrblock"); 1108719Sroot } 1118719Sroot } 1128719Sroot 1138719Sroot /* 1148719Sroot * put a block into the map 1158719Sroot */ 1168719Sroot setblock(fs, cp, h) 1178719Sroot struct fs *fs; 1188719Sroot unsigned char *cp; 1198719Sroot daddr_t h; 1208719Sroot { 1218719Sroot 12226309Skarels switch ((int)fs->fs_frag) { 1238719Sroot 1248719Sroot case 8: 1258719Sroot cp[h] = 0xff; 1268719Sroot return; 1278719Sroot case 4: 1288719Sroot cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 1298719Sroot return; 1308719Sroot case 2: 1318719Sroot cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 1328719Sroot return; 1338719Sroot case 1: 1348719Sroot cp[h >> 3] |= (0x01 << (h & 0x7)); 1358719Sroot return; 1368719Sroot default: 1378719Sroot panic("setblock"); 1388719Sroot } 1398719Sroot } 1409167Ssam 14141537Smckusick #if (!defined(vax) && !defined(tahoe) && !defined(hp300)) \ 14241537Smckusick || defined(VAX630) || defined(VAX650) 14321090Smckusick /* 14429947Skarels * C definitions of special instructions. 14529947Skarels * Normally expanded with inline. 14621090Smckusick */ 14721090Smckusick scanc(size, cp, table, mask) 14821090Smckusick u_int size; 14921090Smckusick register u_char *cp, table[]; 15021090Smckusick register u_char mask; 15121090Smckusick { 15221090Smckusick register u_char *end = &cp[size]; 15321090Smckusick 15421090Smckusick while (cp < end && (table[*cp] & mask) == 0) 15521090Smckusick cp++; 15621090Smckusick return (end - cp); 15721090Smckusick } 15827476Skridle #endif 15927476Skridle 16041537Smckusick #if !defined(vax) && !defined(tahoe) && !defined(hp300) 16121090Smckusick skpc(mask, size, cp) 16221090Smckusick register u_char mask; 16321090Smckusick u_int size; 16421090Smckusick register u_char *cp; 16521090Smckusick { 16621090Smckusick register u_char *end = &cp[size]; 16721090Smckusick 16821090Smckusick while (cp < end && *cp == mask) 16921090Smckusick cp++; 17021090Smckusick return (end - cp); 17121090Smckusick } 17221090Smckusick 17321090Smckusick locc(mask, size, cp) 17421090Smckusick register u_char mask; 17521090Smckusick u_int size; 17621090Smckusick register u_char *cp; 17721090Smckusick { 17821090Smckusick register u_char *end = &cp[size]; 17921090Smckusick 18021090Smckusick while (cp < end && *cp != mask) 18121090Smckusick cp++; 18221090Smckusick return (end - cp); 18321090Smckusick } 18429947Skarels #endif 185