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