1 /* $NetBSD: filecore_utils.c,v 1.2 2003/08/07 16:31:38 agc Exp $ */
2
3 /*-
4 * Copyright (c) 1994 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code includes code derived from software contributed to the
8 * NetBSD project by Mark Brinicombe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * filecore_utils.c 1.1 1998/6/26
35 */
36
37 /*-
38 * Copyright (c) 1998 Andrew McMurry
39 *
40 * This code includes code derived from software contributed to the
41 * NetBSD project by Mark Brinicombe.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * filecore_utils.c 1.1 1998/6/26
72 */
73
74 /*
75 * Copyright (c) 1998 Christopher G. Demetriou. All rights reserved.
76 *
77 * Redistribution and use in source and binary forms, with or without
78 * modification, are permitted provided that the following conditions
79 * are met:
80 * 1. Redistributions of source code must retain the above copyright
81 * notice, this list of conditions and the following disclaimer.
82 * 2. Redistributions in binary form must reproduce the above copyright
83 * notice, this list of conditions and the following disclaimer in the
84 * documentation and/or other materials provided with the distribution.
85 * 3. All advertising materials mentioning features or use of this software
86 * must display the following acknowledgement:
87 * This product includes software developed by Christopher G. Demetriou
88 * for the NetBSD Project.
89 * 4. The name of the author may not be used to endorse or promote products
90 * derived from this software without specific prior written permission
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
93 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
94 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
95 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
96 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
98 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
99 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
100 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
101 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
102 */
103
104 #include <sys/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: filecore_utils.c,v 1.2 2003/08/07 16:31:38 agc Exp $");
106
107 #include <sys/param.h>
108 #include <sys/systm.h>
109 #include <sys/stat.h>
110 #include <sys/buf.h>
111 #include <sys/mount.h>
112 #include <sys/vnode.h>
113 #include <sys/dirent.h>
114
115 #include <fs/filecorefs/filecore.h>
116 #include <fs/filecorefs/filecore_extern.h>
117 #include <fs/filecorefs/filecore_node.h>
118 #include <fs/filecorefs/filecore_mount.h>
119
120 /*
121 * int filecore_bbchecksum(u_char *bootblock)
122 *
123 * Calculates the filecore boot block checksum. This is used to validate
124 * a filecore boot block on the disk. If a boot block is validated then
125 * it is used to locate the partition table. If the boot block is not
126 * validated, it is assumed that the whole disk is NetBSD.
127 *
128 * The basic algorithm is:
129 *
130 * for (each byte in block, excluding checksum) {
131 * sum += byte;
132 * if (sum > 255)
133 * sum -= 255;
134 * }
135 *
136 * That's equivalent to summing all of the bytes in the block
137 * (excluding the checksum byte, of course), then calculating the
138 * checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That
139 * expression may or may not yield a faster checksum function,
140 * but it's easier to reason about.
141 *
142 * Note that if you have a block filled with bytes of a single
143 * value "X" (regardless of that value!) and calculate the cksum
144 * of the block (excluding the checksum byte), you will _always_
145 * end up with a checksum of X. (Do the math; that can be derived
146 * from the checksum calculation function!) That means that
147 * blocks which contain bytes which all have the same value will
148 * always checksum properly. That's a _very_ unlikely occurence
149 * (probably impossible, actually) for a valid filecore boot block,
150 * so we treat such blocks as invalid.
151 */
152 int
153 filecore_bbchecksum(bb)
154 void *bb;
155 {
156 u_char *bootblock = bb;
157 u_char byte0, accum_diff;
158 u_int sum;
159 int i;
160
161 sum = 0;
162 accum_diff = 0;
163 byte0 = bootblock[0];
164
165 /*
166 * Sum the contents of the block, keeping track of whether
167 * or not all bytes are the same. If 'accum_diff' ends up
168 * being zero, all of the bytes are, in fact, the same.
169 */
170 for (i = 0; i < 511; ++i) {
171 sum += bootblock[i];
172 accum_diff |= bootblock[i] ^ byte0;
173 }
174
175 /*
176 * Check to see if the checksum byte is the same as the
177 * rest of the bytes, too. (Note that if all of the bytes
178 * are the same except the checksum, a checksum compare
179 * won't succeed, but that's not our problem.)
180 */
181 accum_diff |= bootblock[i] ^ byte0;
182 sum = (sum - ((sum - 1) / 255) * 255);
183
184 /*
185 * If all bytes in block are the same
186 * or the checksum does not match ; call it invalid.
187 */
188 if (accum_diff == 0 || sum != bootblock[511])
189 return -1;
190 return 0;
191 }
192
193 mode_t
194 filecore_mode(ip)
195 struct filecore_node *ip;
196 {
197 mode_t m = 0;
198 int rf = 0;
199
200 if ((ip->i_dirent.attr & FILECORE_ATTR_READ) ||
201 (ip->i_mnt->fc_mntflags & FILECOREMNT_OWNREAD) ||
202 (ip->i_dirent.attr & FILECORE_ATTR_DIR))
203 rf = 1;
204 if (ip->i_mnt->fc_mntflags & FILECOREMNT_ALLACCESS) {
205 m |= S_IRUSR | S_IXUSR;
206 if (rf || (ip->i_dirent.attr & FILECORE_ATTR_OREAD))
207 m |= S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
208 } else if (ip->i_mnt->fc_mntflags & FILECOREMNT_OWNACCESS) {
209 if (rf) m |= S_IRUSR | S_IXUSR;
210 if (ip->i_dirent.attr & FILECORE_ATTR_OREAD)
211 m |= S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
212 } else {
213 m |= S_IRUSR | S_IXUSR;
214 if (rf) m |= S_IRGRP | S_IXGRP;
215 if (ip->i_dirent.attr & FILECORE_ATTR_OREAD)
216 m |= S_IROTH | S_IXOTH;
217 }
218 if (ip->i_dirent.attr & FILECORE_ATTR_DIR) {
219 m |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
220 } else
221 m |= S_IFREG;
222 return m;
223 }
224
225 struct timespec
226 filecore_time(ip)
227 struct filecore_node *ip;
228 {
229 struct timespec ts;
230 u_int64_t cs;
231
232 cs = (((u_int64_t)(ip->i_dirent.load & 0xFF)) << 32)
233 + ip->i_dirent.exec - ((u_int64_t)1725772500 << 7);
234 ts.tv_sec = cs / 100;
235 ts.tv_nsec = (cs % 100) * 10000000;
236 return ts;
237 }
238
239 ino_t
240 filecore_getparent(ip)
241 struct filecore_node *ip;
242 {
243 struct buf *pbp;
244 u_int32_t addr;
245 u_int32_t paddr;
246 int error = 0;
247 int i = 0;
248
249 #ifdef FILECORE_DEBUG
250 printf("filecore_getparent(ino=%x)\n", ip->i_number);
251 #endif
252 if (ip->i_parent != -2) {
253 return ip->i_parent;
254 }
255 if (ip->i_number == FILECORE_ROOTINO) {
256 ip->i_parent = ip->i_number;
257 return ip->i_number;
258 }
259 addr = ip->i_number & FILECORE_INO_MASK;
260 /* Read directory data for parent dir to find its parent */
261 #ifdef FILECORE_DEBUG
262 printf("filecore_getparent() read parent dir contents\n");
263 #endif
264 error = filecore_bread(ip->i_mnt, addr, FILECORE_DIR_SIZE,
265 NOCRED, &pbp);
266 if (error) {
267 brelse(pbp);
268 return error;
269 }
270 paddr = fcdirtail(pbp->b_data)->parent1
271 | fcdirtail(pbp->b_data)->parent2 << 16;
272 #ifdef FILECORE_DEBUG_BR
273 printf("brelse(%p) ut1\n", pbp);
274 #endif
275 brelse(pbp);
276
277 /* If parent's parent is the parent then parent is root dir */
278 if (paddr == addr) {
279 ip->i_parent = FILECORE_ROOTINO;
280 return FILECORE_ROOTINO;
281 }
282
283 #ifdef FILECORE_DEBUG
284 printf("filecore_getparent() read grand-parent dir contents\n");
285 #endif
286 error = filecore_bread(ip->i_mnt, paddr, FILECORE_DIR_SIZE,
287 NOCRED, &pbp);
288 if (error) {
289 brelse(pbp);
290 return error;
291 }
292 while (fcdirentry(pbp->b_data,i)->addr != addr) {
293 if (fcdirentry(pbp->b_data, i++)->name[0] == 0) {
294 #ifdef FILECORE_DEBUG_BR
295 printf("brelse(%p) ut2\n", pbp);
296 #endif
297 brelse(pbp);
298 return FILECORE_ROOTINO;
299 }
300 }
301 #ifdef FILECORE_DEBUG_BR
302 printf("brelse(%p) ut3\n", pbp);
303 #endif
304 brelse(pbp);
305 ip->i_parent = paddr + (i << FILECORE_INO_INDEX);
306 return (paddr + (i << FILECORE_INO_INDEX));
307 }
308
309 /*static char filecore_hexchar[16]="0123456789abcdef";*/
310
311 int
312 filecore_fn2unix(fcfn, ufn, len)
313 char *fcfn;
314 char *ufn;
315 u_int8_t *len;
316 {
317 int i = 0;
318
319 if (*fcfn == 0)
320 return (-1);
321 while (i++ < 10 && *fcfn >= ' ') {
322 if (*fcfn == '/')
323 *ufn++ = '.';
324 else
325 *ufn++ = *fcfn;
326 fcfn++;
327 }
328 /* if (ip->i_mnt->fc_mntflags & FILECOREMNT_FILETYPE) {
329 *ufn++ = ',';
330 *ufn++ = filecore_hexchar[(ip->i_dirent.load >> 10) & 15];
331 *ufn++ = filecore_hexchar[(ip->i_dirent.load >> 9) & 15];
332 *ufn++ = filecore_hexchar[(ip->i_dirent.load >> 8) & 15];
333 } */
334 *ufn = 0;
335 *len = i - 1;
336 return 0;
337 }
338
339 int
340 filecore_fncmp(fcfn, ufn, len)
341 const char *fcfn;
342 const char *ufn;
343 u_short len;
344 {
345 char f, u;
346 int i = 0;
347
348 if (*fcfn == 0 || len > 10)
349 return -1;
350 while (i++ < len) {
351 if (*fcfn < ' ')
352 return 1;
353 f = *fcfn++;
354 u = *ufn++;
355 if (u == '.')
356 u = '/';
357 if (u >= 'a' && u <= 'z') u -= 'a' - 'A';
358 if (f >= 'a' && f <= 'z') f -= 'a' - 'A';
359 if (f < u)
360 return 1;
361 else if (f > u)
362 return -1;
363 }
364 if (len == 10 || *fcfn < ' ')
365 return 0;
366 return -1;
367 }
Cache object: 47dde8e54da51c79da95863c1df72536
|