ffs.c revision 1.4 1 /* $NetBSD: ffs.c,v 1.4 2002/05/07 12:22:23 pk 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.4 2002/05/07 12:22:23 pk 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(params->stage2 != NULL);
255 assert(_ino != NULL);
256
257 /* Skip directory holes. */
258 if (blk == 0)
259 return (1);
260
261 /* Read the directory block. */
262 if (! ffs_read_disk_block(params, blk, blksize, dirbuf))
263 return (0);
264
265 /* Loop over the directory entries. */
266 de = (struct direct *)&dirbuf[0];
267 ede = (struct direct *)&dirbuf[blksize];
268 while (de < ede) {
269 ino = de->d_ino;
270 if (needswap) {
271 ino = bswap32(ino);
272 de->d_reclen = bswap16(de->d_reclen);
273 }
274 if (ino != 0 && strcmp(de->d_name, params->stage2) == 0) {
275 *((uint32_t *)_ino) = ino;
276 return (2);
277 }
278 if (de->d_reclen == 0)
279 break;
280 de = (struct direct *)((char *)de + de->d_reclen);
281 }
282
283 return (1);
284 }
285
286 struct findblks_state {
287 uint32_t maxblk;
288 uint32_t nblk;
289 ib_block *blocks;
290 };
291
292 /* This callback records the blocks of the secondary bootstrap. */
293 static int
294 ffs_findstage2_blocks(ib_params *params, void *_state,
295 uint32_t blk, uint32_t blksize, int needswap)
296 {
297 struct findblks_state *state = _state;
298
299 assert(params != NULL);
300 assert(params->stage2 != NULL);
301 assert(_state != NULL);
302
303 if (state->nblk == state->maxblk) {
304 warnx("Secondary bootstrap `%s' has too many blocks " \
305 "(max %d)\n", params->stage2, state->maxblk);
306 return (0);
307 }
308 state->blocks[state->nblk].block = blk;
309 state->blocks[state->nblk].blocksize = blksize;
310 state->nblk++;
311 return (1);
312 }
313
314 /* publically visible functions */
315
316 int
317 ffs_match(ib_params *params)
318 {
319 char sbbuf[SBSIZE];
320 struct fs *fs;
321
322 assert(params != 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 == htole32(FS_MAGIC) ||
329 fs->fs_magic == htobe32(FS_MAGIC))
330 return (1);
331
332 return (0);
333 }
334
335 int
336 ffs_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
337 {
338 int rv;
339 uint32_t ino;
340 struct findblks_state state;
341
342 assert(params != NULL);
343 assert(params->stage2 != NULL);
344 assert(maxblk != NULL);
345 assert(blocks != NULL);
346
347 /* The secondary bootstrap must be clearly in /. */
348 if (params->stage2[0] == '/')
349 params->stage2++;
350 if (strchr(params->stage2, '/') != NULL) {
351 warnx("The secondary bootstrap `%s' must be in /",
352 params->stage2);
353 return (0);
354 }
355
356 /* Get the inode number of the secondary bootstrap. */
357 rv = ffs_find_disk_blocks(params, ROOTINO, ffs_findstage2_ino, &ino);
358 if (rv != 2)
359 return (0);
360
361 /* Record the disk blocks of the secondary bootstrap. */
362 state.maxblk = *maxblk;
363 state.nblk = 0;
364 state.blocks = blocks;
365 rv = ffs_find_disk_blocks(params, ino, ffs_findstage2_blocks, &state);
366 if (! rv)
367 return (0);
368
369 *maxblk = state.nblk;
370 return (1);
371 }
372