ffs.c revision 1.5 1 /* $NetBSD: ffs.c,v 1.5 2002/05/14 06:18:50 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.5 2002/05/14 06:18:50 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 static int ffs_read_disk_block(ib_params *, uint32_t, int, char *);
68 static int ffs_find_disk_blocks(ib_params *, uint32_t,
69 int (*)(ib_params *, void *, uint32_t, uint32_t), void *);
70 static int ffs_findstage2_ino(ib_params *, void *, uint32_t, uint32_t);
71 static int ffs_findstage2_blocks(ib_params *, void *, uint32_t, uint32_t);
72
73
74 /* This reads a disk block from the filesystem. */
75 static int
76 ffs_read_disk_block(ib_params *params, uint32_t blkno, int size, char *blk)
77 {
78 int rv;
79
80 assert(params != NULL);
81 assert(blk != NULL);
82 assert(params->filesystem != NULL);
83 assert(params->fsfd != -1);
84 assert(blkno > 0);
85 assert(size > 0);
86 assert(blk != NULL);
87
88 rv = pread(params->fsfd, blk, size, blkno * DEV_BSIZE);
89 if (rv == -1) {
90 warn("Reading block %d in `%s'", blkno, params->filesystem);
91 return (0);
92 } else if (rv != size) {
93 warnx("Reading block %d in `%s': short read", blkno,
94 params->filesystem);
95 return (0);
96 }
97
98 return (1);
99 }
100
101 /*
102 * This iterates over the data blocks belonging to an inode,
103 * making a callback each iteration with the disk block number
104 * and the size.
105 */
106 static int
107 ffs_find_disk_blocks(ib_params *params, uint32_t ino,
108 int (*callback)(ib_params *, void *, uint32_t, uint32_t),
109 void *state)
110 {
111 char sbbuf[SBSIZE];
112 struct fs *fs;
113 char inodebuf[MAXBSIZE];
114 struct dinode *inode;
115 int level_i;
116 ufs_daddr_t blk, lblk, nblk;
117 int rv;
118 #define LEVELS 4
119 struct {
120 ufs_daddr_t *blknums;
121 unsigned long blkcount;
122 char diskbuf[MAXBSIZE];
123 } level[LEVELS];
124
125 assert(params != NULL);
126 assert(params->fstype != NULL);
127 assert(callback != NULL);
128 assert(state != NULL);
129
130 /* Read the superblock. */
131 if (! ffs_read_disk_block(params, SBLOCK, SBSIZE, sbbuf))
132 return (0);
133 fs = (struct fs *)sbbuf;
134 if (params->fstype->needswap)
135 ffs_sb_swap(fs, fs);
136
137 /* Sanity check the superblock. */
138 if (fs->fs_magic != FS_MAGIC) {
139 warnx("Bad superblock magic number in `%s'",
140 params->filesystem);
141 return (0);
142 }
143 if (fs->fs_inopb <= 0) {
144 warnx("Bad inopb %d in superblock in `%s'",
145 fs->fs_inopb, params->filesystem);
146 return (0);
147 }
148
149 /* Read the inode. */
150 if (! ffs_read_disk_block(params, fsbtodb(fs, ino_to_fsba(fs, ino)),
151 fs->fs_bsize, inodebuf))
152 return (0);
153 inode = (struct dinode *)inodebuf;
154 inode += ino_to_fsbo(fs, ino);
155 if (params->fstype->needswap)
156 ffs_dinode_swap(inode, inode);
157
158 /* Get the block count and initialize for our block walk. */
159 nblk = howmany(inode->di_size, fs->fs_bsize);
160 lblk = 0;
161 level_i = 0;
162 level[0].blknums = &inode->di_db[0];
163 level[0].blkcount = NDADDR;
164 level[1].blknums = &inode->di_ib[0];
165 level[1].blkcount = 1;
166 level[2].blknums = &inode->di_ib[1];
167 level[2].blkcount = 1;
168 level[3].blknums = &inode->di_ib[2];
169 level[3].blkcount = 1;
170
171 /* Walk the data blocks. */
172 while (nblk > 0) {
173
174 /*
175 * If there are no more blocks at this indirection
176 * level, move up one indirection level and loop.
177 */
178 if (level[level_i].blkcount == 0) {
179 if (++level_i == LEVELS)
180 break;
181 continue;
182 }
183
184 /* Get the next block at this level. */
185 blk = *(level[level_i].blknums++);
186 level[level_i].blkcount--;
187 if (params->fstype->needswap)
188 blk = bswap32(blk);
189
190 #if 0
191 fprintf(stderr, "ino %lu blk %lu level %d\n", ino, blk,
192 level_i);
193 #endif
194
195 /*
196 * If we're not at the direct level, descend one
197 * level, read in that level's new block list,
198 * and loop.
199 */
200 if (level_i > 0) {
201 level_i--;
202 if (blk == 0)
203 memset(level[level_i].diskbuf, 0, MAXBSIZE);
204 else if (! ffs_read_disk_block(params,
205 fsbtodb(fs, blk),
206 fs->fs_bsize, level[level_i].diskbuf))
207 return (0);
208 level[level_i].blknums =
209 (ufs_daddr_t *)level[level_i].diskbuf;
210 level[level_i].blkcount = NINDIR(fs);
211 continue;
212 }
213
214 /* blk is the next direct level block. */
215 #if 0
216 fprintf(stderr, "ino %lu db %lu blksize %lu\n", ino,
217 fsbtodb(fs, blk), dblksize(fs, inode, lblk));
218 #endif
219 rv = (*callback)(params, state,
220 fsbtodb(fs, blk), dblksize(fs, inode, lblk));
221 lblk++;
222 nblk--;
223 if (rv != 1)
224 return (rv);
225 }
226
227 if (nblk != 0) {
228 warnx("Inode %d in `%s' ran out of blocks?", ino,
229 params->filesystem);
230 return (0);
231 }
232
233 return (1);
234 }
235
236 /*
237 * This callback reads a block of the root directory,
238 * searches for an entry for the secondary bootstrap,
239 * and saves the inode number if one is found.
240 */
241 static int
242 ffs_findstage2_ino(ib_params *params, void *_ino,
243 uint32_t blk, uint32_t blksize)
244 {
245 char dirbuf[MAXBSIZE];
246 struct direct *de, *ede;
247 uint32_t ino;
248
249 assert(params != NULL);
250 assert(params->fstype != NULL);
251 assert(params->stage2 != NULL);
252 assert(_ino != NULL);
253
254 /* Skip directory holes. */
255 if (blk == 0)
256 return (1);
257
258 /* Read the directory block. */
259 if (! ffs_read_disk_block(params, blk, blksize, dirbuf))
260 return (0);
261
262 /* Loop over the directory entries. */
263 de = (struct direct *)&dirbuf[0];
264 ede = (struct direct *)&dirbuf[blksize];
265 while (de < ede) {
266 ino = de->d_ino;
267 if (params->fstype->needswap) {
268 ino = bswap32(ino);
269 de->d_reclen = bswap16(de->d_reclen);
270 }
271 if (ino != 0 && strcmp(de->d_name, params->stage2) == 0) {
272 *((uint32_t *)_ino) = ino;
273 return (2);
274 }
275 if (de->d_reclen == 0)
276 break;
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)
293 {
294 struct findblks_state *state = _state;
295
296 assert(params != NULL);
297 assert(params->stage2 != NULL);
298 assert(_state != NULL);
299
300 if (state->nblk == state->maxblk) {
301 warnx("Secondary bootstrap `%s' has too many blocks " \
302 "(max %d)\n", params->stage2, state->maxblk);
303 return (0);
304 }
305 state->blocks[state->nblk].block = blk;
306 state->blocks[state->nblk].blocksize = blksize;
307 state->nblk++;
308 return (1);
309 }
310
311 /*
312 * publically visible functions
313 */
314
315 int
316 ffs_match(ib_params *params)
317 {
318 char sbbuf[SBSIZE];
319 struct fs *fs;
320
321 assert(params != NULL);
322 assert(params->fstype != NULL);
323
324 /* Read and check the superblock. */
325 if (! ffs_read_disk_block(params, SBLOCK, SBSIZE, sbbuf))
326 return (0);
327 fs = (struct fs *)sbbuf;
328 if (fs->fs_magic == FS_MAGIC) {
329 params->fstype->needswap = 0;
330 params->fstype->blocksize = fs->fs_bsize;
331 } else if (fs->fs_magic == bswap32(FS_MAGIC)) {
332 params->fstype->needswap = 1;
333 params->fstype->blocksize = bswap32(fs->fs_bsize);
334 } else {
335 return (0);
336 }
337
338 return (1);
339 }
340
341 int
342 ffs_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
343 {
344 int rv;
345 uint32_t ino;
346 struct findblks_state state;
347
348 assert(params != NULL);
349 assert(params->stage2 != NULL);
350 assert(maxblk != NULL);
351 assert(blocks != NULL);
352
353 if (params->flags & IB_STAGE2START)
354 return (hardcode_stage2(params, maxblk, blocks));
355
356 /* The secondary bootstrap must be clearly in /. */
357 if (params->stage2[0] == '/')
358 params->stage2++;
359 if (strchr(params->stage2, '/') != NULL) {
360 warnx("The secondary bootstrap `%s' must be in /",
361 params->stage2);
362 return (0);
363 }
364
365 /* Get the inode number of the secondary bootstrap. */
366 rv = ffs_find_disk_blocks(params, ROOTINO, ffs_findstage2_ino, &ino);
367 if (rv != 2)
368 return (0);
369
370 /* Record the disk blocks of the secondary bootstrap. */
371 state.maxblk = *maxblk;
372 state.nblk = 0;
373 state.blocks = blocks;
374 rv = ffs_find_disk_blocks(params, ino, ffs_findstage2_blocks, &state);
375 if (! rv)
376 return (0);
377
378 *maxblk = state.nblk;
379 return (1);
380 }
381