xref: /csrg-svn/sys/ufs/ffs/ffs_subr.c (revision 8719)
1*8719Sroot /*	ffs_subr.c	4.1	82/10/20	*/
2*8719Sroot 
3*8719Sroot #ifdef KERNEL
4*8719Sroot #include "../h/param.h"
5*8719Sroot #include "../h/fs.h"
6*8719Sroot #else
7*8719Sroot #include <sys/param.h>
8*8719Sroot #include <sys/fs.h>
9*8719Sroot #endif
10*8719Sroot 
11*8719Sroot extern	int around[9];
12*8719Sroot extern	int inside[9];
13*8719Sroot extern	u_char *fragtbl[];
14*8719Sroot 
15*8719Sroot /*
16*8719Sroot  * Update the frsum fields to reflect addition or deletion
17*8719Sroot  * of some frags.
18*8719Sroot  */
19*8719Sroot fragacct(fs, fragmap, fraglist, cnt)
20*8719Sroot 	struct fs *fs;
21*8719Sroot 	int fragmap;
22*8719Sroot 	long fraglist[];
23*8719Sroot 	int cnt;
24*8719Sroot {
25*8719Sroot 	int inblk;
26*8719Sroot 	register int field, subfield;
27*8719Sroot 	register int siz, pos;
28*8719Sroot 
29*8719Sroot 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
30*8719Sroot 	fragmap <<= 1;
31*8719Sroot 	for (siz = 1; siz < fs->fs_frag; siz++) {
32*8719Sroot 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
33*8719Sroot 			continue;
34*8719Sroot 		field = around[siz];
35*8719Sroot 		subfield = inside[siz];
36*8719Sroot 		for (pos = siz; pos <= fs->fs_frag; pos++) {
37*8719Sroot 			if ((fragmap & field) == subfield) {
38*8719Sroot 				fraglist[siz] += cnt;
39*8719Sroot 				pos += siz;
40*8719Sroot 				field <<= siz;
41*8719Sroot 				subfield <<= siz;
42*8719Sroot 			}
43*8719Sroot 			field <<= 1;
44*8719Sroot 			subfield <<= 1;
45*8719Sroot 		}
46*8719Sroot 	}
47*8719Sroot }
48*8719Sroot 
49*8719Sroot #ifdef KERNEL
50*8719Sroot /*
51*8719Sroot  * Check that a specified block number is in range.
52*8719Sroot  */
53*8719Sroot badblock(fs, bn)
54*8719Sroot 	register struct fs *fs;
55*8719Sroot 	daddr_t bn;
56*8719Sroot {
57*8719Sroot 
58*8719Sroot 	if ((unsigned)bn >= fs->fs_size) {
59*8719Sroot 		printf("bad block %d, ", bn);
60*8719Sroot 		fserr(fs, "bad block");
61*8719Sroot 		return (1);
62*8719Sroot 	}
63*8719Sroot 	return (0);
64*8719Sroot }
65*8719Sroot #endif
66*8719Sroot 
67*8719Sroot /*
68*8719Sroot  * block operations
69*8719Sroot  *
70*8719Sroot  * check if a block is available
71*8719Sroot  */
72*8719Sroot isblock(fs, cp, h)
73*8719Sroot 	struct fs *fs;
74*8719Sroot 	unsigned char *cp;
75*8719Sroot 	daddr_t h;
76*8719Sroot {
77*8719Sroot 	unsigned char mask;
78*8719Sroot 
79*8719Sroot 	switch (fs->fs_frag) {
80*8719Sroot 	case 8:
81*8719Sroot 		return (cp[h] == 0xff);
82*8719Sroot 	case 4:
83*8719Sroot 		mask = 0x0f << ((h & 0x1) << 2);
84*8719Sroot 		return ((cp[h >> 1] & mask) == mask);
85*8719Sroot 	case 2:
86*8719Sroot 		mask = 0x03 << ((h & 0x3) << 1);
87*8719Sroot 		return ((cp[h >> 2] & mask) == mask);
88*8719Sroot 	case 1:
89*8719Sroot 		mask = 0x01 << (h & 0x7);
90*8719Sroot 		return ((cp[h >> 3] & mask) == mask);
91*8719Sroot 	default:
92*8719Sroot 		panic("isblock");
93*8719Sroot 		return (NULL);
94*8719Sroot 	}
95*8719Sroot }
96*8719Sroot 
97*8719Sroot /*
98*8719Sroot  * take a block out of the map
99*8719Sroot  */
100*8719Sroot clrblock(fs, cp, h)
101*8719Sroot 	struct fs *fs;
102*8719Sroot 	unsigned char *cp;
103*8719Sroot 	daddr_t h;
104*8719Sroot {
105*8719Sroot 
106*8719Sroot 	switch ((fs)->fs_frag) {
107*8719Sroot 	case 8:
108*8719Sroot 		cp[h] = 0;
109*8719Sroot 		return;
110*8719Sroot 	case 4:
111*8719Sroot 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
112*8719Sroot 		return;
113*8719Sroot 	case 2:
114*8719Sroot 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
115*8719Sroot 		return;
116*8719Sroot 	case 1:
117*8719Sroot 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
118*8719Sroot 		return;
119*8719Sroot 	default:
120*8719Sroot 		panic("clrblock");
121*8719Sroot 	}
122*8719Sroot }
123*8719Sroot 
124*8719Sroot /*
125*8719Sroot  * put a block into the map
126*8719Sroot  */
127*8719Sroot setblock(fs, cp, h)
128*8719Sroot 	struct fs *fs;
129*8719Sroot 	unsigned char *cp;
130*8719Sroot 	daddr_t h;
131*8719Sroot {
132*8719Sroot 
133*8719Sroot 	switch (fs->fs_frag) {
134*8719Sroot 
135*8719Sroot 	case 8:
136*8719Sroot 		cp[h] = 0xff;
137*8719Sroot 		return;
138*8719Sroot 	case 4:
139*8719Sroot 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
140*8719Sroot 		return;
141*8719Sroot 	case 2:
142*8719Sroot 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
143*8719Sroot 		return;
144*8719Sroot 	case 1:
145*8719Sroot 		cp[h >> 3] |= (0x01 << (h & 0x7));
146*8719Sroot 		return;
147*8719Sroot 	default:
148*8719Sroot 		panic("setblock");
149*8719Sroot 	}
150*8719Sroot }
151