386BSD 0.1 development
[unix-history] / usr / src / sys.386bsd / ufs / ufs_subr.c
CommitLineData
b688fc87
WJ
1/*
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)ufs_subr.c 7.13 (Berkeley) 6/28/90
34 */
35
36#ifdef KERNEL
37#include "param.h"
38#include "../ufs/fs.h"
39#else
40#include <sys/param.h>
41#include <ufs/fs.h>
42#endif
43
44extern int around[9];
45extern int inside[9];
46extern u_char *fragtbl[];
47
48/*
49 * Update the frsum fields to reflect addition or deletion
50 * of some frags.
51 */
52fragacct(fs, fragmap, fraglist, cnt)
53 struct fs *fs;
54 int fragmap;
55 long fraglist[];
56 int cnt;
57{
58 int inblk;
59 register int field, subfield;
60 register int siz, pos;
61
62 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
63 fragmap <<= 1;
64 for (siz = 1; siz < fs->fs_frag; siz++) {
65 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
66 continue;
67 field = around[siz];
68 subfield = inside[siz];
69 for (pos = siz; pos <= fs->fs_frag; pos++) {
70 if ((fragmap & field) == subfield) {
71 fraglist[siz] += cnt;
72 pos += siz;
73 field <<= siz;
74 subfield <<= siz;
75 }
76 field <<= 1;
77 subfield <<= 1;
78 }
79 }
80}
81
82/*
83 * block operations
84 *
85 * check if a block is available
86 */
87isblock(fs, cp, h)
88 struct fs *fs;
89 unsigned char *cp;
90 daddr_t h;
91{
92 unsigned char mask;
93
94 switch ((int)fs->fs_frag) {
95 case 8:
96 return (cp[h] == 0xff);
97 case 4:
98 mask = 0x0f << ((h & 0x1) << 2);
99 return ((cp[h >> 1] & mask) == mask);
100 case 2:
101 mask = 0x03 << ((h & 0x3) << 1);
102 return ((cp[h >> 2] & mask) == mask);
103 case 1:
104 mask = 0x01 << (h & 0x7);
105 return ((cp[h >> 3] & mask) == mask);
106 default:
107 panic("isblock");
108 return (NULL);
109 }
110}
111
112/*
113 * take a block out of the map
114 */
115clrblock(fs, cp, h)
116 struct fs *fs;
117 u_char *cp;
118 daddr_t h;
119{
120
121 switch ((int)fs->fs_frag) {
122 case 8:
123 cp[h] = 0;
124 return;
125 case 4:
126 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
127 return;
128 case 2:
129 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
130 return;
131 case 1:
132 cp[h >> 3] &= ~(0x01 << (h & 0x7));
133 return;
134 default:
135 panic("clrblock");
136 }
137}
138
139/*
140 * put a block into the map
141 */
142setblock(fs, cp, h)
143 struct fs *fs;
144 unsigned char *cp;
145 daddr_t h;
146{
147
148 switch ((int)fs->fs_frag) {
149
150 case 8:
151 cp[h] = 0xff;
152 return;
153 case 4:
154 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
155 return;
156 case 2:
157 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
158 return;
159 case 1:
160 cp[h >> 3] |= (0x01 << (h & 0x7));
161 return;
162 default:
163 panic("setblock");
164 }
165}
166
167#if (!defined(vax) && !defined(tahoe) && !defined(hp300)) \
168 || defined(VAX630) || defined(VAX650)
169/*
170 * C definitions of special instructions.
171 * Normally expanded with inline.
172 */
173scanc(size, cp, table, mask)
174 u_int size;
175 register u_char *cp, table[];
176 register u_char mask;
177{
178 register u_char *end = &cp[size];
179
180 while (cp < end && (table[*cp] & mask) == 0)
181 cp++;
182 return (end - cp);
183}
184#endif
185
186#if !defined(vax) && !defined(tahoe) && !defined(hp300)
187skpc(mask, size, cp)
188 register u_char mask;
189 u_int size;
190 register u_char *cp;
191{
192 register u_char *end = &cp[size];
193
194 while (cp < end && *cp == mask)
195 cp++;
196 return (end - cp);
197}
198
199locc(mask, size, cp)
200 register u_char mask;
201 u_int size;
202 register u_char *cp;
203{
204 register u_char *end = &cp[size];
205
206 while (cp < end && *cp != mask)
207 cp++;
208 return (end - cp);
209}
210#endif