xref: /csrg-svn/sys/ufs/ffs/ffs_subr.c (revision 51956)
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*51956Smckusick  *	@(#)ffs_subr.c	7.17 (Berkeley) 12/15/91
823402Smckusick  */
98719Sroot 
108719Sroot #include <sys/param.h>
1151471Sbostic #include <ufs/ffs/fs.h>
1251471Sbostic #include <ufs/ffs/ffs_extern.h>
1351471Sbostic 
1451636Sbostic #ifdef KERNEL
15*51956Smckusick #include <sys/buf.h>
1651636Sbostic #include <sys/vnode.h>
17*51956Smckusick #include <ufs/ufs/quota.h>
18*51956Smckusick #include <ufs/ufs/inode.h>
1951636Sbostic 
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
2651543Smckusick ffs_blkatoff(vp, offset, res, bpp)
2751543Smckusick 	struct vnode *vp;
2851471Sbostic 	off_t offset;
2951471Sbostic 	char **res;
3051471Sbostic 	struct buf **bpp;
3151471Sbostic {
3251543Smckusick 	struct inode *ip;
3351471Sbostic 	register struct fs *fs;
3451471Sbostic 	struct buf *bp;
3551471Sbostic 	daddr_t lbn;
3651471Sbostic 	int bsize, error;
3751471Sbostic 
3851543Smckusick 	ip = VTOI(vp);
3951471Sbostic 	fs = ip->i_fs;
4051471Sbostic 	lbn = lblkno(fs, offset);
4151471Sbostic 	bsize = blksize(fs, ip, lbn);
4251471Sbostic 
4351471Sbostic 	*bpp = NULL;
4451543Smckusick 	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 }
5351636Sbostic #endif
5451471Sbostic 
5551471Sbostic /*
568719Sroot  * Update the frsum fields to reflect addition or deletion
578719Sroot  * of some frags.
588719Sroot  */
5951471Sbostic void
6051471Sbostic ffs_fragacct(fs, fragmap, fraglist, cnt)
618719Sroot 	struct fs *fs;
628719Sroot 	int fragmap;
638719Sroot 	long fraglist[];
648719Sroot 	int cnt;
658719Sroot {
668719Sroot 	int inblk;
678719Sroot 	register int field, subfield;
688719Sroot 	register int siz, pos;
698719Sroot 
708719Sroot 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
718719Sroot 	fragmap <<= 1;
728719Sroot 	for (siz = 1; siz < fs->fs_frag; siz++) {
738719Sroot 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
748719Sroot 			continue;
758719Sroot 		field = around[siz];
768719Sroot 		subfield = inside[siz];
778719Sroot 		for (pos = siz; pos <= fs->fs_frag; pos++) {
788719Sroot 			if ((fragmap & field) == subfield) {
798719Sroot 				fraglist[siz] += cnt;
808719Sroot 				pos += siz;
818719Sroot 				field <<= siz;
828719Sroot 				subfield <<= siz;
838719Sroot 			}
848719Sroot 			field <<= 1;
858719Sroot 			subfield <<= 1;
868719Sroot 		}
878719Sroot 	}
888719Sroot }
898719Sroot 
9051636Sbostic #if defined(KERNEL) && defined(DIAGNOSTIC)
9151471Sbostic void
9251471Sbostic ffs_checkoverlap(bp, ip)
9351471Sbostic 	struct buf *bp;
9451471Sbostic 	struct inode *ip;
9551471Sbostic {
9651471Sbostic 	register struct buf *ebp, *ep;
9751471Sbostic 	register daddr_t start, last;
9851471Sbostic 	struct vnode *vp;
9951471Sbostic 
10051471Sbostic 	ebp = &buf[nbuf];
10151471Sbostic 	start = bp->b_blkno;
10251471Sbostic 	last = start + btodb(bp->b_bcount) - 1;
10351471Sbostic 	for (ep = buf; ep < ebp; ep++) {
10451471Sbostic 		if (ep == bp || (ep->b_flags & B_INVAL) ||
10551471Sbostic 		    ep->b_vp == NULLVP)
10651471Sbostic 			continue;
10751471Sbostic 		if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0))
10851471Sbostic 			continue;
10951471Sbostic 		if (vp != ip->i_devvp)
11051471Sbostic 			continue;
11151471Sbostic 		/* look for overlap */
11251471Sbostic 		if (ep->b_bcount == 0 || ep->b_blkno > last ||
11351471Sbostic 		    ep->b_blkno + btodb(ep->b_bcount) <= start)
11451471Sbostic 			continue;
11551471Sbostic 		vprint("Disk overlap", vp);
11651471Sbostic 		(void)printf("\tstart %d, end %d overlap start %d, end %d\n",
11751471Sbostic 			start, last, ep->b_blkno,
11851471Sbostic 			ep->b_blkno + btodb(ep->b_bcount) - 1);
11951471Sbostic 		panic("Disk buffer overlap");
12051471Sbostic 	}
12151471Sbostic }
12251471Sbostic #endif /* DIAGNOSTIC */
12351471Sbostic 
1248719Sroot /*
1258719Sroot  * block operations
1268719Sroot  *
1278719Sroot  * check if a block is available
1288719Sroot  */
12951471Sbostic int
13051471Sbostic ffs_isblock(fs, cp, h)
1318719Sroot 	struct fs *fs;
1328719Sroot 	unsigned char *cp;
1338719Sroot 	daddr_t h;
1348719Sroot {
1358719Sroot 	unsigned char mask;
1368719Sroot 
13726309Skarels 	switch ((int)fs->fs_frag) {
1388719Sroot 	case 8:
1398719Sroot 		return (cp[h] == 0xff);
1408719Sroot 	case 4:
1418719Sroot 		mask = 0x0f << ((h & 0x1) << 2);
1428719Sroot 		return ((cp[h >> 1] & mask) == mask);
1438719Sroot 	case 2:
1448719Sroot 		mask = 0x03 << ((h & 0x3) << 1);
1458719Sroot 		return ((cp[h >> 2] & mask) == mask);
1468719Sroot 	case 1:
1478719Sroot 		mask = 0x01 << (h & 0x7);
1488719Sroot 		return ((cp[h >> 3] & mask) == mask);
1498719Sroot 	default:
15051471Sbostic 		panic("ffs_isblock");
1518719Sroot 	}
1528719Sroot }
1538719Sroot 
1548719Sroot /*
1558719Sroot  * take a block out of the map
1568719Sroot  */
15751471Sbostic void
15851471Sbostic ffs_clrblock(fs, cp, h)
1598719Sroot 	struct fs *fs;
1608770Sroot 	u_char *cp;
1618719Sroot 	daddr_t h;
1628719Sroot {
1638719Sroot 
16426309Skarels 	switch ((int)fs->fs_frag) {
1658719Sroot 	case 8:
1668719Sroot 		cp[h] = 0;
1678719Sroot 		return;
1688719Sroot 	case 4:
1698719Sroot 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1708719Sroot 		return;
1718719Sroot 	case 2:
1728719Sroot 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1738719Sroot 		return;
1748719Sroot 	case 1:
1758719Sroot 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
1768719Sroot 		return;
1778719Sroot 	default:
17851471Sbostic 		panic("ffs_clrblock");
1798719Sroot 	}
1808719Sroot }
1818719Sroot 
1828719Sroot /*
1838719Sroot  * put a block into the map
1848719Sroot  */
18551471Sbostic void
18651471Sbostic ffs_setblock(fs, cp, h)
1878719Sroot 	struct fs *fs;
1888719Sroot 	unsigned char *cp;
1898719Sroot 	daddr_t h;
1908719Sroot {
1918719Sroot 
19226309Skarels 	switch ((int)fs->fs_frag) {
1938719Sroot 
1948719Sroot 	case 8:
1958719Sroot 		cp[h] = 0xff;
1968719Sroot 		return;
1978719Sroot 	case 4:
1988719Sroot 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1998719Sroot 		return;
2008719Sroot 	case 2:
2018719Sroot 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
2028719Sroot 		return;
2038719Sroot 	case 1:
2048719Sroot 		cp[h >> 3] |= (0x01 << (h & 0x7));
2058719Sroot 		return;
2068719Sroot 	default:
20751471Sbostic 		panic("ffs_setblock");
2088719Sroot 	}
2098719Sroot }
2109167Ssam 
21141537Smckusick #if (!defined(vax) && !defined(tahoe) && !defined(hp300)) \
21241537Smckusick 	|| defined(VAX630) || defined(VAX650)
21321090Smckusick /*
21429947Skarels  * C definitions of special instructions.
21529947Skarels  * Normally expanded with inline.
21621090Smckusick  */
21751471Sbostic int
21821090Smckusick scanc(size, cp, table, mask)
21921090Smckusick 	u_int size;
22021090Smckusick 	register u_char *cp, table[];
22121090Smckusick 	register u_char mask;
22221090Smckusick {
22321090Smckusick 	register u_char *end = &cp[size];
22421090Smckusick 
22521090Smckusick 	while (cp < end && (table[*cp] & mask) == 0)
22621090Smckusick 		cp++;
22721090Smckusick 	return (end - cp);
22821090Smckusick }
22927476Skridle #endif
23027476Skridle 
23141537Smckusick #if !defined(vax) && !defined(tahoe) && !defined(hp300)
23251471Sbostic int
23321090Smckusick skpc(mask, size, cp)
23421090Smckusick 	register u_char mask;
23521090Smckusick 	u_int size;
23621090Smckusick 	register u_char *cp;
23721090Smckusick {
23821090Smckusick 	register u_char *end = &cp[size];
23921090Smckusick 
24021090Smckusick 	while (cp < end && *cp == mask)
24121090Smckusick 		cp++;
24221090Smckusick 	return (end - cp);
24321090Smckusick }
24421090Smckusick 
24551471Sbostic int
24621090Smckusick locc(mask, size, cp)
24721090Smckusick 	register u_char mask;
24821090Smckusick 	u_int size;
24921090Smckusick 	register u_char *cp;
25021090Smckusick {
25121090Smckusick 	register u_char *end = &cp[size];
25221090Smckusick 
25321090Smckusick 	while (cp < end && *cp != mask)
25421090Smckusick 		cp++;
25521090Smckusick 	return (end - cp);
25621090Smckusick }
25729947Skarels #endif
258