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*54655Smckusick * @(#)ffs_subr.c 7.26 (Berkeley) 07/03/92 823402Smckusick */ 98719Sroot 108719Sroot #include <sys/param.h> 1151471Sbostic #include <ufs/ffs/fs.h> 1251471Sbostic 1351636Sbostic #ifdef KERNEL 1454304Smckusick #include <sys/systm.h> 1553706Smckusick #include <sys/vnode.h> 1653706Smckusick #include <ufs/ffs/ffs_extern.h> 1751956Smckusick #include <sys/buf.h> 1851956Smckusick #include <ufs/ufs/quota.h> 1951956Smckusick #include <ufs/ufs/inode.h> 2051636Sbostic 218719Sroot /* 2251471Sbostic * Return buffer with the contents of block "offset" from the beginning of 2351471Sbostic * directory "ip". If "res" is non-zero, fill it in with a pointer to the 2451471Sbostic * remaining space in the directory. 2551471Sbostic */ 2651471Sbostic int 27*54655Smckusick ffs_blkatoff(ap) 28*54655Smckusick struct vop_blkatoff_args /* { 29*54655Smckusick struct vnode *a_vp; 30*54655Smckusick off_t a_offset; 31*54655Smckusick char **a_res; 32*54655Smckusick struct buf **a_bpp; 33*54655Smckusick } */ *ap; 3451471Sbostic { 3551543Smckusick struct inode *ip; 3651471Sbostic register struct fs *fs; 3751471Sbostic struct buf *bp; 3851471Sbostic daddr_t lbn; 3951471Sbostic int bsize, error; 4051471Sbostic 4153586Sheideman ip = VTOI(ap->a_vp); 4251471Sbostic fs = ip->i_fs; 4353586Sheideman lbn = lblkno(fs, ap->a_offset); 4451471Sbostic bsize = blksize(fs, ip, lbn); 4551471Sbostic 4653586Sheideman *ap->a_bpp = NULL; 4753586Sheideman if (error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) { 4851471Sbostic brelse(bp); 4951471Sbostic return (error); 5051471Sbostic } 5153586Sheideman if (ap->a_res) 5253586Sheideman *ap->a_res = bp->b_un.b_addr + blkoff(fs, ap->a_offset); 5353586Sheideman *ap->a_bpp = bp; 5451471Sbostic return (0); 5551471Sbostic } 5651636Sbostic #endif 5751471Sbostic 5851471Sbostic /* 598719Sroot * Update the frsum fields to reflect addition or deletion 608719Sroot * of some frags. 618719Sroot */ 6251471Sbostic void 6351471Sbostic ffs_fragacct(fs, fragmap, fraglist, cnt) 648719Sroot struct fs *fs; 658719Sroot int fragmap; 668719Sroot long fraglist[]; 678719Sroot int cnt; 688719Sroot { 698719Sroot int inblk; 708719Sroot register int field, subfield; 718719Sroot register int siz, pos; 728719Sroot 738719Sroot inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 748719Sroot fragmap <<= 1; 758719Sroot for (siz = 1; siz < fs->fs_frag; siz++) { 768719Sroot if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 778719Sroot continue; 788719Sroot field = around[siz]; 798719Sroot subfield = inside[siz]; 808719Sroot for (pos = siz; pos <= fs->fs_frag; pos++) { 818719Sroot if ((fragmap & field) == subfield) { 828719Sroot fraglist[siz] += cnt; 838719Sroot pos += siz; 848719Sroot field <<= siz; 858719Sroot subfield <<= siz; 868719Sroot } 878719Sroot field <<= 1; 888719Sroot subfield <<= 1; 898719Sroot } 908719Sroot } 918719Sroot } 928719Sroot 9351636Sbostic #if defined(KERNEL) && defined(DIAGNOSTIC) 9451471Sbostic void 9551471Sbostic ffs_checkoverlap(bp, ip) 9651471Sbostic struct buf *bp; 9751471Sbostic struct inode *ip; 9851471Sbostic { 9951471Sbostic register struct buf *ebp, *ep; 10051471Sbostic register daddr_t start, last; 10151471Sbostic struct vnode *vp; 10251471Sbostic 10351471Sbostic ebp = &buf[nbuf]; 10451471Sbostic start = bp->b_blkno; 10551471Sbostic last = start + btodb(bp->b_bcount) - 1; 10651471Sbostic for (ep = buf; ep < ebp; ep++) { 10751471Sbostic if (ep == bp || (ep->b_flags & B_INVAL) || 10851471Sbostic ep->b_vp == NULLVP) 10951471Sbostic continue; 11051471Sbostic if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0)) 11151471Sbostic continue; 11251471Sbostic if (vp != ip->i_devvp) 11351471Sbostic continue; 11451471Sbostic /* look for overlap */ 11551471Sbostic if (ep->b_bcount == 0 || ep->b_blkno > last || 11651471Sbostic ep->b_blkno + btodb(ep->b_bcount) <= start) 11751471Sbostic continue; 11851471Sbostic vprint("Disk overlap", vp); 11951471Sbostic (void)printf("\tstart %d, end %d overlap start %d, end %d\n", 12051471Sbostic start, last, ep->b_blkno, 12151471Sbostic ep->b_blkno + btodb(ep->b_bcount) - 1); 12251471Sbostic panic("Disk buffer overlap"); 12351471Sbostic } 12451471Sbostic } 12551471Sbostic #endif /* DIAGNOSTIC */ 12651471Sbostic 1278719Sroot /* 1288719Sroot * block operations 1298719Sroot * 1308719Sroot * check if a block is available 1318719Sroot */ 13251471Sbostic int 13351471Sbostic ffs_isblock(fs, cp, h) 1348719Sroot struct fs *fs; 1358719Sroot unsigned char *cp; 1368719Sroot daddr_t h; 1378719Sroot { 1388719Sroot unsigned char mask; 1398719Sroot 14026309Skarels switch ((int)fs->fs_frag) { 1418719Sroot case 8: 1428719Sroot return (cp[h] == 0xff); 1438719Sroot case 4: 1448719Sroot mask = 0x0f << ((h & 0x1) << 2); 1458719Sroot return ((cp[h >> 1] & mask) == mask); 1468719Sroot case 2: 1478719Sroot mask = 0x03 << ((h & 0x3) << 1); 1488719Sroot return ((cp[h >> 2] & mask) == mask); 1498719Sroot case 1: 1508719Sroot mask = 0x01 << (h & 0x7); 1518719Sroot return ((cp[h >> 3] & mask) == mask); 1528719Sroot default: 15351471Sbostic panic("ffs_isblock"); 1548719Sroot } 1558719Sroot } 1568719Sroot 1578719Sroot /* 1588719Sroot * take a block out of the map 1598719Sroot */ 16051471Sbostic void 16151471Sbostic ffs_clrblock(fs, cp, h) 1628719Sroot struct fs *fs; 1638770Sroot u_char *cp; 1648719Sroot daddr_t h; 1658719Sroot { 1668719Sroot 16726309Skarels switch ((int)fs->fs_frag) { 1688719Sroot case 8: 1698719Sroot cp[h] = 0; 1708719Sroot return; 1718719Sroot case 4: 1728719Sroot cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1738719Sroot return; 1748719Sroot case 2: 1758719Sroot cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1768719Sroot return; 1778719Sroot case 1: 1788719Sroot cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1798719Sroot return; 1808719Sroot default: 18151471Sbostic panic("ffs_clrblock"); 1828719Sroot } 1838719Sroot } 1848719Sroot 1858719Sroot /* 1868719Sroot * put a block into the map 1878719Sroot */ 18851471Sbostic void 18951471Sbostic ffs_setblock(fs, cp, h) 1908719Sroot struct fs *fs; 1918719Sroot unsigned char *cp; 1928719Sroot daddr_t h; 1938719Sroot { 1948719Sroot 19526309Skarels switch ((int)fs->fs_frag) { 1968719Sroot 1978719Sroot case 8: 1988719Sroot cp[h] = 0xff; 1998719Sroot return; 2008719Sroot case 4: 2018719Sroot cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 2028719Sroot return; 2038719Sroot case 2: 2048719Sroot cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 2058719Sroot return; 2068719Sroot case 1: 2078719Sroot cp[h >> 3] |= (0x01 << (h & 0x7)); 2088719Sroot return; 2098719Sroot default: 21051471Sbostic panic("ffs_setblock"); 2118719Sroot } 2128719Sroot } 213