ffs.c revision 1.2 1 /* $NetBSD: ffs.c,v 1.2 2002/04/30 14:21:17 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Fredette.
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if defined(__RCSID) && !defined(__lint)
41 __RCSID("$NetBSD: ffs.c,v 1.2 2002/04/30 14:21:17 lukem Exp $");
42 #endif /* !__lint */
43
44 #include <sys/param.h>
45 #include <sys/mount.h>
46
47 #include <assert.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <stdarg.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 #include "installboot.h"
58
59 #undef DIRBLKSIZ
60
61 #include <ufs/ufs/dinode.h>
62 #include <ufs/ufs/dir.h>
63 #include <ufs/ffs/fs.h>
64 #include <ufs/ffs/ffs_extern.h>
65 #include <ufs/ufs/ufs_bswap.h>
66
67 /* This reads a disk block from the filesystem. */
68 static int
69 ffs_read_disk_block(ib_params *params, uint32_t blkno, int size, char *blk)
70 {
71 int rv;
72
73 assert(params != NULL);
74 assert(blk != NULL);
75 assert(params->filesystem != NULL);
76 assert(params->fsfd != -1);
77 assert(blkno > 0);
78 assert(size > 0);
79 assert(blk != NULL);
80
81 rv = pread(params->fsfd, blk, size, blkno * DEV_BSIZE);
82 if (rv == -1) {
83 warn("Reading block %d in `%s'", blkno, params->filesystem);
84 return (0);
85 } else if (rv != size) {
86 warnx("Reading block %d in `%s': short read", blkno,
87 params->filesystem);
88 return (0);
89 }
90
91 return (1);
92 }
93
94 /*
95 * This iterates over the data blocks belonging to an inode,
96 * making a callback each iteration with the disk block number
97 * and the size.
98 */
99 static int
100 ffs_find_disk_blocks(ib_params *params, uint32_t ino,
101 int (*callback)(ib_params *, void *, uint32_t, uint32_t, int),
102 void *state)
103 {
104 char sbbuf[SBSIZE];
105 struct fs *fs;
106 int needswap;
107 char inodebuf[MAXBSIZE];
108 struct dinode *inode;
109 int level_i;
110 ufs_daddr_t blk, lblk, nblk;
111 int rv;
112 #define LEVELS 4
113 struct {
114 ufs_daddr_t *blknums;
115 unsigned long blkcount;
116 char diskbuf[MAXBSIZE];
117 } level[LEVELS];
118
119 assert(params != NULL);
120 assert(callback != NULL);
121 assert(state != NULL);
122
123 /* Read the superblock. */
124 if (! ffs_read_disk_block(params, SBLOCK, SBSIZE, sbbuf))
125 return (0);
126 fs = (struct fs *)sbbuf;
127 needswap = 0;
128 if (fs->fs_magic == htole32(FS_MAGIC)) {
129 #if BYTE_ORDER == BIG_ENDIAN
130 needswap = 1;
131 #endif
132 } else if (fs->fs_magic == htobe32(FS_MAGIC)) {
133 #if BYTE_ORDER == LITTLE_ENDIAN
134 needswap = 1;
135 #endif
136 } else
137 return (0);
138 if (needswap)
139 ffs_sb_swap(fs, fs);
140
141 /* Sanity check the superblock. */
142 if (fs->fs_magic != FS_MAGIC) {
143 warnx("Bad superblock magic number in `%s'",
144 params->filesystem);
145 return (0);
146 }
147 if (fs->fs_inopb <= 0) {
148 warnx("Bad inopb %d in superblock in `%s'",
149 fs->fs_inopb, params->filesystem);
150 return (0);
151 }
152
153 /* Read the inode. */
154 if (! ffs_read_disk_block(params, fsbtodb(fs, ino_to_fsba(fs, ino)),
155 fs->fs_bsize, inodebuf))
156 return (0);
157 inode = (struct dinode *)inodebuf;
158 inode += ino_to_fsbo(fs, ino);
159 if (needswap)
160 ffs_dinode_swap(inode, inode);
161
162 /* Get the block count and initialize for our block walk. */
163 nblk = howmany(inode->di_size, fs->fs_bsize);
164 lblk = 0;
165 level_i = 0;
166 level[0].blknums = &inode->di_db[0];
167 level[0].blkcount = NDADDR;
168 level[1].blknums = &inode->di_ib[0];
169 level[1].blkcount = 1;
170 level[2].blknums = &inode->di_ib[1];
171 level[2].blkcount = 1;
172 level[3].blknums = &inode->di_ib[2];
173 level[3].blkcount = 1;
174
175 /* Walk the data blocks. */
176 while (nblk > 0) {
177
178 /*
179 * If there are no more blocks at this indirection
180 * level, move up one indirection level and loop.
181 */
182 if (level[level_i].blkcount == 0) {
183 if (++level_i == LEVELS)
184 break;
185 continue;
186 }
187
188 /* Get the next block at this level. */
189 blk = *(level[level_i].blknums++);
190 level[level_i].blkcount--;
191 if (needswap)
192 blk = bswap32(blk);
193
194 #if 0
195 fprintf(stderr, "ino %lu blk %lu level %d\n", ino, blk,
196 level_i);
197 #endif
198
199 /*
200 * If we're not at the direct level, descend one
201 * level, read in that level's new block list,
202 * and loop.
203 */
204 if (level_i > 0) {
205 level_i--;
206 if (blk == 0)
207 memset(level[level_i].diskbuf, 0, MAXBSIZE);
208 else if (! ffs_read_disk_block(params,
209 fsbtodb(fs, blk),
210 fs->fs_bsize, level[level_i].diskbuf))
211 return (0);
212 level[level_i].blknums =
213 (ufs_daddr_t *)level[level_i].diskbuf;
214 level[level_i].blkcount = NINDIR(fs);
215 continue;
216 }
217
218 /* blk is the next direct level block. */
219 #if 0
220 fprintf(stderr, "ino %lu db %lu blksize %lu\n", ino,
221 fsbtodb(fs, blk), dblksize(fs, inode, lblk));
222 #endif
223 rv = (*callback)(params, state,
224 fsbtodb(fs, blk), dblksize(fs, inode, lblk), needswap);
225 lblk++;
226 nblk--;
227 if (rv != 1)
228 return (rv);
229 }
230
231 if (nblk != 0) {
232 warnx("Inode %d in `%s' ran out of blocks?", ino,
233 params->filesystem);
234 return (0);
235 }
236
237 return (1);
238 }
239
240 /*
241 * This callback reads a block of the root directory,
242 * searches for an entry for the secondary bootstrap,
243 * and saves the inode number if one is found.
244 */
245 static int
246 ffs_findstage2_ino(ib_params *params, void *_ino,
247 uint32_t blk, uint32_t blksize, int needswap)
248 {
249 char dirbuf[MAXBSIZE];
250 struct direct *de, *ede;
251 uint32_t ino;
252
253 assert(params != NULL);
254 assert(_ino != NULL);
255
256 /* Skip directory holes. */
257 if (blk == 0)
258 return (1);
259
260 /* Read the directory block. */
261 if (! ffs_read_disk_block(params, blk, blksize, dirbuf))
262 return (0);
263
264 /* Loop over the directory entries. */
265 de = (struct direct *)&dirbuf[0];
266 ede = (struct direct *)&dirbuf[blksize];
267 while (de < ede) {
268 ino = de->d_ino;
269 if (needswap) {
270 ino = bswap32(ino);
271 de->d_reclen = bswap16(de->d_reclen);
272 }
273 if (ino != 0 && strcmp(de->d_name, params->stage2) == 0) {
274 *((uint32_t *)_ino) = ino;
275 return (2);
276 }
277 de = (struct direct *)((char *)de + de->d_reclen);
278 }
279
280 return (1);
281 }
282
283 struct findblks_state {
284 uint32_t maxblk;
285 uint32_t nblk;
286 ib_block *blocks;
287 };
288
289 /* This callback records the blocks of the secondary bootstrap. */
290 static int
291 ffs_findstage2_blocks(ib_params *params, void *_state,
292 uint32_t blk, uint32_t blksize, int needswap)
293 {
294 struct findblks_state *state = _state;
295
296 assert(params != NULL);
297 assert(_state != NULL);
298
299 if (state->nblk == state->maxblk) {
300 warnx("Secondary bootstrap `%s' has too many blocks " \
301 "(max %d)\n", params->stage2, state->maxblk);
302 return (0);
303 }
304 state->blocks[state->nblk].block = blk;
305 state->blocks[state->nblk].blocksize = blksize;
306 state->nblk++;
307 return (1);
308 }
309
310 /* publically visible functions */
311
312 int
313 ffs_match(ib_params *params)
314 {
315 char sbbuf[SBSIZE];
316 struct fs *fs;
317
318 assert(params != NULL);
319 assert(params->stage2 != NULL);
320
321 /* Read and check the superblock. */
322 if (! ffs_read_disk_block(params, SBLOCK, SBSIZE, sbbuf))
323 return (0);
324 fs = (struct fs *)sbbuf;
325 if (fs->fs_magic == htole32(FS_MAGIC) ||
326 fs->fs_magic == htobe32(FS_MAGIC))
327 return (1);
328
329 return (0);
330 }
331
332 int
333 ffs_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
334 {
335 int rv;
336 uint32_t ino;
337 struct findblks_state state;
338
339 assert(params != NULL);
340 assert(params->stage2 != NULL);
341 assert(maxblk != NULL);
342 assert(blocks != NULL);
343
344 /* The secondary bootstrap must be clearly in /. */
345 if (params->stage2[0] == '/')
346 params->stage2++;
347 if (strchr(params->stage2, '/') != NULL) {
348 warnx("The secondary bootstrap `%s' must be in /",
349 params->stage2);
350 return (0);
351 }
352
353 /* Get the inode number of the secondary bootstrap. */
354 rv = ffs_find_disk_blocks(params, ROOTINO, ffs_findstage2_ino, &ino);
355 if (rv != 2)
356 return (0);
357
358 /* Record the disk blocks of the secondary bootstrap. */
359 state.maxblk = *maxblk;
360 state.nblk = 0;
361 state.blocks = blocks;
362 rv = ffs_find_disk_blocks(params, ino, ffs_findstage2_blocks, &state);
363 if (! rv)
364 return (0);
365
366 *maxblk = state.nblk;
367 return (1);
368 }
369