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*51636Sbostic * @(#)ffs_subr.c 7.16 (Berkeley) 11/11/91 823402Smckusick */ 98719Sroot 108719Sroot #include <sys/param.h> 1151471Sbostic #include <sys/buf.h> 128719Sroot 1351471Sbostic #include <ufs/ufs/quota.h> 1451471Sbostic #include <ufs/ufs/inode.h> 158719Sroot 1651471Sbostic #include <ufs/ffs/fs.h> 1751471Sbostic #include <ufs/ffs/ffs_extern.h> 1851471Sbostic 19*51636Sbostic #ifdef KERNEL 20*51636Sbostic #include <sys/vnode.h> 21*51636Sbostic 228719Sroot /* 2351471Sbostic * Return buffer with the contents of block "offset" from the beginning of 2451471Sbostic * directory "ip". If "res" is non-zero, fill it in with a pointer to the 2551471Sbostic * remaining space in the directory. 2651471Sbostic */ 2751471Sbostic int 2851543Smckusick ffs_blkatoff(vp, offset, res, bpp) 2951543Smckusick struct vnode *vp; 3051471Sbostic off_t offset; 3151471Sbostic char **res; 3251471Sbostic struct buf **bpp; 3351471Sbostic { 3451543Smckusick struct inode *ip; 3551471Sbostic register struct fs *fs; 3651471Sbostic struct buf *bp; 3751471Sbostic daddr_t lbn; 3851471Sbostic int bsize, error; 3951471Sbostic 4051543Smckusick ip = VTOI(vp); 4151471Sbostic fs = ip->i_fs; 4251471Sbostic lbn = lblkno(fs, offset); 4351471Sbostic bsize = blksize(fs, ip, lbn); 4451471Sbostic 4551471Sbostic *bpp = NULL; 4651543Smckusick if (error = bread(vp, lbn, bsize, NOCRED, &bp)) { 4751471Sbostic brelse(bp); 4851471Sbostic return (error); 4951471Sbostic } 5051471Sbostic if (res) 5151471Sbostic *res = bp->b_un.b_addr + blkoff(fs, offset); 5251471Sbostic *bpp = bp; 5351471Sbostic return (0); 5451471Sbostic } 55*51636Sbostic #endif 5651471Sbostic 5751471Sbostic /* 588719Sroot * Update the frsum fields to reflect addition or deletion 598719Sroot * of some frags. 608719Sroot */ 6151471Sbostic void 6251471Sbostic ffs_fragacct(fs, fragmap, fraglist, cnt) 638719Sroot struct fs *fs; 648719Sroot int fragmap; 658719Sroot long fraglist[]; 668719Sroot int cnt; 678719Sroot { 688719Sroot int inblk; 698719Sroot register int field, subfield; 708719Sroot register int siz, pos; 718719Sroot 728719Sroot inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 738719Sroot fragmap <<= 1; 748719Sroot for (siz = 1; siz < fs->fs_frag; siz++) { 758719Sroot if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 768719Sroot continue; 778719Sroot field = around[siz]; 788719Sroot subfield = inside[siz]; 798719Sroot for (pos = siz; pos <= fs->fs_frag; pos++) { 808719Sroot if ((fragmap & field) == subfield) { 818719Sroot fraglist[siz] += cnt; 828719Sroot pos += siz; 838719Sroot field <<= siz; 848719Sroot subfield <<= siz; 858719Sroot } 868719Sroot field <<= 1; 878719Sroot subfield <<= 1; 888719Sroot } 898719Sroot } 908719Sroot } 918719Sroot 92*51636Sbostic #if defined(KERNEL) && defined(DIAGNOSTIC) 9351471Sbostic void 9451471Sbostic ffs_checkoverlap(bp, ip) 9551471Sbostic struct buf *bp; 9651471Sbostic struct inode *ip; 9751471Sbostic { 9851471Sbostic register struct buf *ebp, *ep; 9951471Sbostic register daddr_t start, last; 10051471Sbostic struct vnode *vp; 10151471Sbostic 10251471Sbostic ebp = &buf[nbuf]; 10351471Sbostic start = bp->b_blkno; 10451471Sbostic last = start + btodb(bp->b_bcount) - 1; 10551471Sbostic for (ep = buf; ep < ebp; ep++) { 10651471Sbostic if (ep == bp || (ep->b_flags & B_INVAL) || 10751471Sbostic ep->b_vp == NULLVP) 10851471Sbostic continue; 10951471Sbostic if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0)) 11051471Sbostic continue; 11151471Sbostic if (vp != ip->i_devvp) 11251471Sbostic continue; 11351471Sbostic /* look for overlap */ 11451471Sbostic if (ep->b_bcount == 0 || ep->b_blkno > last || 11551471Sbostic ep->b_blkno + btodb(ep->b_bcount) <= start) 11651471Sbostic continue; 11751471Sbostic vprint("Disk overlap", vp); 11851471Sbostic (void)printf("\tstart %d, end %d overlap start %d, end %d\n", 11951471Sbostic start, last, ep->b_blkno, 12051471Sbostic ep->b_blkno + btodb(ep->b_bcount) - 1); 12151471Sbostic panic("Disk buffer overlap"); 12251471Sbostic } 12351471Sbostic } 12451471Sbostic #endif /* DIAGNOSTIC */ 12551471Sbostic 1268719Sroot /* 1278719Sroot * block operations 1288719Sroot * 1298719Sroot * check if a block is available 1308719Sroot */ 13151471Sbostic int 13251471Sbostic ffs_isblock(fs, cp, h) 1338719Sroot struct fs *fs; 1348719Sroot unsigned char *cp; 1358719Sroot daddr_t h; 1368719Sroot { 1378719Sroot unsigned char mask; 1388719Sroot 13926309Skarels switch ((int)fs->fs_frag) { 1408719Sroot case 8: 1418719Sroot return (cp[h] == 0xff); 1428719Sroot case 4: 1438719Sroot mask = 0x0f << ((h & 0x1) << 2); 1448719Sroot return ((cp[h >> 1] & mask) == mask); 1458719Sroot case 2: 1468719Sroot mask = 0x03 << ((h & 0x3) << 1); 1478719Sroot return ((cp[h >> 2] & mask) == mask); 1488719Sroot case 1: 1498719Sroot mask = 0x01 << (h & 0x7); 1508719Sroot return ((cp[h >> 3] & mask) == mask); 1518719Sroot default: 15251471Sbostic panic("ffs_isblock"); 1538719Sroot } 1548719Sroot } 1558719Sroot 1568719Sroot /* 1578719Sroot * take a block out of the map 1588719Sroot */ 15951471Sbostic void 16051471Sbostic ffs_clrblock(fs, cp, h) 1618719Sroot struct fs *fs; 1628770Sroot u_char *cp; 1638719Sroot daddr_t h; 1648719Sroot { 1658719Sroot 16626309Skarels switch ((int)fs->fs_frag) { 1678719Sroot case 8: 1688719Sroot cp[h] = 0; 1698719Sroot return; 1708719Sroot case 4: 1718719Sroot cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1728719Sroot return; 1738719Sroot case 2: 1748719Sroot cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1758719Sroot return; 1768719Sroot case 1: 1778719Sroot cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1788719Sroot return; 1798719Sroot default: 18051471Sbostic panic("ffs_clrblock"); 1818719Sroot } 1828719Sroot } 1838719Sroot 1848719Sroot /* 1858719Sroot * put a block into the map 1868719Sroot */ 18751471Sbostic void 18851471Sbostic ffs_setblock(fs, cp, h) 1898719Sroot struct fs *fs; 1908719Sroot unsigned char *cp; 1918719Sroot daddr_t h; 1928719Sroot { 1938719Sroot 19426309Skarels switch ((int)fs->fs_frag) { 1958719Sroot 1968719Sroot case 8: 1978719Sroot cp[h] = 0xff; 1988719Sroot return; 1998719Sroot case 4: 2008719Sroot cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 2018719Sroot return; 2028719Sroot case 2: 2038719Sroot cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 2048719Sroot return; 2058719Sroot case 1: 2068719Sroot cp[h >> 3] |= (0x01 << (h & 0x7)); 2078719Sroot return; 2088719Sroot default: 20951471Sbostic panic("ffs_setblock"); 2108719Sroot } 2118719Sroot } 2129167Ssam 21341537Smckusick #if (!defined(vax) && !defined(tahoe) && !defined(hp300)) \ 21441537Smckusick || defined(VAX630) || defined(VAX650) 21521090Smckusick /* 21629947Skarels * C definitions of special instructions. 21729947Skarels * Normally expanded with inline. 21821090Smckusick */ 21951471Sbostic int 22021090Smckusick scanc(size, cp, table, mask) 22121090Smckusick u_int size; 22221090Smckusick register u_char *cp, table[]; 22321090Smckusick register u_char mask; 22421090Smckusick { 22521090Smckusick register u_char *end = &cp[size]; 22621090Smckusick 22721090Smckusick while (cp < end && (table[*cp] & mask) == 0) 22821090Smckusick cp++; 22921090Smckusick return (end - cp); 23021090Smckusick } 23127476Skridle #endif 23227476Skridle 23341537Smckusick #if !defined(vax) && !defined(tahoe) && !defined(hp300) 23451471Sbostic int 23521090Smckusick skpc(mask, size, cp) 23621090Smckusick register u_char mask; 23721090Smckusick u_int size; 23821090Smckusick register u_char *cp; 23921090Smckusick { 24021090Smckusick register u_char *end = &cp[size]; 24121090Smckusick 24221090Smckusick while (cp < end && *cp == mask) 24321090Smckusick cp++; 24421090Smckusick return (end - cp); 24521090Smckusick } 24621090Smckusick 24751471Sbostic int 24821090Smckusick locc(mask, size, cp) 24921090Smckusick register u_char mask; 25021090Smckusick u_int size; 25121090Smckusick register u_char *cp; 25221090Smckusick { 25321090Smckusick register u_char *end = &cp[size]; 25421090Smckusick 25521090Smckusick while (cp < end && *cp != mask) 25621090Smckusick cp++; 25721090Smckusick return (end - cp); 25821090Smckusick } 25929947Skarels #endif 260