ffs.c revision 1.23 1 /* $NetBSD: ffs.c,v 1.23 2006/10/23 19:44:32 christos 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 #if HAVE_NBTOOL_CONFIG_H
40 #include "nbtool_config.h"
41 #endif
42
43 #include <sys/cdefs.h>
44 #if defined(__RCSID) && !defined(__lint)
45 __RCSID("$NetBSD: ffs.c,v 1.23 2006/10/23 19:44:32 christos Exp $");
46 #endif /* !__lint */
47
48 #include <sys/param.h>
49
50 #if !HAVE_NBTOOL_CONFIG_H
51 #include <sys/mount.h>
52 #endif
53
54 #include <assert.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <stdarg.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 #include "installboot.h"
65
66 /* From <dev/raidframe/raidframevar.h> */
67 #define RF_PROTECTED_SECTORS 64L
68
69 #undef DIRBLKSIZ
70
71 #include <ufs/ufs/dinode.h>
72 #include <ufs/ufs/dir.h>
73 #include <ufs/ffs/fs.h>
74 #include <ufs/ffs/ffs_extern.h>
75 #ifndef NO_FFS_SWAP
76 #include <ufs/ufs/ufs_bswap.h>
77 #else
78 #define ffs_sb_swap(fs_a, fs_b)
79 #define ffs_dinode1_swap(inode_a, inode_b)
80 #define ffs_dinode2_swap(inode_a, inode_b)
81 #endif
82
83 static int ffs_match_common(ib_params *, off_t);
84 static int ffs_read_disk_block(ib_params *, uint64_t, int, char []);
85 static int ffs_find_disk_blocks_ufs1(ib_params *, ino_t,
86 int (*)(ib_params *, void *, uint64_t, uint32_t), void *);
87 static int ffs_find_disk_blocks_ufs2(ib_params *, ino_t,
88 int (*)(ib_params *, void *, uint64_t, uint32_t), void *);
89 static int ffs_findstage2_ino(ib_params *, void *, uint64_t, uint32_t);
90 static int ffs_findstage2_blocks(ib_params *, void *, uint64_t, uint32_t);
91
92 static int is_ufs2;
93
94
95 /* This reads a disk block from the filesystem. */
96 static int
97 ffs_read_disk_block(ib_params *params, uint64_t blkno, int size, char blk[])
98 {
99 int rv;
100
101 assert(params != NULL);
102 assert(params->filesystem != NULL);
103 assert(params->fsfd != -1);
104 assert(size > 0);
105 assert(blk != NULL);
106
107 rv = pread(params->fsfd, blk, size, blkno * DEV_BSIZE);
108 if (rv == -1) {
109 warn("Reading block %llu in `%s'",
110 (unsigned long long)blkno, params->filesystem);
111 return (0);
112 } else if (rv != size) {
113 warnx("Reading block %llu in `%s': short read",
114 (unsigned long long)blkno, params->filesystem);
115 return (0);
116 }
117
118 return (1);
119 }
120
121 /*
122 * This iterates over the data blocks belonging to an inode,
123 * making a callback each iteration with the disk block number
124 * and the size.
125 */
126 static int
127 ffs_find_disk_blocks_ufs1(ib_params *params, ino_t ino,
128 int (*callback)(ib_params *, void *, uint64_t, uint32_t),
129 void *state)
130 {
131 char sbbuf[SBLOCKSIZE];
132 struct fs *fs;
133 char inodebuf[MAXBSIZE];
134 struct ufs1_dinode *inode;
135 int level_i;
136 int32_t blk, lblk, nblk;
137 int rv;
138 #define LEVELS 4
139 struct {
140 int32_t *blknums;
141 unsigned long blkcount;
142 char diskbuf[MAXBSIZE];
143 } level[LEVELS];
144
145 assert(params != NULL);
146 assert(params->fstype != NULL);
147 assert(callback != NULL);
148 assert(state != NULL);
149
150 /* Read the superblock. */
151 if (!ffs_read_disk_block(params, params->fstype->sblockloc, SBLOCKSIZE,
152 sbbuf))
153 return (0);
154 fs = (struct fs *)sbbuf;
155 if (params->fstype->needswap)
156 ffs_sb_swap(fs, fs);
157
158 if (fs->fs_inopb <= 0) {
159 warnx("Bad inopb %d in superblock in `%s'",
160 fs->fs_inopb, params->filesystem);
161 return (0);
162 }
163
164 /* Read the inode. */
165 if (! ffs_read_disk_block(params,
166 fsbtodb(fs, ino_to_fsba(fs, ino)) + params->fstype->offset,
167 fs->fs_bsize, inodebuf))
168 return (0);
169 inode = (struct ufs1_dinode *)inodebuf;
170 inode += ino_to_fsbo(fs, ino);
171 if (params->fstype->needswap)
172 ffs_dinode1_swap(inode, inode);
173
174 /* Get the block count and initialize for our block walk. */
175 nblk = howmany(inode->di_size, fs->fs_bsize);
176 lblk = 0;
177 level_i = 0;
178 level[0].blknums = &inode->di_db[0];
179 level[0].blkcount = NDADDR;
180 level[1].blknums = &inode->di_ib[0];
181 level[1].blkcount = 1;
182 level[2].blknums = &inode->di_ib[1];
183 level[2].blkcount = 1;
184 level[3].blknums = &inode->di_ib[2];
185 level[3].blkcount = 1;
186
187 /* Walk the data blocks. */
188 while (nblk > 0) {
189
190 /*
191 * If there are no more blocks at this indirection
192 * level, move up one indirection level and loop.
193 */
194 if (level[level_i].blkcount == 0) {
195 if (++level_i == LEVELS)
196 break;
197 continue;
198 }
199
200 /* Get the next block at this level. */
201 blk = *(level[level_i].blknums++);
202 level[level_i].blkcount--;
203 if (params->fstype->needswap)
204 blk = bswap32(blk);
205
206 #if 0
207 fprintf(stderr, "ino %lu blk %lu level %d\n", ino, blk,
208 level_i);
209 #endif
210
211 /*
212 * If we're not at the direct level, descend one
213 * level, read in that level's new block list,
214 * and loop.
215 */
216 if (level_i > 0) {
217 level_i--;
218 if (blk == 0)
219 memset(level[level_i].diskbuf, 0, MAXBSIZE);
220 else if (! ffs_read_disk_block(params,
221 fsbtodb(fs, blk) + params->fstype->offset,
222 fs->fs_bsize, level[level_i].diskbuf))
223 return (0);
224 /* XXX ondisk32 */
225 level[level_i].blknums =
226 (int32_t *)level[level_i].diskbuf;
227 level[level_i].blkcount = NINDIR(fs);
228 continue;
229 }
230
231 /* blk is the next direct level block. */
232 #if 0
233 fprintf(stderr, "ino %lu db %lu blksize %lu\n", ino,
234 fsbtodb(fs, blk), sblksize(fs, inode->di_size, lblk));
235 #endif
236 rv = (*callback)(params, state,
237 fsbtodb(fs, blk) + params->fstype->offset,
238 sblksize(fs, inode->di_size, lblk));
239 lblk++;
240 nblk--;
241 if (rv != 1)
242 return (rv);
243 }
244
245 if (nblk != 0) {
246 warnx("Inode %llu in `%s' ran out of blocks?",
247 (unsigned long long)ino, params->filesystem);
248 return (0);
249 }
250
251 return (1);
252 }
253
254 /*
255 * This iterates over the data blocks belonging to an inode,
256 * making a callback each iteration with the disk block number
257 * and the size.
258 */
259 static int
260 ffs_find_disk_blocks_ufs2(ib_params *params, ino_t ino,
261 int (*callback)(ib_params *, void *, uint64_t, uint32_t),
262 void *state)
263 {
264 char sbbuf[SBLOCKSIZE];
265 struct fs *fs;
266 char inodebuf[MAXBSIZE];
267 struct ufs2_dinode *inode;
268 int level_i;
269 int64_t blk, lblk, nblk;
270 int rv;
271 #define LEVELS 4
272 struct {
273 int64_t *blknums;
274 unsigned long blkcount;
275 char diskbuf[MAXBSIZE];
276 } level[LEVELS];
277
278 assert(params != NULL);
279 assert(params->fstype != NULL);
280 assert(callback != NULL);
281 assert(state != NULL);
282
283 /* Read the superblock. */
284 if (!ffs_read_disk_block(params, params->fstype->sblockloc, SBLOCKSIZE,
285 sbbuf))
286 return (0);
287 fs = (struct fs *)sbbuf;
288 if (params->fstype->needswap)
289 ffs_sb_swap(fs, fs);
290
291 if (fs->fs_inopb <= 0) {
292 warnx("Bad inopb %d in superblock in `%s'",
293 fs->fs_inopb, params->filesystem);
294 return (0);
295 }
296
297 /* Read the inode. */
298 if (! ffs_read_disk_block(params,
299 fsbtodb(fs, ino_to_fsba(fs, ino)) + params->fstype->offset,
300 fs->fs_bsize, inodebuf))
301 return (0);
302 inode = (struct ufs2_dinode *)inodebuf;
303 inode += ino_to_fsbo(fs, ino);
304 if (params->fstype->needswap)
305 ffs_dinode2_swap(inode, inode);
306
307 /* Get the block count and initialize for our block walk. */
308 nblk = howmany(inode->di_size, fs->fs_bsize);
309 lblk = 0;
310 level_i = 0;
311 level[0].blknums = &inode->di_db[0];
312 level[0].blkcount = NDADDR;
313 level[1].blknums = &inode->di_ib[0];
314 level[1].blkcount = 1;
315 level[2].blknums = &inode->di_ib[1];
316 level[2].blkcount = 1;
317 level[3].blknums = &inode->di_ib[2];
318 level[3].blkcount = 1;
319
320 /* Walk the data blocks. */
321 while (nblk > 0) {
322
323 /*
324 * If there are no more blocks at this indirection
325 * level, move up one indirection level and loop.
326 */
327 if (level[level_i].blkcount == 0) {
328 if (++level_i == LEVELS)
329 break;
330 continue;
331 }
332
333 /* Get the next block at this level. */
334 blk = *(level[level_i].blknums++);
335 level[level_i].blkcount--;
336 if (params->fstype->needswap)
337 blk = bswap64(blk);
338
339 #if 0
340 fprintf(stderr, "ino %lu blk %llu level %d\n", ino,
341 (unsigned long long)blk, level_i);
342 #endif
343
344 /*
345 * If we're not at the direct level, descend one
346 * level, read in that level's new block list,
347 * and loop.
348 */
349 if (level_i > 0) {
350 level_i--;
351 if (blk == 0)
352 memset(level[level_i].diskbuf, 0, MAXBSIZE);
353 else if (! ffs_read_disk_block(params,
354 fsbtodb(fs, blk) + params->fstype->offset,
355 fs->fs_bsize, level[level_i].diskbuf))
356 return (0);
357 level[level_i].blknums =
358 (int64_t *)level[level_i].diskbuf;
359 level[level_i].blkcount = NINDIR(fs);
360 continue;
361 }
362
363 /* blk is the next direct level block. */
364 #if 0
365 fprintf(stderr, "ino %lu db %llu blksize %lu\n", ino,
366 fsbtodb(fs, blk), sblksize(fs, inode->di_size, lblk));
367 #endif
368 rv = (*callback)(params, state,
369 fsbtodb(fs, blk) + params->fstype->offset,
370 sblksize(fs, inode->di_size, lblk));
371 lblk++;
372 nblk--;
373 if (rv != 1)
374 return (rv);
375 }
376
377 if (nblk != 0) {
378 warnx("Inode %llu in `%s' ran out of blocks?",
379 (unsigned long long)ino, params->filesystem);
380 return (0);
381 }
382
383 return (1);
384 }
385
386 /*
387 * This callback reads a block of the root directory,
388 * searches for an entry for the secondary bootstrap,
389 * and saves the inode number if one is found.
390 */
391 static int
392 ffs_findstage2_ino(ib_params *params, void *_ino,
393 uint64_t blk, uint32_t blksize)
394 {
395 char dirbuf[MAXBSIZE];
396 struct direct *de, *ede;
397 uint32_t ino;
398
399 assert(params != NULL);
400 assert(params->fstype != NULL);
401 assert(params->stage2 != NULL);
402 assert(_ino != NULL);
403
404 /* Skip directory holes. */
405 if (blk == 0)
406 return (1);
407
408 /* Read the directory block. */
409 if (! ffs_read_disk_block(params, blk, blksize, dirbuf))
410 return (0);
411
412 /* Loop over the directory entries. */
413 de = (struct direct *)&dirbuf[0];
414 ede = (struct direct *)&dirbuf[blksize];
415 while (de < ede) {
416 ino = de->d_fileno;
417 if (params->fstype->needswap) {
418 ino = bswap32(ino);
419 de->d_reclen = bswap16(de->d_reclen);
420 }
421 if (ino != 0 && strcmp(de->d_name, params->stage2) == 0) {
422 *((uint32_t *)_ino) = ino;
423 return (2);
424 }
425 if (de->d_reclen == 0)
426 break;
427 de = (struct direct *)((char *)de + de->d_reclen);
428 }
429
430 return (1);
431 }
432
433 struct findblks_state {
434 uint32_t maxblk;
435 uint32_t nblk;
436 ib_block *blocks;
437 };
438
439 /* This callback records the blocks of the secondary bootstrap. */
440 static int
441 ffs_findstage2_blocks(ib_params *params, void *_state,
442 uint64_t blk, uint32_t blksize)
443 {
444 struct findblks_state *state = _state;
445
446 assert(params != NULL);
447 assert(params->stage2 != NULL);
448 assert(_state != NULL);
449
450 if (state->nblk == state->maxblk) {
451 warnx("Secondary bootstrap `%s' has too many blocks (max %d)",
452 params->stage2, state->maxblk);
453 return (0);
454 }
455 state->blocks[state->nblk].block = blk;
456 state->blocks[state->nblk].blocksize = blksize;
457 state->nblk++;
458 return (1);
459 }
460
461 /*
462 * publicly visible functions
463 */
464
465 static off_t sblock_try[] = SBLOCKSEARCH;
466
467 int
468 ffs_match(ib_params *params)
469 {
470 return ffs_match_common(params, (off_t) 0);
471 }
472
473 int
474 raid_match(ib_params *params)
475 {
476 /* XXX Assumes 512 bytes / sector */
477 if (DEV_BSIZE != 512) {
478 warnx("Media is %d bytes/sector."
479 " RAID is only supported on 512 bytes/sector media.",
480 DEV_BSIZE);
481 return 0;
482 }
483 return ffs_match_common(params, (off_t) RF_PROTECTED_SECTORS);
484 }
485
486 int
487 ffs_match_common(ib_params *params, off_t offset)
488 {
489 char sbbuf[SBLOCKSIZE];
490 struct fs *fs;
491 int i;
492 off_t loc;
493
494 assert(params != NULL);
495 assert(params->fstype != NULL);
496
497 fs = (struct fs *)sbbuf;
498 for (i = 0; sblock_try[i] != -1; i++) {
499 loc = sblock_try[i] / DEV_BSIZE + offset;
500 if (!ffs_read_disk_block(params, loc, SBLOCKSIZE, sbbuf))
501 continue;
502 switch (fs->fs_magic) {
503 case FS_UFS2_MAGIC:
504 is_ufs2 = 1;
505 /* FALLTHROUGH */
506 case FS_UFS1_MAGIC:
507 params->fstype->needswap = 0;
508 params->fstype->blocksize = fs->fs_bsize;
509 params->fstype->sblockloc = loc;
510 params->fstype->offset = offset;
511 break;
512 #ifndef FFS_NO_SWAP
513 case FS_UFS2_MAGIC_SWAPPED:
514 is_ufs2 = 1;
515 /* FALLTHROUGH */
516 case FS_UFS1_MAGIC_SWAPPED:
517 params->fstype->needswap = 1;
518 params->fstype->blocksize = bswap32(fs->fs_bsize);
519 params->fstype->sblockloc = loc;
520 params->fstype->offset = offset;
521 break;
522 #endif
523 default:
524 continue;
525 }
526 if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
527 continue;
528 return 1;
529 }
530
531 return (0);
532 }
533
534 int
535 ffs_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
536 {
537 int rv;
538 uint32_t ino;
539 struct findblks_state state;
540
541 assert(params != NULL);
542 assert(params->stage2 != NULL);
543 assert(maxblk != NULL);
544 assert(blocks != NULL);
545
546 if (params->flags & IB_STAGE2START)
547 return (hardcode_stage2(params, maxblk, blocks));
548
549 /* The secondary bootstrap must be clearly in /. */
550 if (params->stage2[0] == '/')
551 params->stage2++;
552 if (strchr(params->stage2, '/') != NULL) {
553 warnx("The secondary bootstrap `%s' must be in /",
554 params->stage2);
555 return (0);
556 }
557
558 /* Get the inode number of the secondary bootstrap. */
559 if (is_ufs2)
560 rv = ffs_find_disk_blocks_ufs2(params, ROOTINO,
561 ffs_findstage2_ino, &ino);
562 else
563 rv = ffs_find_disk_blocks_ufs1(params, ROOTINO,
564 ffs_findstage2_ino, &ino);
565 if (rv != 2) {
566 warnx("Could not find secondary bootstrap `%s' in `%s'",
567 params->stage2, params->filesystem);
568 return (0);
569 }
570
571 /* Record the disk blocks of the secondary bootstrap. */
572 state.maxblk = *maxblk;
573 state.nblk = 0;
574 state.blocks = blocks;
575 if (is_ufs2)
576 rv = ffs_find_disk_blocks_ufs2(params, ino,
577 ffs_findstage2_blocks, &state);
578 else
579 rv = ffs_find_disk_blocks_ufs1(params, ino,
580 ffs_findstage2_blocks, &state);
581 if (! rv) {
582 return (0);
583 }
584
585 *maxblk = state.nblk;
586 return (1);
587 }
588