xref: /csrg-svn/sys/ufs/ffs/ffs_subr.c (revision 54304)
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*54304Smckusick  *	@(#)ffs_subr.c	7.25 (Berkeley) 06/23/92
823402Smckusick  */
98719Sroot 
108719Sroot #include <sys/param.h>
1151471Sbostic #include <ufs/ffs/fs.h>
1251471Sbostic 
1351636Sbostic #ifdef KERNEL
14*54304Smckusick #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
2753521Sheideman ffs_blkatoff (ap)
2853521Sheideman 	struct vop_blkatoff_args *ap;
2951471Sbostic {
3051543Smckusick 	struct inode *ip;
3151471Sbostic 	register struct fs *fs;
3251471Sbostic 	struct buf *bp;
3351471Sbostic 	daddr_t lbn;
3451471Sbostic 	int bsize, error;
3551471Sbostic 
3653586Sheideman 	ip = VTOI(ap->a_vp);
3751471Sbostic 	fs = ip->i_fs;
3853586Sheideman 	lbn = lblkno(fs, ap->a_offset);
3951471Sbostic 	bsize = blksize(fs, ip, lbn);
4051471Sbostic 
4153586Sheideman 	*ap->a_bpp = NULL;
4253586Sheideman 	if (error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) {
4351471Sbostic 		brelse(bp);
4451471Sbostic 		return (error);
4551471Sbostic 	}
4653586Sheideman 	if (ap->a_res)
4753586Sheideman 		*ap->a_res = bp->b_un.b_addr + blkoff(fs, ap->a_offset);
4853586Sheideman 	*ap->a_bpp = bp;
4951471Sbostic 	return (0);
5051471Sbostic }
5151636Sbostic #endif
5251471Sbostic 
5351471Sbostic /*
548719Sroot  * Update the frsum fields to reflect addition or deletion
558719Sroot  * of some frags.
568719Sroot  */
5751471Sbostic void
5851471Sbostic ffs_fragacct(fs, fragmap, fraglist, cnt)
598719Sroot 	struct fs *fs;
608719Sroot 	int fragmap;
618719Sroot 	long fraglist[];
628719Sroot 	int cnt;
638719Sroot {
648719Sroot 	int inblk;
658719Sroot 	register int field, subfield;
668719Sroot 	register int siz, pos;
678719Sroot 
688719Sroot 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
698719Sroot 	fragmap <<= 1;
708719Sroot 	for (siz = 1; siz < fs->fs_frag; siz++) {
718719Sroot 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
728719Sroot 			continue;
738719Sroot 		field = around[siz];
748719Sroot 		subfield = inside[siz];
758719Sroot 		for (pos = siz; pos <= fs->fs_frag; pos++) {
768719Sroot 			if ((fragmap & field) == subfield) {
778719Sroot 				fraglist[siz] += cnt;
788719Sroot 				pos += siz;
798719Sroot 				field <<= siz;
808719Sroot 				subfield <<= siz;
818719Sroot 			}
828719Sroot 			field <<= 1;
838719Sroot 			subfield <<= 1;
848719Sroot 		}
858719Sroot 	}
868719Sroot }
878719Sroot 
8851636Sbostic #if defined(KERNEL) && defined(DIAGNOSTIC)
8951471Sbostic void
9051471Sbostic ffs_checkoverlap(bp, ip)
9151471Sbostic 	struct buf *bp;
9251471Sbostic 	struct inode *ip;
9351471Sbostic {
9453521Sheideman 	USES_VOP_BMAP;
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 }
209