123402Smckusick /* 237737Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 337737Smckusick * All rights reserved. 423402Smckusick * 544539Sbostic * %sccs.include.redist.c% 637737Smckusick * 7*51543Smckusick * @(#)ffs_subr.c 7.15 (Berkeley) 11/05/91 823402Smckusick */ 98719Sroot 108719Sroot #include <sys/param.h> 1151471Sbostic #include <sys/buf.h> 1251471Sbostic #include <sys/vnode.h> 138719Sroot 1451471Sbostic #include <ufs/ufs/quota.h> 1551471Sbostic #include <ufs/ufs/inode.h> 168719Sroot 1751471Sbostic #include <ufs/ffs/fs.h> 1851471Sbostic #include <ufs/ffs/ffs_extern.h> 1951471Sbostic 208719Sroot /* 2151471Sbostic * Return buffer with the contents of block "offset" from the beginning of 2251471Sbostic * directory "ip". If "res" is non-zero, fill it in with a pointer to the 2351471Sbostic * remaining space in the directory. 2451471Sbostic */ 2551471Sbostic int 26*51543Smckusick ffs_blkatoff(vp, offset, res, bpp) 27*51543Smckusick struct vnode *vp; 2851471Sbostic off_t offset; 2951471Sbostic char **res; 3051471Sbostic struct buf **bpp; 3151471Sbostic { 32*51543Smckusick struct inode *ip; 3351471Sbostic register struct fs *fs; 3451471Sbostic struct buf *bp; 3551471Sbostic daddr_t lbn; 3651471Sbostic int bsize, error; 3751471Sbostic 38*51543Smckusick ip = VTOI(vp); 3951471Sbostic fs = ip->i_fs; 4051471Sbostic lbn = lblkno(fs, offset); 4151471Sbostic bsize = blksize(fs, ip, lbn); 4251471Sbostic 4351471Sbostic *bpp = NULL; 44*51543Smckusick if (error = bread(vp, lbn, bsize, NOCRED, &bp)) { 4551471Sbostic brelse(bp); 4651471Sbostic return (error); 4751471Sbostic } 4851471Sbostic if (res) 4951471Sbostic *res = bp->b_un.b_addr + blkoff(fs, offset); 5051471Sbostic *bpp = bp; 5151471Sbostic return (0); 5251471Sbostic } 5351471Sbostic 5451471Sbostic /* 558719Sroot * Update the frsum fields to reflect addition or deletion 568719Sroot * of some frags. 578719Sroot */ 5851471Sbostic void 5951471Sbostic ffs_fragacct(fs, fragmap, fraglist, cnt) 608719Sroot struct fs *fs; 618719Sroot int fragmap; 628719Sroot long fraglist[]; 638719Sroot int cnt; 648719Sroot { 658719Sroot int inblk; 668719Sroot register int field, subfield; 678719Sroot register int siz, pos; 688719Sroot 698719Sroot inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 708719Sroot fragmap <<= 1; 718719Sroot for (siz = 1; siz < fs->fs_frag; siz++) { 728719Sroot if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 738719Sroot continue; 748719Sroot field = around[siz]; 758719Sroot subfield = inside[siz]; 768719Sroot for (pos = siz; pos <= fs->fs_frag; pos++) { 778719Sroot if ((fragmap & field) == subfield) { 788719Sroot fraglist[siz] += cnt; 798719Sroot pos += siz; 808719Sroot field <<= siz; 818719Sroot subfield <<= siz; 828719Sroot } 838719Sroot field <<= 1; 848719Sroot subfield <<= 1; 858719Sroot } 868719Sroot } 878719Sroot } 888719Sroot 8951471Sbostic #ifdef DIAGNOSTIC 9051471Sbostic void 9151471Sbostic ffs_checkoverlap(bp, ip) 9251471Sbostic struct buf *bp; 9351471Sbostic struct inode *ip; 9451471Sbostic { 9551471Sbostic register struct buf *ebp, *ep; 9651471Sbostic register daddr_t start, last; 9751471Sbostic struct vnode *vp; 9851471Sbostic 9951471Sbostic ebp = &buf[nbuf]; 10051471Sbostic start = bp->b_blkno; 10151471Sbostic last = start + btodb(bp->b_bcount) - 1; 10251471Sbostic for (ep = buf; ep < ebp; ep++) { 10351471Sbostic if (ep == bp || (ep->b_flags & B_INVAL) || 10451471Sbostic ep->b_vp == NULLVP) 10551471Sbostic continue; 10651471Sbostic if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0)) 10751471Sbostic continue; 10851471Sbostic if (vp != ip->i_devvp) 10951471Sbostic continue; 11051471Sbostic /* look for overlap */ 11151471Sbostic if (ep->b_bcount == 0 || ep->b_blkno > last || 11251471Sbostic ep->b_blkno + btodb(ep->b_bcount) <= start) 11351471Sbostic continue; 11451471Sbostic vprint("Disk overlap", vp); 11551471Sbostic (void)printf("\tstart %d, end %d overlap start %d, end %d\n", 11651471Sbostic start, last, ep->b_blkno, 11751471Sbostic ep->b_blkno + btodb(ep->b_bcount) - 1); 11851471Sbostic panic("Disk buffer overlap"); 11951471Sbostic } 12051471Sbostic } 12151471Sbostic #endif /* DIAGNOSTIC */ 12251471Sbostic 1238719Sroot /* 1248719Sroot * block operations 1258719Sroot * 1268719Sroot * check if a block is available 1278719Sroot */ 12851471Sbostic int 12951471Sbostic ffs_isblock(fs, cp, h) 1308719Sroot struct fs *fs; 1318719Sroot unsigned char *cp; 1328719Sroot daddr_t h; 1338719Sroot { 1348719Sroot unsigned char mask; 1358719Sroot 13626309Skarels switch ((int)fs->fs_frag) { 1378719Sroot case 8: 1388719Sroot return (cp[h] == 0xff); 1398719Sroot case 4: 1408719Sroot mask = 0x0f << ((h & 0x1) << 2); 1418719Sroot return ((cp[h >> 1] & mask) == mask); 1428719Sroot case 2: 1438719Sroot mask = 0x03 << ((h & 0x3) << 1); 1448719Sroot return ((cp[h >> 2] & mask) == mask); 1458719Sroot case 1: 1468719Sroot mask = 0x01 << (h & 0x7); 1478719Sroot return ((cp[h >> 3] & mask) == mask); 1488719Sroot default: 14951471Sbostic panic("ffs_isblock"); 1508719Sroot } 1518719Sroot } 1528719Sroot 1538719Sroot /* 1548719Sroot * take a block out of the map 1558719Sroot */ 15651471Sbostic void 15751471Sbostic ffs_clrblock(fs, cp, h) 1588719Sroot struct fs *fs; 1598770Sroot u_char *cp; 1608719Sroot daddr_t h; 1618719Sroot { 1628719Sroot 16326309Skarels switch ((int)fs->fs_frag) { 1648719Sroot case 8: 1658719Sroot cp[h] = 0; 1668719Sroot return; 1678719Sroot case 4: 1688719Sroot cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1698719Sroot return; 1708719Sroot case 2: 1718719Sroot cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1728719Sroot return; 1738719Sroot case 1: 1748719Sroot cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1758719Sroot return; 1768719Sroot default: 17751471Sbostic panic("ffs_clrblock"); 1788719Sroot } 1798719Sroot } 1808719Sroot 1818719Sroot /* 1828719Sroot * put a block into the map 1838719Sroot */ 18451471Sbostic void 18551471Sbostic ffs_setblock(fs, cp, h) 1868719Sroot struct fs *fs; 1878719Sroot unsigned char *cp; 1888719Sroot daddr_t h; 1898719Sroot { 1908719Sroot 19126309Skarels switch ((int)fs->fs_frag) { 1928719Sroot 1938719Sroot case 8: 1948719Sroot cp[h] = 0xff; 1958719Sroot return; 1968719Sroot case 4: 1978719Sroot cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 1988719Sroot return; 1998719Sroot case 2: 2008719Sroot cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 2018719Sroot return; 2028719Sroot case 1: 2038719Sroot cp[h >> 3] |= (0x01 << (h & 0x7)); 2048719Sroot return; 2058719Sroot default: 20651471Sbostic panic("ffs_setblock"); 2078719Sroot } 2088719Sroot } 2099167Ssam 21041537Smckusick #if (!defined(vax) && !defined(tahoe) && !defined(hp300)) \ 21141537Smckusick || defined(VAX630) || defined(VAX650) 21221090Smckusick /* 21329947Skarels * C definitions of special instructions. 21429947Skarels * Normally expanded with inline. 21521090Smckusick */ 21651471Sbostic int 21721090Smckusick scanc(size, cp, table, mask) 21821090Smckusick u_int size; 21921090Smckusick register u_char *cp, table[]; 22021090Smckusick register u_char mask; 22121090Smckusick { 22221090Smckusick register u_char *end = &cp[size]; 22321090Smckusick 22421090Smckusick while (cp < end && (table[*cp] & mask) == 0) 22521090Smckusick cp++; 22621090Smckusick return (end - cp); 22721090Smckusick } 22827476Skridle #endif 22927476Skridle 23041537Smckusick #if !defined(vax) && !defined(tahoe) && !defined(hp300) 23151471Sbostic int 23221090Smckusick skpc(mask, size, cp) 23321090Smckusick register u_char mask; 23421090Smckusick u_int size; 23521090Smckusick register u_char *cp; 23621090Smckusick { 23721090Smckusick register u_char *end = &cp[size]; 23821090Smckusick 23921090Smckusick while (cp < end && *cp == mask) 24021090Smckusick cp++; 24121090Smckusick return (end - cp); 24221090Smckusick } 24321090Smckusick 24451471Sbostic int 24521090Smckusick locc(mask, size, cp) 24621090Smckusick register u_char mask; 24721090Smckusick u_int size; 24821090Smckusick register u_char *cp; 24921090Smckusick { 25021090Smckusick register u_char *end = &cp[size]; 25121090Smckusick 25221090Smckusick while (cp < end && *cp != mask) 25321090Smckusick cp++; 25421090Smckusick return (end - cp); 25521090Smckusick } 25629947Skarels #endif 257