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