vfsck: Tidy header includes
[openafs.git] / src / vfsck / ufs_subr.c
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 are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *      @(#)ufs_subr.c  7.11 (Berkeley) 12/30/89
18  */
19
20 #include <afsconfig.h>
21 #include <afs/param.h>
22
23 #include <roken.h>
24
25 #define VICE
26
27 #ifdef  AFS_OSF_ENV
28 #include <ufs/fs.h>
29 #else /* AFS_OSF_ENV */
30 #ifdef AFS_VFSINCL_ENV
31 #ifdef  AFS_SUN5_ENV
32 #include <sys/fs/ufs_fs.h>
33 #else
34 #include <ufs/fs.h>
35 #endif
36 #else /* AFS_VFSINCL_ENV */
37 #include <sys/fs.h>
38 #endif /* AFS_VFSINCL_ENV */
39 #endif /* AFS_OSF_ENV */
40
41 extern int around[9];
42 extern int inside[9];
43 extern u_char *fragtbl[];
44
45 /*
46  * Update the frsum fields to reflect addition or deletion
47  * of some frags.
48  */
49 fragacct(fs, fragmap, fraglist, cnt)
50      struct fs *fs;
51      int fragmap;
52      afs_int32 fraglist[];
53      int cnt;
54 {
55     int inblk;
56     int field, subfield;
57     int siz, pos;
58
59     inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
60     fragmap <<= 1;
61     for (siz = 1; siz < fs->fs_frag; siz++) {
62         if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
63             continue;
64         field = around[siz];
65         subfield = inside[siz];
66         for (pos = siz; pos <= fs->fs_frag; pos++) {
67             if ((fragmap & field) == subfield) {
68                 fraglist[siz] += cnt;
69                 pos += siz;
70                 field <<= siz;
71                 subfield <<= siz;
72             }
73             field <<= 1;
74             subfield <<= 1;
75         }
76     }
77 }
78
79 /*
80  * block operations
81  *
82  * check if a block is available
83  */
84 isblock(fs, cp, h)
85      struct fs *fs;
86      unsigned char *cp;
87      daddr_t h;
88 {
89     unsigned char mask;
90
91     switch ((int)fs->fs_frag) {
92     case 8:
93         return (cp[h] == 0xff);
94     case 4:
95         mask = 0x0f << ((h & 0x1) << 2);
96         return ((cp[h >> 1] & mask) == mask);
97     case 2:
98         mask = 0x03 << ((h & 0x3) << 1);
99         return ((cp[h >> 2] & mask) == mask);
100     case 1:
101         mask = 0x01 << (h & 0x7);
102         return ((cp[h >> 3] & mask) == mask);
103     default:
104         panic("isblock");
105         return (NULL);
106     }
107 }
108
109 /*
110  * take a block out of the map
111  */
112 clrblock(fs, cp, h)
113      struct fs *fs;
114      u_char *cp;
115      daddr_t h;
116 {
117
118     switch ((int)fs->fs_frag) {
119     case 8:
120         cp[h] = 0;
121         return;
122     case 4:
123         cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
124         return;
125     case 2:
126         cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
127         return;
128     case 1:
129         cp[h >> 3] &= ~(0x01 << (h & 0x7));
130         return;
131     default:
132         panic("clrblock");
133     }
134 }
135
136 /*
137  * put a block into the map
138  */
139 setblock(fs, cp, h)
140      struct fs *fs;
141      unsigned char *cp;
142      daddr_t h;
143 {
144
145     switch ((int)fs->fs_frag) {
146
147     case 8:
148         cp[h] = 0xff;
149         return;
150     case 4:
151         cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
152         return;
153     case 2:
154         cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
155         return;
156     case 1:
157         cp[h >> 3] |= (0x01 << (h & 0x7));
158         return;
159     default:
160         panic("setblock");
161     }
162 }
163
164 #if (!defined(vax) && !defined(tahoe)) || defined(VAX630) || defined(VAX650)
165 /*
166  * C definitions of special instructions.
167  * Normally expanded with inline.
168  */
169 scanc(size, cp, table, mask)
170      u_int size;
171      u_char *cp, table[];
172      u_char mask;
173 {
174     u_char *end = &cp[size];
175
176     while (cp < end && (table[*cp] & mask) == 0)
177         cp++;
178     return (end - cp);
179 }
180 #endif
181
182 #if !defined(vax) && !defined(tahoe)
183 skpc(mask, size, cp)
184      u_char mask;
185      u_int size;
186      u_char *cp;
187 {
188     u_char *end = &cp[size];
189
190     while (cp < end && *cp == mask)
191         cp++;
192     return (end - cp);
193 }
194
195 locc(mask, size, cp)
196      u_char mask;
197      u_int size;
198      u_char *cp;
199 {
200     u_char *end = &cp[size];
201
202     while (cp < end && *cp != mask)
203         cp++;
204     return (end - cp);
205 }
206 #endif