123402Smckusick /* 263371Sbostic * Copyright (c) 1982, 1986, 1989, 1993 363371Sbostic * The Regents of the University of California. All rights reserved. 423402Smckusick * 544539Sbostic * %sccs.include.redist.c% 637737Smckusick * 7*68554Smckusick * @(#)ffs_subr.c 8.4 (Berkeley) 03/21/95 823402Smckusick */ 98719Sroot 108719Sroot #include <sys/param.h> 11*68554Smckusick #ifndef KERNEL 12*68554Smckusick typedef u_int32_t ufs_daddr_t; 1351471Sbostic #include <ufs/ffs/fs.h> 14*68554Smckusick #else 1551471Sbostic 1654304Smckusick #include <sys/systm.h> 1753706Smckusick #include <sys/vnode.h> 1851956Smckusick #include <sys/buf.h> 1951956Smckusick #include <ufs/ufs/quota.h> 2051956Smckusick #include <ufs/ufs/inode.h> 21*68554Smckusick #include <ufs/ffs/fs.h> 22*68554Smckusick #include <ufs/ffs/ffs_extern.h> 2351636Sbostic 248719Sroot /* 2551471Sbostic * Return buffer with the contents of block "offset" from the beginning of 2651471Sbostic * directory "ip". If "res" is non-zero, fill it in with a pointer to the 2751471Sbostic * remaining space in the directory. 2851471Sbostic */ 2951471Sbostic int 3054655Smckusick ffs_blkatoff(ap) 3154655Smckusick struct vop_blkatoff_args /* { 3254655Smckusick struct vnode *a_vp; 3354655Smckusick off_t a_offset; 3454655Smckusick char **a_res; 3554655Smckusick struct buf **a_bpp; 3654655Smckusick } */ *ap; 3751471Sbostic { 3851543Smckusick struct inode *ip; 3951471Sbostic register struct fs *fs; 4051471Sbostic struct buf *bp; 41*68554Smckusick ufs_daddr_t lbn; 4251471Sbostic int bsize, error; 4351471Sbostic 4453586Sheideman ip = VTOI(ap->a_vp); 4551471Sbostic fs = ip->i_fs; 4653586Sheideman lbn = lblkno(fs, ap->a_offset); 4751471Sbostic bsize = blksize(fs, ip, lbn); 4851471Sbostic 4953586Sheideman *ap->a_bpp = NULL; 5053586Sheideman if (error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) { 5151471Sbostic brelse(bp); 5251471Sbostic return (error); 5351471Sbostic } 5453586Sheideman if (ap->a_res) 5564509Sbostic *ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset); 5653586Sheideman *ap->a_bpp = bp; 5751471Sbostic return (0); 5851471Sbostic } 5951636Sbostic #endif 6051471Sbostic 6151471Sbostic /* 628719Sroot * Update the frsum fields to reflect addition or deletion 638719Sroot * of some frags. 648719Sroot */ 6551471Sbostic void 6651471Sbostic ffs_fragacct(fs, fragmap, fraglist, cnt) 678719Sroot struct fs *fs; 688719Sroot int fragmap; 6968122Smckusick int32_t fraglist[]; 708719Sroot int cnt; 718719Sroot { 728719Sroot int inblk; 738719Sroot register int field, subfield; 748719Sroot register int siz, pos; 758719Sroot 768719Sroot inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 778719Sroot fragmap <<= 1; 788719Sroot for (siz = 1; siz < fs->fs_frag; siz++) { 798719Sroot if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 808719Sroot continue; 818719Sroot field = around[siz]; 828719Sroot subfield = inside[siz]; 838719Sroot for (pos = siz; pos <= fs->fs_frag; pos++) { 848719Sroot if ((fragmap & field) == subfield) { 858719Sroot fraglist[siz] += cnt; 868719Sroot pos += siz; 878719Sroot field <<= siz; 888719Sroot subfield <<= siz; 898719Sroot } 908719Sroot field <<= 1; 918719Sroot subfield <<= 1; 928719Sroot } 938719Sroot } 948719Sroot } 958719Sroot 9651636Sbostic #if defined(KERNEL) && defined(DIAGNOSTIC) 9751471Sbostic void 9851471Sbostic ffs_checkoverlap(bp, ip) 9951471Sbostic struct buf *bp; 10051471Sbostic struct inode *ip; 10151471Sbostic { 10251471Sbostic register struct buf *ebp, *ep; 103*68554Smckusick register ufs_daddr_t start, last; 10451471Sbostic struct vnode *vp; 10551471Sbostic 10651471Sbostic ebp = &buf[nbuf]; 10751471Sbostic start = bp->b_blkno; 10851471Sbostic last = start + btodb(bp->b_bcount) - 1; 10951471Sbostic for (ep = buf; ep < ebp; ep++) { 11051471Sbostic if (ep == bp || (ep->b_flags & B_INVAL) || 11151471Sbostic ep->b_vp == NULLVP) 11251471Sbostic continue; 113*68554Smckusick if (VOP_BMAP(ep->b_vp, (ufs_daddr_t)0, &vp, (ufs_daddr_t)0, 114*68554Smckusick NULL)) 11551471Sbostic continue; 11651471Sbostic if (vp != ip->i_devvp) 11751471Sbostic continue; 11851471Sbostic /* look for overlap */ 11951471Sbostic if (ep->b_bcount == 0 || ep->b_blkno > last || 12051471Sbostic ep->b_blkno + btodb(ep->b_bcount) <= start) 12151471Sbostic continue; 12251471Sbostic vprint("Disk overlap", vp); 12351471Sbostic (void)printf("\tstart %d, end %d overlap start %d, end %d\n", 12451471Sbostic start, last, ep->b_blkno, 12551471Sbostic ep->b_blkno + btodb(ep->b_bcount) - 1); 12651471Sbostic panic("Disk buffer overlap"); 12751471Sbostic } 12851471Sbostic } 12951471Sbostic #endif /* DIAGNOSTIC */ 13051471Sbostic 1318719Sroot /* 1328719Sroot * block operations 1338719Sroot * 1348719Sroot * check if a block is available 1358719Sroot */ 13651471Sbostic int 13751471Sbostic ffs_isblock(fs, cp, h) 1388719Sroot struct fs *fs; 1398719Sroot unsigned char *cp; 140*68554Smckusick ufs_daddr_t h; 1418719Sroot { 1428719Sroot unsigned char mask; 1438719Sroot 14426309Skarels switch ((int)fs->fs_frag) { 1458719Sroot case 8: 1468719Sroot return (cp[h] == 0xff); 1478719Sroot case 4: 1488719Sroot mask = 0x0f << ((h & 0x1) << 2); 1498719Sroot return ((cp[h >> 1] & mask) == mask); 1508719Sroot case 2: 1518719Sroot mask = 0x03 << ((h & 0x3) << 1); 1528719Sroot return ((cp[h >> 2] & mask) == mask); 1538719Sroot case 1: 1548719Sroot mask = 0x01 << (h & 0x7); 1558719Sroot return ((cp[h >> 3] & mask) == mask); 1568719Sroot default: 15751471Sbostic panic("ffs_isblock"); 1588719Sroot } 1598719Sroot } 1608719Sroot 1618719Sroot /* 1628719Sroot * take a block out of the map 1638719Sroot */ 16451471Sbostic void 16551471Sbostic ffs_clrblock(fs, cp, h) 1668719Sroot struct fs *fs; 1678770Sroot u_char *cp; 168*68554Smckusick ufs_daddr_t h; 1698719Sroot { 1708719Sroot 17126309Skarels switch ((int)fs->fs_frag) { 1728719Sroot case 8: 1738719Sroot cp[h] = 0; 1748719Sroot return; 1758719Sroot case 4: 1768719Sroot cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1778719Sroot return; 1788719Sroot case 2: 1798719Sroot cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1808719Sroot return; 1818719Sroot case 1: 1828719Sroot cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1838719Sroot return; 1848719Sroot default: 18551471Sbostic panic("ffs_clrblock"); 1868719Sroot } 1878719Sroot } 1888719Sroot 1898719Sroot /* 1908719Sroot * put a block into the map 1918719Sroot */ 19251471Sbostic void 19351471Sbostic ffs_setblock(fs, cp, h) 1948719Sroot struct fs *fs; 1958719Sroot unsigned char *cp; 196*68554Smckusick ufs_daddr_t h; 1978719Sroot { 1988719Sroot 19926309Skarels switch ((int)fs->fs_frag) { 2008719Sroot 2018719Sroot case 8: 2028719Sroot cp[h] = 0xff; 2038719Sroot return; 2048719Sroot case 4: 2058719Sroot cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 2068719Sroot return; 2078719Sroot case 2: 2088719Sroot cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 2098719Sroot return; 2108719Sroot case 1: 2118719Sroot cp[h >> 3] |= (0x01 << (h & 0x7)); 2128719Sroot return; 2138719Sroot default: 21451471Sbostic panic("ffs_setblock"); 2158719Sroot } 2168719Sroot } 217