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 * $Id: ufs_subr.c,v 1.1 94/10/20 10:56:44 root Exp $
34 */
35
36 #include <sys/param.h>
37
38 #include "ufs.h"
39 #ifdef KERNEL
40 #include "sys/errno.h"
41 #include "prototypes.h"
42 #endif
43
44
45 extern int around[9];
46 extern int inside[9];
47 extern u_char *fragtbl[];
48
49 /*
50 * Update the frsum fields to reflect addition or deletion
51 * of some frags.
52 */
53 fragacct(fs, fragmap, fraglist, cnt)
54 struct fs *fs;
55 int fragmap;
56 long fraglist[];
57 int cnt;
58 {
59 int inblk;
60 register int field, subfield;
61 register int siz, pos;
62
63 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
64 fragmap <<= 1;
65 for (siz = 1; siz < fs->fs_frag; siz++) {
66 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
67 continue;
68 field = around[siz];
69 subfield = inside[siz];
70 for (pos = siz; pos <= fs->fs_frag; pos++) {
71 if ((fragmap & field) == subfield) {
72 fraglist[siz] += cnt;
73 pos += siz;
74 field <<= siz;
75 subfield <<= siz;
76 }
77 field <<= 1;
78 subfield <<= 1;
79 }
80 }
81 }
82
83 /*
84 * block operations
85 *
86 * check if a block is available
87 */
88 isblock(fs, cp, h)
89 struct fs *fs;
90 unsigned char *cp;
91 daddr_t h;
92 {
93 unsigned char mask;
94
95 switch ((int)fs->fs_frag) {
96 case 8:
97 return (cp[h] == 0xff);
98 case 4:
99 mask = 0x0f << ((h & 0x1) << 2);
100 return ((cp[h >> 1] & mask) == mask);
101 case 2:
102 mask = 0x03 << ((h & 0x3) << 1);
103 return ((cp[h >> 2] & mask) == mask);
104 case 1:
105 mask = 0x01 << (h & 0x7);
106 return ((cp[h >> 3] & mask) == mask);
107 default:
108 panic("isblock");
109 return (NULL);
110 }
111 }
112
113 /*
114 * take a block out of the map
115 */
116 clrblock(fs, cp, h)
117 struct fs *fs;
118 u_char *cp;
119 daddr_t h;
120 {
121
122 switch ((int)fs->fs_frag) {
123 case 8:
124 cp[h] = 0;
125 return;
126 case 4:
127 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
128 return;
129 case 2:
130 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
131 return;
132 case 1:
133 cp[h >> 3] &= ~(0x01 << (h & 0x7));
134 return;
135 default:
136 panic("clrblock");
137 }
138 }
139
140 /*
141 * put a block into the map
142 */
143 setblock(fs, cp, h)
144 struct fs *fs;
145 unsigned char *cp;
146 daddr_t h;
147 {
148
149 switch ((int)fs->fs_frag) {
150
151 case 8:
152 cp[h] = 0xff;
153 return;
154 case 4:
155 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
156 return;
157 case 2:
158 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
159 return;
160 case 1:
161 cp[h >> 3] |= (0x01 << (h & 0x7));
162 return;
163 default:
164 panic("setblock");
165 }
166 }
167
168 #ifdef nope
169 #if (!defined(vax) && !defined(tahoe) && !defined(hp300)) \
170 || defined(VAX630) || defined(VAX650)
171 /*
172 * C definitions of special instructions.
173 * Normally expanded with inline.
174 */
scanc(size,cp,table,mask)175 scanc(size, cp, table, mask)
176 u_int size;
177 register u_char *cp, table[];
178 register u_char mask;
179 {
180 register u_char *end = &cp[size];
181
182 while (cp < end && (table[*cp] & mask) == 0)
183 cp++;
184 return (end - cp);
185 }
186 #endif
187
188 #if !defined(vax) && !defined(tahoe) && !defined(hp300)
skpc(mask,size,cp)189 skpc(mask, size, cp)
190 register u_char mask;
191 u_int size;
192 register u_char *cp;
193 {
194 register u_char *end = &cp[size];
195
196 while (cp < end && *cp == mask)
197 cp++;
198 return (end - cp);
199 }
200
locc(mask,size,cp)201 locc(mask, size, cp)
202 register u_char mask;
203 u_int size;
204 register u_char *cp;
205 {
206 register u_char *end = &cp[size];
207
208 while (cp < end && *cp != mask)
209 cp++;
210 return (end - cp);
211 }
212 #endif
213 #endif
214